SystemLibraryPlugin.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. /** @type {string[]} */
  96. const externalVarInitialization = [];
  97. // The system.register format requires an array of setter functions for externals.
  98. const setters =
  99. externalWebpackNames.length === 0
  100. ? ""
  101. : Template.asString([
  102. "setters: [",
  103. Template.indent(
  104. externals
  105. .map((module, i) => {
  106. const external = externalWebpackNames[i];
  107. const exportsInfo = moduleGraph.getExportsInfo(module);
  108. const otherUnused =
  109. exportsInfo.otherExportsInfo.getUsed(chunk.runtime) ===
  110. UsageState.Unused;
  111. const instructions = [];
  112. const handledNames = [];
  113. for (const exportInfo of exportsInfo.orderedExports) {
  114. const used = exportInfo.getUsedName(
  115. undefined,
  116. chunk.runtime
  117. );
  118. if (used) {
  119. if (otherUnused || used !== exportInfo.name) {
  120. instructions.push(
  121. `${external}${propertyAccess([
  122. used
  123. ])} = module${propertyAccess([exportInfo.name])};`
  124. );
  125. handledNames.push(exportInfo.name);
  126. }
  127. } else {
  128. handledNames.push(exportInfo.name);
  129. }
  130. }
  131. if (!otherUnused) {
  132. if (
  133. !Array.isArray(module.request) ||
  134. module.request.length === 1
  135. ) {
  136. externalVarInitialization.push(
  137. `Object.defineProperty(${external}, "__esModule", { value: true });`
  138. );
  139. }
  140. if (handledNames.length > 0) {
  141. const name = `${external}handledNames`;
  142. externalVarInitialization.push(
  143. `var ${name} = ${JSON.stringify(handledNames)};`
  144. );
  145. instructions.push(
  146. Template.asString([
  147. "Object.keys(module).forEach(function(key) {",
  148. Template.indent([
  149. `if(${name}.indexOf(key) >= 0)`,
  150. Template.indent(`${external}[key] = module[key];`)
  151. ]),
  152. "});"
  153. ])
  154. );
  155. } else {
  156. instructions.push(
  157. Template.asString([
  158. "Object.keys(module).forEach(function(key) {",
  159. Template.indent([`${external}[key] = module[key];`]),
  160. "});"
  161. ])
  162. );
  163. }
  164. }
  165. if (instructions.length === 0) return "function() {}";
  166. return Template.asString([
  167. "function(module) {",
  168. Template.indent(instructions),
  169. "}"
  170. ]);
  171. })
  172. .join(",\n")
  173. ),
  174. "],"
  175. ]);
  176. return new ConcatSource(
  177. Template.asString([
  178. `System.register(${name}${systemDependencies}, function(${dynamicExport}, __system_context__) {`,
  179. Template.indent([
  180. externalVarDeclarations,
  181. Template.asString(externalVarInitialization),
  182. "return {",
  183. Template.indent([
  184. setters,
  185. "execute: function() {",
  186. Template.indent(`${dynamicExport}(`)
  187. ])
  188. ]),
  189. ""
  190. ]),
  191. source,
  192. Template.asString([
  193. "",
  194. Template.indent([
  195. Template.indent([Template.indent([");"]), "}"]),
  196. "};"
  197. ]),
  198. "})"
  199. ])
  200. );
  201. }
  202. /**
  203. * @param {Chunk} chunk the chunk
  204. * @param {Hash} hash hash
  205. * @param {ChunkHashContext} chunkHashContext chunk hash context
  206. * @param {LibraryContext<T>} libraryContext context
  207. * @returns {void}
  208. */
  209. chunkHash(chunk, hash, chunkHashContext, { options, compilation }) {
  210. hash.update("SystemLibraryPlugin");
  211. if (options.name) {
  212. hash.update(compilation.getPath(options.name, { chunk }));
  213. }
  214. }
  215. }
  216. module.exports = SystemLibraryPlugin;