SystemLibraryPlugin.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. const _name = /** @type {string} */ (name);
  55. return {
  56. name: _name
  57. };
  58. }
  59. /**
  60. * @param {Source} source source
  61. * @param {RenderContext} renderContext render context
  62. * @param {LibraryContext<T>} libraryContext context
  63. * @returns {Source} source with library export
  64. */
  65. render(source, { chunkGraph, moduleGraph, chunk }, { options, compilation }) {
  66. const modules = chunkGraph
  67. .getChunkModules(chunk)
  68. .filter(m => m instanceof ExternalModule && m.externalType === "system");
  69. const externals = /** @type {ExternalModule[]} */ (modules);
  70. // The name this bundle should be registered as with System
  71. const name = options.name
  72. ? `${JSON.stringify(compilation.getPath(options.name, { chunk }))}, `
  73. : "";
  74. // The array of dependencies that are external to webpack and will be provided by System
  75. const systemDependencies = JSON.stringify(
  76. externals.map(m =>
  77. typeof m.request === "object" && !Array.isArray(m.request)
  78. ? m.request.amd
  79. : m.request
  80. )
  81. );
  82. // The name of the variable provided by System for exporting
  83. const dynamicExport = "__WEBPACK_DYNAMIC_EXPORT__";
  84. // An array of the internal variable names for the webpack externals
  85. const externalWebpackNames = externals.map(
  86. m =>
  87. `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
  88. `${chunkGraph.getModuleId(m)}`
  89. )}__`
  90. );
  91. // Declaring variables for the internal variable names for the webpack externals
  92. const externalVarDeclarations = externalWebpackNames
  93. .map(name => `var ${name} = {};`)
  94. .join("\n");
  95. // Define __esModule flag on all internal variables and helpers
  96. /** @type {string[]} */
  97. const externalVarInitialization = [];
  98. // The system.register format requires an array of setter functions for externals.
  99. const setters =
  100. externalWebpackNames.length === 0
  101. ? ""
  102. : Template.asString([
  103. "setters: [",
  104. Template.indent(
  105. externals
  106. .map((module, i) => {
  107. const external = externalWebpackNames[i];
  108. const exportsInfo = moduleGraph.getExportsInfo(module);
  109. const otherUnused =
  110. exportsInfo.otherExportsInfo.getUsed(chunk.runtime) ===
  111. UsageState.Unused;
  112. const instructions = [];
  113. const handledNames = [];
  114. for (const exportInfo of exportsInfo.orderedExports) {
  115. const used = exportInfo.getUsedName(
  116. undefined,
  117. chunk.runtime
  118. );
  119. if (used) {
  120. if (otherUnused || used !== exportInfo.name) {
  121. instructions.push(
  122. `${external}${propertyAccess([
  123. used
  124. ])} = module${propertyAccess([exportInfo.name])};`
  125. );
  126. handledNames.push(exportInfo.name);
  127. }
  128. } else {
  129. handledNames.push(exportInfo.name);
  130. }
  131. }
  132. if (!otherUnused) {
  133. if (
  134. !Array.isArray(module.request) ||
  135. module.request.length === 1
  136. ) {
  137. externalVarInitialization.push(
  138. `Object.defineProperty(${external}, "__esModule", { value: true });`
  139. );
  140. }
  141. if (handledNames.length > 0) {
  142. const name = `${external}handledNames`;
  143. externalVarInitialization.push(
  144. `var ${name} = ${JSON.stringify(handledNames)};`
  145. );
  146. instructions.push(
  147. Template.asString([
  148. "Object.keys(module).forEach(function(key) {",
  149. Template.indent([
  150. `if(${name}.indexOf(key) >= 0)`,
  151. Template.indent(`${external}[key] = module[key];`)
  152. ]),
  153. "});"
  154. ])
  155. );
  156. } else {
  157. instructions.push(
  158. Template.asString([
  159. "Object.keys(module).forEach(function(key) {",
  160. Template.indent([`${external}[key] = module[key];`]),
  161. "});"
  162. ])
  163. );
  164. }
  165. }
  166. if (instructions.length === 0) return "function() {}";
  167. return Template.asString([
  168. "function(module) {",
  169. Template.indent(instructions),
  170. "}"
  171. ]);
  172. })
  173. .join(",\n")
  174. ),
  175. "],"
  176. ]);
  177. return new ConcatSource(
  178. Template.asString([
  179. `System.register(${name}${systemDependencies}, function(${dynamicExport}, __system_context__) {`,
  180. Template.indent([
  181. externalVarDeclarations,
  182. Template.asString(externalVarInitialization),
  183. "return {",
  184. Template.indent([
  185. setters,
  186. "execute: function() {",
  187. Template.indent(`${dynamicExport}(`)
  188. ])
  189. ]),
  190. ""
  191. ]),
  192. source,
  193. Template.asString([
  194. "",
  195. Template.indent([
  196. Template.indent([Template.indent([");"]), "}"]),
  197. "};"
  198. ]),
  199. "})"
  200. ])
  201. );
  202. }
  203. /**
  204. * @param {Chunk} chunk the chunk
  205. * @param {Hash} hash hash
  206. * @param {ChunkHashContext} chunkHashContext chunk hash context
  207. * @param {LibraryContext<T>} libraryContext context
  208. * @returns {void}
  209. */
  210. chunkHash(chunk, hash, chunkHashContext, { options, compilation }) {
  211. hash.update("SystemLibraryPlugin");
  212. if (options.name) {
  213. hash.update(compilation.getPath(options.name, { chunk }));
  214. }
  215. }
  216. }
  217. module.exports = SystemLibraryPlugin;