1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
9 | const JsonpChunkLoadingRuntimeModule = require("./JsonpChunkLoadingRuntimeModule");
|
---|
10 |
|
---|
11 | /** @typedef {import("../Chunk")} Chunk */
|
---|
12 | /** @typedef {import("../Compiler")} Compiler */
|
---|
13 |
|
---|
14 | class JsonpChunkLoadingPlugin {
|
---|
15 | /**
|
---|
16 | * Apply the plugin
|
---|
17 | * @param {Compiler} compiler the compiler instance
|
---|
18 | * @returns {void}
|
---|
19 | */
|
---|
20 | apply(compiler) {
|
---|
21 | compiler.hooks.thisCompilation.tap(
|
---|
22 | "JsonpChunkLoadingPlugin",
|
---|
23 | compilation => {
|
---|
24 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
---|
25 | /**
|
---|
26 | * @param {Chunk} chunk chunk
|
---|
27 | * @returns {boolean} true, if wasm loading is enabled for the chunk
|
---|
28 | */
|
---|
29 | const isEnabledForChunk = chunk => {
|
---|
30 | const options = chunk.getEntryOptions();
|
---|
31 | const chunkLoading =
|
---|
32 | options && options.chunkLoading !== undefined
|
---|
33 | ? options.chunkLoading
|
---|
34 | : globalChunkLoading;
|
---|
35 | return chunkLoading === "jsonp";
|
---|
36 | };
|
---|
37 | const onceForChunkSet = new WeakSet();
|
---|
38 | /**
|
---|
39 | * @param {Chunk} chunk chunk
|
---|
40 | * @param {Set<string>} set runtime requirements
|
---|
41 | */
|
---|
42 | const handler = (chunk, set) => {
|
---|
43 | if (onceForChunkSet.has(chunk)) return;
|
---|
44 | onceForChunkSet.add(chunk);
|
---|
45 | if (!isEnabledForChunk(chunk)) return;
|
---|
46 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
---|
47 | set.add(RuntimeGlobals.hasOwnProperty);
|
---|
48 | compilation.addRuntimeModule(
|
---|
49 | chunk,
|
---|
50 | new JsonpChunkLoadingRuntimeModule(set)
|
---|
51 | );
|
---|
52 | };
|
---|
53 | compilation.hooks.runtimeRequirementInTree
|
---|
54 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
55 | .tap("JsonpChunkLoadingPlugin", handler);
|
---|
56 | compilation.hooks.runtimeRequirementInTree
|
---|
57 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
---|
58 | .tap("JsonpChunkLoadingPlugin", handler);
|
---|
59 | compilation.hooks.runtimeRequirementInTree
|
---|
60 | .for(RuntimeGlobals.hmrDownloadManifest)
|
---|
61 | .tap("JsonpChunkLoadingPlugin", handler);
|
---|
62 | compilation.hooks.runtimeRequirementInTree
|
---|
63 | .for(RuntimeGlobals.baseURI)
|
---|
64 | .tap("JsonpChunkLoadingPlugin", handler);
|
---|
65 | compilation.hooks.runtimeRequirementInTree
|
---|
66 | .for(RuntimeGlobals.onChunksLoaded)
|
---|
67 | .tap("JsonpChunkLoadingPlugin", handler);
|
---|
68 |
|
---|
69 | compilation.hooks.runtimeRequirementInTree
|
---|
70 | .for(RuntimeGlobals.ensureChunkHandlers)
|
---|
71 | .tap("JsonpChunkLoadingPlugin", (chunk, set) => {
|
---|
72 | if (!isEnabledForChunk(chunk)) return;
|
---|
73 | set.add(RuntimeGlobals.publicPath);
|
---|
74 | set.add(RuntimeGlobals.loadScript);
|
---|
75 | set.add(RuntimeGlobals.getChunkScriptFilename);
|
---|
76 | });
|
---|
77 | compilation.hooks.runtimeRequirementInTree
|
---|
78 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
---|
79 | .tap("JsonpChunkLoadingPlugin", (chunk, set) => {
|
---|
80 | if (!isEnabledForChunk(chunk)) return;
|
---|
81 | set.add(RuntimeGlobals.publicPath);
|
---|
82 | set.add(RuntimeGlobals.loadScript);
|
---|
83 | set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
|
---|
84 | set.add(RuntimeGlobals.moduleCache);
|
---|
85 | set.add(RuntimeGlobals.hmrModuleData);
|
---|
86 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
---|
87 | });
|
---|
88 | compilation.hooks.runtimeRequirementInTree
|
---|
89 | .for(RuntimeGlobals.hmrDownloadManifest)
|
---|
90 | .tap("JsonpChunkLoadingPlugin", (chunk, set) => {
|
---|
91 | if (!isEnabledForChunk(chunk)) return;
|
---|
92 | set.add(RuntimeGlobals.publicPath);
|
---|
93 | set.add(RuntimeGlobals.getUpdateManifestFilename);
|
---|
94 | });
|
---|
95 | }
|
---|
96 | );
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | module.exports = JsonpChunkLoadingPlugin;
|
---|