UNPKG

3.47 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var _memoryFs = _interopRequireDefault(require("./memory-fs"));
7
8var _proxyFileSystem = _interopRequireDefault(require("./proxyFileSystem"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12const PLUGIN = 'css-literal-loader';
13
14class VirtualModulePlugin {
15 /**
16 * Apply an instance of the plugin to compilation.
17 * helpful for adding the plugin inside a loader.
18 */
19 static bootstrap(compilation, files) {
20 const {
21 compiler
22 } = compilation;
23 const plugin = new VirtualModulePlugin(files);
24 plugin.augmentCompilerFileSystem(compiler);
25 compilation.inputFileSystem = compiler.inputFileSystem; // v3
26
27 if (!compiler.resolverFactory) return plugin; // this is suuuch a hack
28 // we need to ensure that resolvers are rebuilt with the new filesystem, and
29 // I don't know the right way to do that.
30 // pre 4.36.1 this cache also existed
31
32 if (compiler.resolverFactory.cache1) compiler.resolverFactory.cache1 = new WeakMap(); // >=4.36.1 this one is needed
33
34 if (compiler.resolverFactory.cache2) compiler.resolverFactory.cache2.clear();
35 return plugin;
36 }
37
38 constructor(files) {
39 this.addFile = (virtualPath, content) => {
40 this.fs.addFile(virtualPath, content);
41 };
42
43 this.fs = new _memoryFs.default();
44
45 if (files) {
46 Object.keys(files).forEach(key => {
47 this.addFile(key, files[key]);
48 });
49 }
50 }
51
52 augmentCompilerFileSystem(compiler) {
53 if (this.augmented === true) {
54 return;
55 }
56
57 const fs = (0, _proxyFileSystem.default)(compiler.inputFileSystem, this.fs);
58 compiler.inputFileSystem = fs;
59
60 if (!compiler.hooks) {
61 compiler.resolvers.normal.fileSystem = fs;
62 compiler.resolvers.context.fileSystem = fs;
63 compiler.resolvers.loader.fileSystem = fs;
64 } else {
65 /**
66 * When webpack is in watch mode, the map of file timestamps is computed
67 * from the watcher instance, which uses the real filesystem and as a
68 * result the virtual css files are not found in this map.
69 * To correct this, we manually add these files to the map here.
70 * @see https://github.com/4Catalyzer/astroturf/pull/381
71 */
72 compiler.hooks.watchRun.tapAsync('astroturf', ({
73 fileTimestamps
74 }, callback) => {
75 this.fs.getPaths().forEach((value, key) => {
76 const mtime = +value.mtime;
77 fileTimestamps.set(key, mtime);
78 });
79 callback();
80 });
81 }
82
83 this.augmented = true;
84 }
85
86 apply(compiler) {
87 const augmentOnCompile = () => {
88 this.augmentCompilerFileSystem(compiler);
89 };
90
91 const augmentLoaderContext = loaderContext => {
92 loaderContext.emitVirtualFile = this.addFile;
93 }; // if the fs is already present then immediately augment it
94
95
96 if (compiler.inputFileSystem) this.augmentCompilerFileSystem(compiler);
97
98 if (compiler.hooks) {
99 compiler.hooks.compile.tap(PLUGIN, augmentOnCompile);
100 compiler.hooks.compilation.tap(PLUGIN, compilation => {
101 compilation.hooks.normalModuleLoader.tap(PLUGIN, augmentLoaderContext);
102 });
103 } else {
104 compiler.plugin('compile', augmentOnCompile);
105 compiler.plugin('compilation', compilation => {
106 compilation.plugin('normal-module-loader', augmentLoaderContext);
107 });
108 }
109 }
110
111}
112
113var _default = VirtualModulePlugin;
114exports.default = _default;
\No newline at end of file