UNPKG

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