source: imaps-frontend/node_modules/webpack/lib/library/AbstractLibraryPlugin.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 8.7 KB
RevLine 
[79a0317]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
10
11/** @typedef {import("webpack-sources").Source} Source */
12/** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
13/** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
14/** @typedef {import("../Chunk")} Chunk */
15/** @typedef {import("../ChunkGraph")} ChunkGraph */
16/** @typedef {import("../Compilation")} Compilation */
17/** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
18/** @typedef {import("../Compiler")} Compiler */
19/** @typedef {import("../Module")} Module */
20/** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
21/** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
22/** @typedef {import("../util/Hash")} Hash */
23
24const COMMON_LIBRARY_NAME_MESSAGE =
25 "Common configuration options that specific library names are 'output.library[.name]', 'entry.xyz.library[.name]', 'ModuleFederationPlugin.name' and 'ModuleFederationPlugin.library[.name]'.";
26
27/**
28 * @template T
29 * @typedef {object} LibraryContext
30 * @property {Compilation} compilation
31 * @property {ChunkGraph} chunkGraph
32 * @property {T} options
33 */
34
35/**
36 * @template T
37 */
38class AbstractLibraryPlugin {
39 /**
40 * @param {object} options options
41 * @param {string} options.pluginName name of the plugin
42 * @param {LibraryType} options.type used library type
43 */
44 constructor({ pluginName, type }) {
45 this._pluginName = pluginName;
46 this._type = type;
47 this._parseCache = new WeakMap();
48 }
49
50 /**
51 * Apply the plugin
52 * @param {Compiler} compiler the compiler instance
53 * @returns {void}
54 */
55 apply(compiler) {
56 const { _pluginName } = this;
57 compiler.hooks.thisCompilation.tap(_pluginName, compilation => {
58 compilation.hooks.finishModules.tap(
59 { name: _pluginName, stage: 10 },
60 () => {
61 for (const [
62 name,
63 {
64 dependencies: deps,
65 options: { library }
66 }
67 ] of compilation.entries) {
68 const options = this._parseOptionsCached(
69 library !== undefined
70 ? library
71 : compilation.outputOptions.library
72 );
73 if (options !== false) {
74 const dep = deps[deps.length - 1];
75 if (dep) {
76 const module = compilation.moduleGraph.getModule(dep);
77 if (module) {
78 this.finishEntryModule(module, name, {
79 options,
80 compilation,
81 chunkGraph: compilation.chunkGraph
82 });
83 }
84 }
85 }
86 }
87 }
88 );
89
90 /**
91 * @param {Chunk} chunk chunk
92 * @returns {TODO} options for the chunk
93 */
94 const getOptionsForChunk = chunk => {
95 if (compilation.chunkGraph.getNumberOfEntryModules(chunk) === 0)
96 return false;
97 const options = chunk.getEntryOptions();
98 const library = options && options.library;
99 return this._parseOptionsCached(
100 library !== undefined ? library : compilation.outputOptions.library
101 );
102 };
103
104 if (
105 this.render !== AbstractLibraryPlugin.prototype.render ||
106 this.runtimeRequirements !==
107 AbstractLibraryPlugin.prototype.runtimeRequirements
108 ) {
109 compilation.hooks.additionalChunkRuntimeRequirements.tap(
110 _pluginName,
111 (chunk, set, { chunkGraph }) => {
112 const options = getOptionsForChunk(chunk);
113 if (options !== false) {
114 this.runtimeRequirements(chunk, set, {
115 options,
116 compilation,
117 chunkGraph
118 });
119 }
120 }
121 );
122 }
123
124 const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
125
126 if (this.render !== AbstractLibraryPlugin.prototype.render) {
127 hooks.render.tap(_pluginName, (source, renderContext) => {
128 const options = getOptionsForChunk(renderContext.chunk);
129 if (options === false) return source;
130 return this.render(source, renderContext, {
131 options,
132 compilation,
133 chunkGraph: compilation.chunkGraph
134 });
135 });
136 }
137
138 if (
139 this.embedInRuntimeBailout !==
140 AbstractLibraryPlugin.prototype.embedInRuntimeBailout
141 ) {
142 hooks.embedInRuntimeBailout.tap(
143 _pluginName,
144 (module, renderContext) => {
145 const options = getOptionsForChunk(renderContext.chunk);
146 if (options === false) return;
147 return this.embedInRuntimeBailout(module, renderContext, {
148 options,
149 compilation,
150 chunkGraph: compilation.chunkGraph
151 });
152 }
153 );
154 }
155
156 if (
157 this.strictRuntimeBailout !==
158 AbstractLibraryPlugin.prototype.strictRuntimeBailout
159 ) {
160 hooks.strictRuntimeBailout.tap(_pluginName, renderContext => {
161 const options = getOptionsForChunk(renderContext.chunk);
162 if (options === false) return;
163 return this.strictRuntimeBailout(renderContext, {
164 options,
165 compilation,
166 chunkGraph: compilation.chunkGraph
167 });
168 });
169 }
170
171 if (
172 this.renderStartup !== AbstractLibraryPlugin.prototype.renderStartup
173 ) {
174 hooks.renderStartup.tap(
175 _pluginName,
176 (source, module, renderContext) => {
177 const options = getOptionsForChunk(renderContext.chunk);
178 if (options === false) return source;
179 return this.renderStartup(source, module, renderContext, {
180 options,
181 compilation,
182 chunkGraph: compilation.chunkGraph
183 });
184 }
185 );
186 }
187
188 hooks.chunkHash.tap(_pluginName, (chunk, hash, context) => {
189 const options = getOptionsForChunk(chunk);
190 if (options === false) return;
191 this.chunkHash(chunk, hash, context, {
192 options,
193 compilation,
194 chunkGraph: compilation.chunkGraph
195 });
196 });
197 });
198 }
199
200 /**
201 * @param {LibraryOptions=} library normalized library option
202 * @returns {T | false} preprocess as needed by overriding
203 */
204 _parseOptionsCached(library) {
205 if (!library) return false;
206 if (library.type !== this._type) return false;
207 const cacheEntry = this._parseCache.get(library);
208 if (cacheEntry !== undefined) return cacheEntry;
209 const result = this.parseOptions(library);
210 this._parseCache.set(library, result);
211 return result;
212 }
213
214 /* istanbul ignore next */
215 /**
216 * @abstract
217 * @param {LibraryOptions} library normalized library option
218 * @returns {T | false} preprocess as needed by overriding
219 */
220 parseOptions(library) {
221 const AbstractMethodError = require("../AbstractMethodError");
222 throw new AbstractMethodError();
223 }
224
225 /**
226 * @param {Module} module the exporting entry module
227 * @param {string} entryName the name of the entrypoint
228 * @param {LibraryContext<T>} libraryContext context
229 * @returns {void}
230 */
231 finishEntryModule(module, entryName, libraryContext) {}
232
233 /**
234 * @param {Module} module the exporting entry module
235 * @param {RenderContext} renderContext render context
236 * @param {LibraryContext<T>} libraryContext context
237 * @returns {string | undefined} bailout reason
238 */
239 embedInRuntimeBailout(module, renderContext, libraryContext) {
240 return undefined;
241 }
242
243 /**
244 * @param {RenderContext} renderContext render context
245 * @param {LibraryContext<T>} libraryContext context
246 * @returns {string | undefined} bailout reason
247 */
248 strictRuntimeBailout(renderContext, libraryContext) {
249 return undefined;
250 }
251
252 /**
253 * @param {Chunk} chunk the chunk
254 * @param {Set<string>} set runtime requirements
255 * @param {LibraryContext<T>} libraryContext context
256 * @returns {void}
257 */
258 runtimeRequirements(chunk, set, libraryContext) {
259 if (this.render !== AbstractLibraryPlugin.prototype.render)
260 set.add(RuntimeGlobals.returnExportsFromRuntime);
261 }
262
263 /**
264 * @param {Source} source source
265 * @param {RenderContext} renderContext render context
266 * @param {LibraryContext<T>} libraryContext context
267 * @returns {Source} source with library export
268 */
269 render(source, renderContext, libraryContext) {
270 return source;
271 }
272
273 /**
274 * @param {Source} source source
275 * @param {Module} module module
276 * @param {StartupRenderContext} renderContext render context
277 * @param {LibraryContext<T>} libraryContext context
278 * @returns {Source} source with library export
279 */
280 renderStartup(source, module, renderContext, libraryContext) {
281 return source;
282 }
283
284 /**
285 * @param {Chunk} chunk the chunk
286 * @param {Hash} hash hash
287 * @param {ChunkHashContext} chunkHashContext chunk hash context
288 * @param {LibraryContext<T>} libraryContext context
289 * @returns {void}
290 */
291 chunkHash(chunk, hash, chunkHashContext, libraryContext) {
292 const options = this._parseOptionsCached(
293 libraryContext.compilation.outputOptions.library
294 );
295 hash.update(this._pluginName);
296 hash.update(JSON.stringify(options));
297 }
298}
299
300AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE = COMMON_LIBRARY_NAME_MESSAGE;
301module.exports = AbstractLibraryPlugin;
Note: See TracBrowser for help on using the repository browser.