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