source: trip-planner-front/node_modules/webpack/lib/library/EnableLibraryPlugin.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.5 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").LibraryOptions} LibraryOptions */
9/** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
10/** @typedef {import("../Compiler")} Compiler */
11
12/** @type {WeakMap<Compiler, Set<LibraryType>>} */
13const enabledTypes = new WeakMap();
14
15const 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
24class EnableLibraryPlugin {
25 /**
26 * @param {LibraryType} type library type that should be available
27 */
28 constructor(type) {
29 this.type = type;
30 }
31
32 /**
33 * @param {Compiler} compiler the compiler instance
34 * @param {LibraryType} type type of library
35 * @returns {void}
36 */
37 static setEnabled(compiler, type) {
38 getEnabledTypes(compiler).add(type);
39 }
40
41 /**
42 * @param {Compiler} compiler the compiler instance
43 * @param {LibraryType} type type of library
44 * @returns {void}
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 * Apply the plugin
61 * @param {Compiler} compiler the compiler instance
62 * @returns {void}
63 */
64 apply(compiler) {
65 const { type } = this;
66
67 // Only enable once
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 //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
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 //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
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 //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
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 //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
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 //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
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 //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
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 //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
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 //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
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 "commonjs2":
171 case "commonjs-module": {
172 //@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
173 const AssignLibraryPlugin = require("./AssignLibraryPlugin");
174 new AssignLibraryPlugin({
175 type,
176 prefix: ["module", "exports"],
177 declare: false,
178 unnamed: "assign"
179 }).apply(compiler);
180 break;
181 }
182 case "amd":
183 case "amd-require": {
184 enableExportProperty();
185 const AmdLibraryPlugin = require("./AmdLibraryPlugin");
186 new AmdLibraryPlugin({
187 type,
188 requireAsWrapper: type === "amd-require"
189 }).apply(compiler);
190 break;
191 }
192 case "umd":
193 case "umd2": {
194 enableExportProperty();
195 const UmdLibraryPlugin = require("./UmdLibraryPlugin");
196 new UmdLibraryPlugin({
197 type,
198 optionalAmdExternalAsGlobal: type === "umd2"
199 }).apply(compiler);
200 break;
201 }
202 case "system": {
203 enableExportProperty();
204 const SystemLibraryPlugin = require("./SystemLibraryPlugin");
205 new SystemLibraryPlugin({
206 type
207 }).apply(compiler);
208 break;
209 }
210 case "jsonp": {
211 enableExportProperty();
212 const JsonpLibraryPlugin = require("./JsonpLibraryPlugin");
213 new JsonpLibraryPlugin({
214 type
215 }).apply(compiler);
216 break;
217 }
218 case "module": {
219 enableExportProperty();
220 const ModuleLibraryPlugin = require("./ModuleLibraryPlugin");
221 new ModuleLibraryPlugin({
222 type
223 }).apply(compiler);
224 break;
225 }
226 default:
227 throw new Error(`Unsupported library type ${type}.
228Plugins which provide custom library types must call EnableLibraryPlugin.setEnabled(compiler, type) to disable this error.`);
229 }
230 } else {
231 // TODO support plugin instances here
232 // apply them to the compiler
233 }
234 }
235}
236
237module.exports = EnableLibraryPlugin;
Note: See TracBrowser for help on using the repository browser.