1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { RawSource } = require("webpack-sources");
|
9 | const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
|
10 | const Dependency = require("../Dependency");
|
11 | const Module = require("../Module");
|
12 | const ModuleFactory = require("../ModuleFactory");
|
13 | const RuntimeGlobals = require("../RuntimeGlobals");
|
14 | const Template = require("../Template");
|
15 | const CommonJsRequireDependency = require("../dependencies/CommonJsRequireDependency");
|
16 | const { registerNotSerializable } = require("../util/serialization");
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | const IGNORED_DEPENDENCY_TYPES = new Set([
|
42 | "import.meta.webpackHot.accept",
|
43 | "import.meta.webpackHot.decline",
|
44 | "module.hot.accept",
|
45 | "module.hot.decline"
|
46 | ]);
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | const checkTest = (test, module) => {
|
54 | if (test === undefined) return true;
|
55 | if (typeof test === "function") {
|
56 | return test(module);
|
57 | }
|
58 | if (typeof test === "string") {
|
59 | const name = module.nameForCondition();
|
60 | return name && name.startsWith(test);
|
61 | }
|
62 | if (test instanceof RegExp) {
|
63 | const name = module.nameForCondition();
|
64 | return name && test.test(name);
|
65 | }
|
66 | return false;
|
67 | };
|
68 |
|
69 | const TYPES = new Set(["javascript"]);
|
70 |
|
71 | class LazyCompilationDependency extends Dependency {
|
72 | constructor(proxyModule) {
|
73 | super();
|
74 | this.proxyModule = proxyModule;
|
75 | }
|
76 |
|
77 | get category() {
|
78 | return "esm";
|
79 | }
|
80 |
|
81 | get type() {
|
82 | return "lazy import()";
|
83 | }
|
84 |
|
85 | |
86 |
|
87 |
|
88 | getResourceIdentifier() {
|
89 | return this.proxyModule.originalModule.identifier();
|
90 | }
|
91 | }
|
92 |
|
93 | registerNotSerializable(LazyCompilationDependency);
|
94 |
|
95 | class LazyCompilationProxyModule extends Module {
|
96 | constructor(context, originalModule, request, client, data, active) {
|
97 | super("lazy-compilation-proxy", context, originalModule.layer);
|
98 | this.originalModule = originalModule;
|
99 | this.request = request;
|
100 | this.client = client;
|
101 | this.data = data;
|
102 | this.active = active;
|
103 | }
|
104 |
|
105 | |
106 |
|
107 |
|
108 | identifier() {
|
109 | return `lazy-compilation-proxy|${this.originalModule.identifier()}`;
|
110 | }
|
111 |
|
112 | |
113 |
|
114 |
|
115 |
|
116 | readableIdentifier(requestShortener) {
|
117 | return `lazy-compilation-proxy ${this.originalModule.readableIdentifier(
|
118 | requestShortener
|
119 | )}`;
|
120 | }
|
121 |
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | updateCacheModule(module) {
|
130 | super.updateCacheModule(module);
|
131 | const m = (module);
|
132 | this.originalModule = m.originalModule;
|
133 | this.request = m.request;
|
134 | this.client = m.client;
|
135 | this.data = m.data;
|
136 | this.active = m.active;
|
137 | }
|
138 |
|
139 | |
140 |
|
141 |
|
142 |
|
143 | libIdent(options) {
|
144 | return `${this.originalModule.libIdent(options)}!lazy-compilation-proxy`;
|
145 | }
|
146 |
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 | needBuild(context, callback) {
|
153 | callback(null, !this.buildInfo || this.buildInfo.active !== this.active);
|
154 | }
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | build(options, compilation, resolver, fs, callback) {
|
165 | this.buildInfo = {
|
166 | active: this.active
|
167 | };
|
168 |
|
169 | this.buildMeta = {};
|
170 | this.clearDependenciesAndBlocks();
|
171 | const dep = new CommonJsRequireDependency(this.client);
|
172 | this.addDependency(dep);
|
173 | if (this.active) {
|
174 | const dep = new LazyCompilationDependency(this);
|
175 | const block = new AsyncDependenciesBlock({});
|
176 | block.addDependency(dep);
|
177 | this.addBlock(block);
|
178 | }
|
179 | callback();
|
180 | }
|
181 |
|
182 | |
183 |
|
184 |
|
185 | getSourceTypes() {
|
186 | return TYPES;
|
187 | }
|
188 |
|
189 | |
190 |
|
191 |
|
192 |
|
193 | size(type) {
|
194 | return 200;
|
195 | }
|
196 |
|
197 | |
198 |
|
199 |
|
200 |
|
201 | codeGeneration({ runtimeTemplate, chunkGraph, moduleGraph }) {
|
202 | const sources = new Map();
|
203 | const runtimeRequirements = new Set();
|
204 | runtimeRequirements.add(RuntimeGlobals.module);
|
205 | const clientDep = (
|
206 | this.dependencies[0]
|
207 | );
|
208 | const clientModule = moduleGraph.getModule(clientDep);
|
209 | const block = this.blocks[0];
|
210 | const client = Template.asString([
|
211 | `var client = ${runtimeTemplate.moduleExports({
|
212 | module: clientModule,
|
213 | chunkGraph,
|
214 | request: clientDep.userRequest,
|
215 | runtimeRequirements
|
216 | })}`,
|
217 | `var data = ${JSON.stringify(this.data)};`
|
218 | ]);
|
219 | const keepActive = Template.asString([
|
220 | `var dispose = client.keepAlive({ data: data, active: ${JSON.stringify(
|
221 | !!block
|
222 | )}, module: module, onError: onError });`
|
223 | ]);
|
224 | let source;
|
225 | if (block) {
|
226 | const dep = block.dependencies[0];
|
227 | const module = moduleGraph.getModule(dep);
|
228 | source = Template.asString([
|
229 | client,
|
230 | `module.exports = ${runtimeTemplate.moduleNamespacePromise({
|
231 | chunkGraph,
|
232 | block,
|
233 | module,
|
234 | request: this.request,
|
235 | strict: false, // TODO this should be inherited from the original module
|
236 | message: "import()",
|
237 | runtimeRequirements
|
238 | })};`,
|
239 | "if (module.hot) {",
|
240 | Template.indent([
|
241 | "module.hot.accept();",
|
242 | `module.hot.accept(${JSON.stringify(
|
243 | chunkGraph.getModuleId(module)
|
244 | )}, function() { module.hot.invalidate(); });`,
|
245 | "module.hot.dispose(function(data) { delete data.resolveSelf; dispose(data); });",
|
246 | "if (module.hot.data && module.hot.data.resolveSelf) module.hot.data.resolveSelf(module.exports);"
|
247 | ]),
|
248 | "}",
|
249 | "function onError() { /* ignore */ }",
|
250 | keepActive
|
251 | ]);
|
252 | } else {
|
253 | source = Template.asString([
|
254 | client,
|
255 | "var resolveSelf, onError;",
|
256 | `module.exports = new Promise(function(resolve, reject) { resolveSelf = resolve; onError = reject; });`,
|
257 | "if (module.hot) {",
|
258 | Template.indent([
|
259 | "module.hot.accept();",
|
260 | "if (module.hot.data && module.hot.data.resolveSelf) module.hot.data.resolveSelf(module.exports);",
|
261 | "module.hot.dispose(function(data) { data.resolveSelf = resolveSelf; dispose(data); });"
|
262 | ]),
|
263 | "}",
|
264 | keepActive
|
265 | ]);
|
266 | }
|
267 | sources.set("javascript", new RawSource(source));
|
268 | return {
|
269 | sources,
|
270 | runtimeRequirements
|
271 | };
|
272 | }
|
273 |
|
274 | |
275 |
|
276 |
|
277 |
|
278 |
|
279 | updateHash(hash, context) {
|
280 | super.updateHash(hash, context);
|
281 | hash.update(this.active ? "active" : "");
|
282 | hash.update(JSON.stringify(this.data));
|
283 | }
|
284 | }
|
285 |
|
286 | registerNotSerializable(LazyCompilationProxyModule);
|
287 |
|
288 | class LazyCompilationDependencyFactory extends ModuleFactory {
|
289 | constructor(factory) {
|
290 | super();
|
291 | this._factory = factory;
|
292 | }
|
293 |
|
294 | |
295 |
|
296 |
|
297 |
|
298 |
|
299 | create(data, callback) {
|
300 | const dependency = (
|
301 | data.dependencies[0]
|
302 | );
|
303 | callback(null, {
|
304 | module: dependency.proxyModule.originalModule
|
305 | });
|
306 | }
|
307 | }
|
308 |
|
309 | class LazyCompilationPlugin {
|
310 | |
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | constructor({ backend, entries, imports, test }) {
|
318 | this.backend = backend;
|
319 | this.entries = entries;
|
320 | this.imports = imports;
|
321 | this.test = test;
|
322 | }
|
323 | |
324 |
|
325 |
|
326 |
|
327 |
|
328 | apply(compiler) {
|
329 | let backend;
|
330 | compiler.hooks.beforeCompile.tapAsync(
|
331 | "LazyCompilationPlugin",
|
332 | (params, callback) => {
|
333 | if (backend !== undefined) return callback();
|
334 | const promise = this.backend(compiler, (err, result) => {
|
335 | if (err) return callback(err);
|
336 | backend = result;
|
337 | callback();
|
338 | });
|
339 | if (promise && promise.then) {
|
340 | promise.then(b => {
|
341 | backend = b;
|
342 | callback();
|
343 | }, callback);
|
344 | }
|
345 | }
|
346 | );
|
347 | compiler.hooks.thisCompilation.tap(
|
348 | "LazyCompilationPlugin",
|
349 | (compilation, { normalModuleFactory }) => {
|
350 | normalModuleFactory.hooks.module.tap(
|
351 | "LazyCompilationPlugin",
|
352 | (originalModule, createData, resolveData) => {
|
353 | if (
|
354 | resolveData.dependencies.every(
|
355 | dep =>
|
356 | IGNORED_DEPENDENCY_TYPES.has(dep.type) ||
|
357 | (this.imports &&
|
358 | (dep.type === "import()" ||
|
359 | dep.type === "import() context element")) ||
|
360 | (this.entries && dep.type === "entry")
|
361 | ) &&
|
362 | !/webpack[/\\]hot[/\\]|webpack-dev-server[/\\]client/.test(
|
363 | resolveData.request
|
364 | ) &&
|
365 | checkTest(this.test, originalModule)
|
366 | ) {
|
367 | const moduleInfo = backend.module(originalModule);
|
368 | if (!moduleInfo) return;
|
369 | const { client, data, active } = moduleInfo;
|
370 |
|
371 | return new LazyCompilationProxyModule(
|
372 | compiler.context,
|
373 | originalModule,
|
374 | resolveData.request,
|
375 | client,
|
376 | data,
|
377 | active
|
378 | );
|
379 | }
|
380 | }
|
381 | );
|
382 | compilation.dependencyFactories.set(
|
383 | LazyCompilationDependency,
|
384 | new LazyCompilationDependencyFactory()
|
385 | );
|
386 | }
|
387 | );
|
388 | compiler.hooks.shutdown.tapAsync("LazyCompilationPlugin", callback => {
|
389 | backend.dispose(callback);
|
390 | });
|
391 | }
|
392 | }
|
393 |
|
394 | module.exports = LazyCompilationPlugin;
|