SystemLibraryPlugin.js 7.0 KB

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