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