Skip to content

Commit

Permalink
fix(perf): add virtual css timestamps to webpack compilation (#381)
Browse files Browse the repository at this point in the history
fixes webpack issue whereby timestamps for virtual css files are always
undefined when webpack is in watch mode
  • Loading branch information
AlexFell-Velo authored and jquense committed Sep 3, 2019
1 parent c28f1f8 commit d9965ee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/VirtualModulePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ class VirtualModulePlugin {
compiler.resolvers.normal.fileSystem = fs;
compiler.resolvers.context.fileSystem = fs;
compiler.resolvers.loader.fileSystem = fs;
} else {
/**
* When webpack is in watch mode, the map of file timestamps is computed
* from the watcher instance, which uses the real filesystem and as a
* result the virtual css files are not found in this map.
* To correct this, we manually add these files to the map here.
* @see https://github.com/4Catalyzer/astroturf/pull/381
*/
compiler.hooks.watchRun.tapAsync(
'astroturf',
({ fileTimestamps }, callback) => {
this.fs.getPaths().forEach((value, key) => {
const mtime = +value.mtime;
fileTimestamps.set(key, mtime);
});

callback();
},
);
}

this.augmented = true;
Expand Down
2 changes: 2 additions & 0 deletions src/memory-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class MemoryFs {
});
};

getPaths = () => this.paths;

exists = (p, cb) => cb(this.existsSync(p));

existsSync = p => this.paths.has(path.normalize(p));
Expand Down
4 changes: 3 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export function runLoader(src, options, filename = 'MyStyleFile.js') {
resourcePath: filename,
request: `babel-loader!css-literal-loader!${filename}`,
_compiler: {},
_compilation: {},
_compilation: {
fileTimestamps: new Map(),
},
_module: {},
resolve(request, cb) {
cb(null, relative(dirname(filename), request));
Expand Down

0 comments on commit d9965ee

Please sign in to comment.