Skip to content

Commit

Permalink
fix(hmr): reload from changed css import in global styles
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Oct 18, 2020
1 parent f1488a9 commit 4f8934d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/compiler/build/compiler-ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class CompilerContext implements d.CompilerCtx {
addWatchDir: (path: string) => void = noop;
addWatchFile: (path: string) => void = noop;
cache: d.Cache;
cachedStyleMeta = new Map<string, d.StyleCompiler>();
cssModuleImports = new Map<string, string[]>();
changedFiles = new Set<string>();
changedModules = new Set<string>();
collections: d.CollectionCompilerMeta[] = [];
Expand All @@ -43,7 +43,7 @@ export class CompilerContext implements d.CompilerCtx {

reset() {
this.cache.clear();
this.cachedStyleMeta.clear();
this.cssModuleImports.clear();
this.cachedGlobalStyle = null;
this.collections.length = 0;
this.compilerOptions = null;
Expand Down
15 changes: 14 additions & 1 deletion src/compiler/style/global-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ const buildGlobalStyles = async (config: d.Config, compilerCtx: d.CompilerCtx, b
globalStylePath,
);
compilerCtx.cachedGlobalStyle = optimizedCss;

if (Array.isArray(transformResults.dependencies)) {
transformResults.dependencies.forEach(dep => compilerCtx.addWatchFile(dep));
const cssModuleImports = compilerCtx.cssModuleImports.get(globalStylePath) || [];
transformResults.dependencies.forEach(dep => {
compilerCtx.addWatchFile(dep);
if (!cssModuleImports.includes(dep)) {
cssModuleImports.push(dep);
}
});
compilerCtx.cssModuleImports.set(globalStylePath, cssModuleImports);
}
return optimizedCss;
}
Expand Down Expand Up @@ -75,6 +83,11 @@ const canSkipGlobalStyles = async (config: d.Config, compilerCtx: d.CompilerCtx,
return false;
}

const cssModuleImports = compilerCtx.cssModuleImports.get(config.globalStyle);
if (cssModuleImports && buildCtx.filesChanged.some(f => cssModuleImports.includes(f))) {
return false;
}

const hasChangedImports = await hasChangedImportFile(
config,
compilerCtx,
Expand Down
2 changes: 1 addition & 1 deletion src/declarations/stencil-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ export interface CompilerCtx {
addWatchDir: (path: string, recursive: boolean) => void;
addWatchFile: (path: string) => void;
cache: Cache;
cachedStyleMeta: Map<string, StyleCompiler>;
cssModuleImports: Map<string, string[]>;
cachedGlobalStyle: string;
collections: CollectionCompilerMeta[];
compilerOptions: any;
Expand Down
2 changes: 1 addition & 1 deletion src/testing/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function mockCompilerCtx(config?: Config) {
collections: [],
compilerOptions: null,
cache: null,
cachedStyleMeta: new Map(),
cssModuleImports: new Map(),
events: null,
fs: null,
hasSuccessfulBuild: false,
Expand Down

0 comments on commit 4f8934d

Please sign in to comment.