[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | /** @typedef {import("../../declarations/WebpackOptions").ChunkLoadingType} ChunkLoadingType */
|
---|
| 9 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 10 |
|
---|
| 11 | /** @type {WeakMap<Compiler, Set<ChunkLoadingType>>} */
|
---|
| 12 | const enabledTypes = new WeakMap();
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * @param {Compiler} compiler compiler
|
---|
| 16 | * @returns {Set<ChunkLoadingType>} enabled types
|
---|
| 17 | */
|
---|
| 18 | const getEnabledTypes = compiler => {
|
---|
| 19 | let set = enabledTypes.get(compiler);
|
---|
| 20 | if (set === undefined) {
|
---|
| 21 | set = new Set();
|
---|
| 22 | enabledTypes.set(compiler, set);
|
---|
| 23 | }
|
---|
| 24 | return set;
|
---|
| 25 | };
|
---|
| 26 |
|
---|
| 27 | class EnableChunkLoadingPlugin {
|
---|
| 28 | /**
|
---|
| 29 | * @param {ChunkLoadingType} type library type that should be available
|
---|
| 30 | */
|
---|
| 31 | constructor(type) {
|
---|
| 32 | this.type = type;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * @param {Compiler} compiler the compiler instance
|
---|
| 37 | * @param {ChunkLoadingType} type type of library
|
---|
| 38 | * @returns {void}
|
---|
| 39 | */
|
---|
| 40 | static setEnabled(compiler, type) {
|
---|
| 41 | getEnabledTypes(compiler).add(type);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * @param {Compiler} compiler the compiler instance
|
---|
| 46 | * @param {ChunkLoadingType} type type of library
|
---|
| 47 | * @returns {void}
|
---|
| 48 | */
|
---|
| 49 | static checkEnabled(compiler, type) {
|
---|
| 50 | if (!getEnabledTypes(compiler).has(type)) {
|
---|
| 51 | throw new Error(
|
---|
| 52 | `Chunk loading type "${type}" is not enabled. ` +
|
---|
| 53 | "EnableChunkLoadingPlugin need to be used to enable this type of chunk loading. " +
|
---|
| 54 | 'This usually happens through the "output.enabledChunkLoadingTypes" option. ' +
|
---|
| 55 | 'If you are using a function as entry which sets "chunkLoading", you need to add all potential chunk loading types to "output.enabledChunkLoadingTypes". ' +
|
---|
| 56 | `These types are enabled: ${Array.from(
|
---|
| 57 | getEnabledTypes(compiler)
|
---|
| 58 | ).join(", ")}`
|
---|
| 59 | );
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Apply the plugin
|
---|
| 65 | * @param {Compiler} compiler the compiler instance
|
---|
| 66 | * @returns {void}
|
---|
| 67 | */
|
---|
| 68 | apply(compiler) {
|
---|
| 69 | const { type } = this;
|
---|
| 70 |
|
---|
| 71 | // Only enable once
|
---|
| 72 | const enabled = getEnabledTypes(compiler);
|
---|
| 73 | if (enabled.has(type)) return;
|
---|
| 74 | enabled.add(type);
|
---|
| 75 |
|
---|
| 76 | if (typeof type === "string") {
|
---|
| 77 | switch (type) {
|
---|
| 78 | case "jsonp": {
|
---|
| 79 | const JsonpChunkLoadingPlugin = require("../web/JsonpChunkLoadingPlugin");
|
---|
| 80 | new JsonpChunkLoadingPlugin().apply(compiler);
|
---|
| 81 | break;
|
---|
| 82 | }
|
---|
| 83 | case "import-scripts": {
|
---|
| 84 | const ImportScriptsChunkLoadingPlugin = require("../webworker/ImportScriptsChunkLoadingPlugin");
|
---|
| 85 | new ImportScriptsChunkLoadingPlugin().apply(compiler);
|
---|
| 86 | break;
|
---|
| 87 | }
|
---|
| 88 | case "require": {
|
---|
| 89 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 90 | const CommonJsChunkLoadingPlugin = require("../node/CommonJsChunkLoadingPlugin");
|
---|
| 91 | new CommonJsChunkLoadingPlugin({
|
---|
| 92 | asyncChunkLoading: false
|
---|
| 93 | }).apply(compiler);
|
---|
| 94 | break;
|
---|
| 95 | }
|
---|
| 96 | case "async-node": {
|
---|
| 97 | // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
|
---|
| 98 | const CommonJsChunkLoadingPlugin = require("../node/CommonJsChunkLoadingPlugin");
|
---|
| 99 | new CommonJsChunkLoadingPlugin({
|
---|
| 100 | asyncChunkLoading: true
|
---|
| 101 | }).apply(compiler);
|
---|
| 102 | break;
|
---|
| 103 | }
|
---|
| 104 | case "import":
|
---|
| 105 | case "universal": {
|
---|
| 106 | const ModuleChunkLoadingPlugin = require("../esm/ModuleChunkLoadingPlugin");
|
---|
| 107 | new ModuleChunkLoadingPlugin().apply(compiler);
|
---|
| 108 | break;
|
---|
| 109 | }
|
---|
| 110 | default:
|
---|
| 111 | throw new Error(`Unsupported chunk loading type ${type}.
|
---|
| 112 | Plugins which provide custom chunk loading types must call EnableChunkLoadingPlugin.setEnabled(compiler, type) to disable this error.`);
|
---|
| 113 | }
|
---|
| 114 | } else {
|
---|
| 115 | // TODO support plugin instances here
|
---|
| 116 | // apply them to the compiler
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | module.exports = EnableChunkLoadingPlugin;
|
---|