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