1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const enabledTypes = new WeakMap();
|
14 |
|
15 | const getEnabledTypes = compiler => {
|
16 | let set = enabledTypes.get(compiler);
|
17 | if (set === undefined) {
|
18 | set = new Set();
|
19 | enabledTypes.set(compiler, set);
|
20 | }
|
21 | return set;
|
22 | };
|
23 |
|
24 | class EnableLibraryPlugin {
|
25 | |
26 |
|
27 |
|
28 | constructor(type) {
|
29 | this.type = type;
|
30 | }
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 | static setEnabled(compiler, type) {
|
38 | getEnabledTypes(compiler).add(type);
|
39 | }
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 | static checkEnabled(compiler, type) {
|
47 | if (!getEnabledTypes(compiler).has(type)) {
|
48 | throw new Error(
|
49 | `Library type "${type}" is not enabled. ` +
|
50 | "EnableLibraryPlugin need to be used to enable this type of library. " +
|
51 | 'This usually happens through the "output.enabledLibraryTypes" option. ' +
|
52 | 'If you are using a function as entry which sets "library", you need to add all potential library types to "output.enabledLibraryTypes". ' +
|
53 | "These types are enabled: " +
|
54 | Array.from(getEnabledTypes(compiler)).join(", ")
|
55 | );
|
56 | }
|
57 | }
|
58 |
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
64 | apply(compiler) {
|
65 | const { type } = this;
|
66 |
|
67 |
|
68 | const enabled = getEnabledTypes(compiler);
|
69 | if (enabled.has(type)) return;
|
70 | enabled.add(type);
|
71 |
|
72 | if (typeof type === "string") {
|
73 | const enableExportProperty = () => {
|
74 | const ExportPropertyTemplatePlugin = require("./ExportPropertyLibraryPlugin");
|
75 | new ExportPropertyTemplatePlugin({
|
76 | type,
|
77 | nsObjectUsed: type !== "module"
|
78 | }).apply(compiler);
|
79 | };
|
80 | switch (type) {
|
81 | case "var": {
|
82 |
|
83 | const AssignLibraryPlugin = require("./AssignLibraryPlugin");
|
84 | new AssignLibraryPlugin({
|
85 | type,
|
86 | prefix: [],
|
87 | declare: "var",
|
88 | unnamed: "error"
|
89 | }).apply(compiler);
|
90 | break;
|
91 | }
|
92 | case "assign-properties": {
|
93 |
|
94 | const AssignLibraryPlugin = require("./AssignLibraryPlugin");
|
95 | new AssignLibraryPlugin({
|
96 | type,
|
97 | prefix: [],
|
98 | declare: false,
|
99 | unnamed: "error",
|
100 | named: "copy"
|
101 | }).apply(compiler);
|
102 | break;
|
103 | }
|
104 | case "assign": {
|
105 |
|
106 | const AssignLibraryPlugin = require("./AssignLibraryPlugin");
|
107 | new AssignLibraryPlugin({
|
108 | type,
|
109 | prefix: [],
|
110 | declare: false,
|
111 | unnamed: "error"
|
112 | }).apply(compiler);
|
113 | break;
|
114 | }
|
115 | case "this": {
|
116 |
|
117 | const AssignLibraryPlugin = require("./AssignLibraryPlugin");
|
118 | new AssignLibraryPlugin({
|
119 | type,
|
120 | prefix: ["this"],
|
121 | declare: false,
|
122 | unnamed: "copy"
|
123 | }).apply(compiler);
|
124 | break;
|
125 | }
|
126 | case "window": {
|
127 |
|
128 | const AssignLibraryPlugin = require("./AssignLibraryPlugin");
|
129 | new AssignLibraryPlugin({
|
130 | type,
|
131 | prefix: ["window"],
|
132 | declare: false,
|
133 | unnamed: "copy"
|
134 | }).apply(compiler);
|
135 | break;
|
136 | }
|
137 | case "self": {
|
138 |
|
139 | const AssignLibraryPlugin = require("./AssignLibraryPlugin");
|
140 | new AssignLibraryPlugin({
|
141 | type,
|
142 | prefix: ["self"],
|
143 | declare: false,
|
144 | unnamed: "copy"
|
145 | }).apply(compiler);
|
146 | break;
|
147 | }
|
148 | case "global": {
|
149 |
|
150 | const AssignLibraryPlugin = require("./AssignLibraryPlugin");
|
151 | new AssignLibraryPlugin({
|
152 | type,
|
153 | prefix: "global",
|
154 | declare: false,
|
155 | unnamed: "copy"
|
156 | }).apply(compiler);
|
157 | break;
|
158 | }
|
159 | case "commonjs": {
|
160 |
|
161 | const AssignLibraryPlugin = require("./AssignLibraryPlugin");
|
162 | new AssignLibraryPlugin({
|
163 | type,
|
164 | prefix: ["exports"],
|
165 | declare: false,
|
166 | unnamed: "copy"
|
167 | }).apply(compiler);
|
168 | break;
|
169 | }
|
170 | case "commonjs-static": {
|
171 |
|
172 | const AssignLibraryPlugin = require("./AssignLibraryPlugin");
|
173 | new AssignLibraryPlugin({
|
174 | type,
|
175 | prefix: ["exports"],
|
176 | declare: false,
|
177 | unnamed: "static"
|
178 | }).apply(compiler);
|
179 | break;
|
180 | }
|
181 | case "commonjs2":
|
182 | case "commonjs-module": {
|
183 |
|
184 | const AssignLibraryPlugin = require("./AssignLibraryPlugin");
|
185 | new AssignLibraryPlugin({
|
186 | type,
|
187 | prefix: ["module", "exports"],
|
188 | declare: false,
|
189 | unnamed: "assign"
|
190 | }).apply(compiler);
|
191 | break;
|
192 | }
|
193 | case "amd":
|
194 | case "amd-require": {
|
195 | enableExportProperty();
|
196 | const AmdLibraryPlugin = require("./AmdLibraryPlugin");
|
197 | new AmdLibraryPlugin({
|
198 | type,
|
199 | requireAsWrapper: type === "amd-require"
|
200 | }).apply(compiler);
|
201 | break;
|
202 | }
|
203 | case "umd":
|
204 | case "umd2": {
|
205 | enableExportProperty();
|
206 | const UmdLibraryPlugin = require("./UmdLibraryPlugin");
|
207 | new UmdLibraryPlugin({
|
208 | type,
|
209 | optionalAmdExternalAsGlobal: type === "umd2"
|
210 | }).apply(compiler);
|
211 | break;
|
212 | }
|
213 | case "system": {
|
214 | enableExportProperty();
|
215 | const SystemLibraryPlugin = require("./SystemLibraryPlugin");
|
216 | new SystemLibraryPlugin({
|
217 | type
|
218 | }).apply(compiler);
|
219 | break;
|
220 | }
|
221 | case "jsonp": {
|
222 | enableExportProperty();
|
223 | const JsonpLibraryPlugin = require("./JsonpLibraryPlugin");
|
224 | new JsonpLibraryPlugin({
|
225 | type
|
226 | }).apply(compiler);
|
227 | break;
|
228 | }
|
229 | case "module": {
|
230 | enableExportProperty();
|
231 | const ModuleLibraryPlugin = require("./ModuleLibraryPlugin");
|
232 | new ModuleLibraryPlugin({
|
233 | type
|
234 | }).apply(compiler);
|
235 | break;
|
236 | }
|
237 | default:
|
238 | throw new Error(`Unsupported library type ${type}.
|
239 | Plugins which provide custom library types must call EnableLibraryPlugin.setEnabled(compiler, type) to disable this error.`);
|
240 | }
|
241 | } else {
|
242 |
|
243 |
|
244 | }
|
245 | }
|
246 | }
|
247 |
|
248 | module.exports = EnableLibraryPlugin;
|