1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const enabledTypes = new WeakMap();
|
13 |
|
14 | const 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 |
|
23 | class EnableChunkLoadingPlugin {
|
24 | |
25 |
|
26 |
|
27 | constructor(type) {
|
28 | this.type = type;
|
29 | }
|
30 |
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 | static setEnabled(compiler, type) {
|
37 | getEnabledTypes(compiler).add(type);
|
38 | }
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
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 |
|
60 |
|
61 |
|
62 |
|
63 | apply(compiler) {
|
64 | const { type } = this;
|
65 |
|
66 |
|
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 |
|
85 | const CommonJsChunkLoadingPlugin = require("../node/CommonJsChunkLoadingPlugin");
|
86 | new CommonJsChunkLoadingPlugin({
|
87 | asyncChunkLoading: false
|
88 | }).apply(compiler);
|
89 | break;
|
90 | }
|
91 | case "async-node": {
|
92 |
|
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 |
|
106 | throw new Error("Universal Chunk Loading is not implemented yet");
|
107 | default:
|
108 | throw new Error(`Unsupported chunk loading type ${type}.
|
109 | Plugins which provide custom chunk loading types must call EnableChunkLoadingPlugin.setEnabled(compiler, type) to disable this error.`);
|
110 | }
|
111 | } else {
|
112 |
|
113 |
|
114 | }
|
115 | }
|
116 | }
|
117 |
|
118 | module.exports = EnableChunkLoadingPlugin;
|