source: imaps-frontend/node_modules/webpack/lib/library/SystemLibraryPlugin.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: 7.0 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Joel Denning @joeldenning
4*/
5
6"use strict";
7
8const { ConcatSource } = require("webpack-sources");
9const { UsageState } = require("../ExportsInfo");
10const ExternalModule = require("../ExternalModule");
11const Template = require("../Template");
12const propertyAccess = require("../util/propertyAccess");
13const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
14
15/** @typedef {import("webpack-sources").Source} Source */
16/** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
17/** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
18/** @typedef {import("../Chunk")} Chunk */
19/** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
20/** @typedef {import("../Compiler")} Compiler */
21/** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
22/** @typedef {import("../util/Hash")} Hash */
23/** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */
24
25/**
26 * @typedef {object} SystemLibraryPluginOptions
27 * @property {LibraryType} type
28 */
29
30/**
31 * @typedef {object} SystemLibraryPluginParsed
32 * @property {string} name
33 */
34
35/**
36 * @typedef {SystemLibraryPluginParsed} T
37 * @extends {AbstractLibraryPlugin<SystemLibraryPluginParsed>}
38 */
39class SystemLibraryPlugin extends AbstractLibraryPlugin {
40 /**
41 * @param {SystemLibraryPluginOptions} options the plugin options
42 */
43 constructor(options) {
44 super({
45 pluginName: "SystemLibraryPlugin",
46 type: options.type
47 });
48 }
49
50 /**
51 * @param {LibraryOptions} library normalized library option
52 * @returns {T | false} preprocess as needed by overriding
53 */
54 parseOptions(library) {
55 const { name } = library;
56 if (name && typeof name !== "string") {
57 throw new Error(
58 `System.js library name must be a simple string or unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
59 );
60 }
61 const _name = /** @type {string} */ (name);
62 return {
63 name: _name
64 };
65 }
66
67 /**
68 * @param {Source} source source
69 * @param {RenderContext} renderContext render context
70 * @param {LibraryContext<T>} libraryContext context
71 * @returns {Source} source with library export
72 */
73 render(source, { chunkGraph, moduleGraph, chunk }, { options, compilation }) {
74 const modules = chunkGraph
75 .getChunkModules(chunk)
76 .filter(m => m instanceof ExternalModule && m.externalType === "system");
77 const externals = /** @type {ExternalModule[]} */ (modules);
78
79 // The name this bundle should be registered as with System
80 const name = options.name
81 ? `${JSON.stringify(compilation.getPath(options.name, { chunk }))}, `
82 : "";
83
84 // The array of dependencies that are external to webpack and will be provided by System
85 const systemDependencies = JSON.stringify(
86 externals.map(m =>
87 typeof m.request === "object" && !Array.isArray(m.request)
88 ? m.request.amd
89 : m.request
90 )
91 );
92
93 // The name of the variable provided by System for exporting
94 const dynamicExport = "__WEBPACK_DYNAMIC_EXPORT__";
95
96 // An array of the internal variable names for the webpack externals
97 const externalWebpackNames = externals.map(
98 m =>
99 `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
100 `${chunkGraph.getModuleId(m)}`
101 )}__`
102 );
103
104 // Declaring variables for the internal variable names for the webpack externals
105 const externalVarDeclarations = externalWebpackNames
106 .map(name => `var ${name} = {};`)
107 .join("\n");
108
109 // Define __esModule flag on all internal variables and helpers
110 /** @type {string[]} */
111 const externalVarInitialization = [];
112
113 // The system.register format requires an array of setter functions for externals.
114 const setters =
115 externalWebpackNames.length === 0
116 ? ""
117 : Template.asString([
118 "setters: [",
119 Template.indent(
120 externals
121 .map((module, i) => {
122 const external = externalWebpackNames[i];
123 const exportsInfo = moduleGraph.getExportsInfo(module);
124 const otherUnused =
125 exportsInfo.otherExportsInfo.getUsed(chunk.runtime) ===
126 UsageState.Unused;
127 const instructions = [];
128 const handledNames = [];
129 for (const exportInfo of exportsInfo.orderedExports) {
130 const used = exportInfo.getUsedName(
131 undefined,
132 chunk.runtime
133 );
134 if (used) {
135 if (otherUnused || used !== exportInfo.name) {
136 instructions.push(
137 `${external}${propertyAccess([
138 used
139 ])} = module${propertyAccess([exportInfo.name])};`
140 );
141 handledNames.push(exportInfo.name);
142 }
143 } else {
144 handledNames.push(exportInfo.name);
145 }
146 }
147 if (!otherUnused) {
148 if (
149 !Array.isArray(module.request) ||
150 module.request.length === 1
151 ) {
152 externalVarInitialization.push(
153 `Object.defineProperty(${external}, "__esModule", { value: true });`
154 );
155 }
156 if (handledNames.length > 0) {
157 const name = `${external}handledNames`;
158 externalVarInitialization.push(
159 `var ${name} = ${JSON.stringify(handledNames)};`
160 );
161 instructions.push(
162 Template.asString([
163 "Object.keys(module).forEach(function(key) {",
164 Template.indent([
165 `if(${name}.indexOf(key) >= 0)`,
166 Template.indent(`${external}[key] = module[key];`)
167 ]),
168 "});"
169 ])
170 );
171 } else {
172 instructions.push(
173 Template.asString([
174 "Object.keys(module).forEach(function(key) {",
175 Template.indent([`${external}[key] = module[key];`]),
176 "});"
177 ])
178 );
179 }
180 }
181 if (instructions.length === 0) return "function() {}";
182 return Template.asString([
183 "function(module) {",
184 Template.indent(instructions),
185 "}"
186 ]);
187 })
188 .join(",\n")
189 ),
190 "],"
191 ]);
192
193 return new ConcatSource(
194 Template.asString([
195 `System.register(${name}${systemDependencies}, function(${dynamicExport}, __system_context__) {`,
196 Template.indent([
197 externalVarDeclarations,
198 Template.asString(externalVarInitialization),
199 "return {",
200 Template.indent([
201 setters,
202 "execute: function() {",
203 Template.indent(`${dynamicExport}(`)
204 ])
205 ]),
206 ""
207 ]),
208 source,
209 Template.asString([
210 "",
211 Template.indent([
212 Template.indent([Template.indent([");"]), "}"]),
213 "};"
214 ]),
215 "})"
216 ])
217 );
218 }
219
220 /**
221 * @param {Chunk} chunk the chunk
222 * @param {Hash} hash hash
223 * @param {ChunkHashContext} chunkHashContext chunk hash context
224 * @param {LibraryContext<T>} libraryContext context
225 * @returns {void}
226 */
227 chunkHash(chunk, hash, chunkHashContext, { options, compilation }) {
228 hash.update("SystemLibraryPlugin");
229 if (options.name) {
230 hash.update(compilation.getPath(options.name, { chunk }));
231 }
232 }
233}
234
235module.exports = SystemLibraryPlugin;
Note: See TracBrowser for help on using the repository browser.