source: frontend/node_modules/webpack/lib/library/EnableLibraryPlugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

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