UNPKG

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