| [9af201e] | 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 | /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
|
|---|
| 14 |
|
|---|
| 15 | const PLUGIN_NAME = "JsonpChunkLoadingPlugin";
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Enables browser-side JavaScript chunk loading through the JSONP runtime and
|
|---|
| 19 | * adds the supporting runtime requirements for matching chunks.
|
|---|
| 20 | */
|
|---|
| 21 | class JsonpChunkLoadingPlugin {
|
|---|
| 22 | /**
|
|---|
| 23 | * Registers compilation hooks that attach the JSONP chunk-loading runtime
|
|---|
| 24 | * module and its dependent runtime globals to chunks using `chunkLoading:
|
|---|
| 25 | * "jsonp"`.
|
|---|
| 26 | * @param {Compiler} compiler the compiler instance
|
|---|
| 27 | * @returns {void}
|
|---|
| 28 | */
|
|---|
| 29 | apply(compiler) {
|
|---|
| 30 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|---|
| 31 | const globalChunkLoading = compilation.outputOptions.chunkLoading;
|
|---|
| 32 | /**
|
|---|
| 33 | * Determines whether the chunk resolves JavaScript chunks through the
|
|---|
| 34 | * JSONP loading backend.
|
|---|
| 35 | * @param {Chunk} chunk chunk
|
|---|
| 36 | * @returns {boolean} true, if wasm loading is enabled for the chunk
|
|---|
| 37 | */
|
|---|
| 38 | const isEnabledForChunk = (chunk) => {
|
|---|
| 39 | const options = chunk.getEntryOptions();
|
|---|
| 40 | const chunkLoading =
|
|---|
| 41 | options && options.chunkLoading !== undefined
|
|---|
| 42 | ? options.chunkLoading
|
|---|
| 43 | : globalChunkLoading;
|
|---|
| 44 | return chunkLoading === "jsonp";
|
|---|
| 45 | };
|
|---|
| 46 | /** @type {WeakSet<Chunk>} */
|
|---|
| 47 | const onceForChunkSet = new WeakSet();
|
|---|
| 48 | /**
|
|---|
| 49 | * Adds the JSONP runtime module to a chunk once, along with the core
|
|---|
| 50 | * runtime globals it relies on.
|
|---|
| 51 | * @param {Chunk} chunk chunk
|
|---|
| 52 | * @param {RuntimeRequirements} set runtime requirements
|
|---|
| 53 | */
|
|---|
| 54 | const handler = (chunk, set) => {
|
|---|
| 55 | if (onceForChunkSet.has(chunk)) return;
|
|---|
| 56 | onceForChunkSet.add(chunk);
|
|---|
| 57 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 58 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
|---|
| 59 | set.add(RuntimeGlobals.hasOwnProperty);
|
|---|
| 60 | compilation.addRuntimeModule(
|
|---|
| 61 | chunk,
|
|---|
| 62 | new JsonpChunkLoadingRuntimeModule(set)
|
|---|
| 63 | );
|
|---|
| 64 | };
|
|---|
| 65 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 66 | .for(RuntimeGlobals.ensureChunkHandlers)
|
|---|
| 67 | .tap(PLUGIN_NAME, handler);
|
|---|
| 68 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 69 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
|---|
| 70 | .tap(PLUGIN_NAME, handler);
|
|---|
| 71 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 72 | .for(RuntimeGlobals.hmrDownloadManifest)
|
|---|
| 73 | .tap(PLUGIN_NAME, handler);
|
|---|
| 74 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 75 | .for(RuntimeGlobals.baseURI)
|
|---|
| 76 | .tap(PLUGIN_NAME, handler);
|
|---|
| 77 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 78 | .for(RuntimeGlobals.onChunksLoaded)
|
|---|
| 79 | .tap(PLUGIN_NAME, handler);
|
|---|
| 80 |
|
|---|
| 81 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 82 | .for(RuntimeGlobals.ensureChunkHandlers)
|
|---|
| 83 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 84 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 85 | set.add(RuntimeGlobals.publicPath);
|
|---|
| 86 | set.add(RuntimeGlobals.loadScript);
|
|---|
| 87 | set.add(RuntimeGlobals.getChunkScriptFilename);
|
|---|
| 88 | });
|
|---|
| 89 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 90 | .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
|
|---|
| 91 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 92 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 93 | set.add(RuntimeGlobals.publicPath);
|
|---|
| 94 | set.add(RuntimeGlobals.loadScript);
|
|---|
| 95 | set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
|
|---|
| 96 | set.add(RuntimeGlobals.moduleCache);
|
|---|
| 97 | set.add(RuntimeGlobals.hmrModuleData);
|
|---|
| 98 | set.add(RuntimeGlobals.moduleFactoriesAddOnly);
|
|---|
| 99 | });
|
|---|
| 100 | compilation.hooks.runtimeRequirementInTree
|
|---|
| 101 | .for(RuntimeGlobals.hmrDownloadManifest)
|
|---|
| 102 | .tap(PLUGIN_NAME, (chunk, set) => {
|
|---|
| 103 | if (!isEnabledForChunk(chunk)) return;
|
|---|
| 104 | set.add(RuntimeGlobals.publicPath);
|
|---|
| 105 | set.add(RuntimeGlobals.getUpdateManifestFilename);
|
|---|
| 106 | });
|
|---|
| 107 | });
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | module.exports = JsonpChunkLoadingPlugin;
|
|---|