ModuleLibraryPlugin.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { ConcatSource } = require("webpack-sources");
  7. const RuntimeGlobals = require("../RuntimeGlobals");
  8. const Template = require("../Template");
  9. const ConcatenatedModule = require("../optimize/ConcatenatedModule");
  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("../Module")} Module */
  19. /** @typedef {import("../Module").BuildMeta} BuildMeta */
  20. /** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
  21. /** @typedef {import("../util/Hash")} Hash */
  22. /**
  23. * @template T
  24. * @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T>
  25. */
  26. /**
  27. * @typedef {object} ModuleLibraryPluginOptions
  28. * @property {LibraryType} type
  29. */
  30. /**
  31. * @typedef {object} ModuleLibraryPluginParsed
  32. * @property {string} name
  33. * @property {string | string[]=} export
  34. */
  35. const PLUGIN_NAME = "ModuleLibraryPlugin";
  36. /**
  37. * @typedef {ModuleLibraryPluginParsed} T
  38. * @extends {AbstractLibraryPlugin<ModuleLibraryPluginParsed>}
  39. */
  40. class ModuleLibraryPlugin extends AbstractLibraryPlugin {
  41. /**
  42. * @param {ModuleLibraryPluginOptions} options the plugin options
  43. */
  44. constructor(options) {
  45. super({
  46. pluginName: "ModuleLibraryPlugin",
  47. type: options.type
  48. });
  49. }
  50. /**
  51. * Apply the plugin
  52. * @param {Compiler} compiler the compiler instance
  53. * @returns {void}
  54. */
  55. apply(compiler) {
  56. super.apply(compiler);
  57. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
  58. const { exportsDefinitions } =
  59. ConcatenatedModule.getCompilationHooks(compilation);
  60. exportsDefinitions.tap(PLUGIN_NAME, (definitions, module) => {
  61. // If we have connections not all modules were concatenated, so we need the wrapper
  62. const connections =
  63. compilation.moduleGraph.getIncomingConnections(module);
  64. for (const connection of connections) {
  65. if (connection.originModule) {
  66. return false;
  67. }
  68. }
  69. // Runtime and splitting chunks now requires the wrapper too
  70. for (const chunk of compilation.chunkGraph.getModuleChunksIterable(
  71. module
  72. )) {
  73. if (
  74. !chunk.hasRuntime() ||
  75. compilation.chunkGraph.getNumberOfEntryModules(chunk) > 1
  76. ) {
  77. return false;
  78. }
  79. }
  80. return true;
  81. });
  82. });
  83. }
  84. /**
  85. * @param {LibraryOptions} library normalized library option
  86. * @returns {T | false} preprocess as needed by overriding
  87. */
  88. parseOptions(library) {
  89. const { name } = library;
  90. if (name) {
  91. throw new Error(
  92. `Library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
  93. );
  94. }
  95. const _name = /** @type {string} */ (name);
  96. return {
  97. name: _name,
  98. export: library.export
  99. };
  100. }
  101. /**
  102. * @param {Source} source source
  103. * @param {Module} module module
  104. * @param {StartupRenderContext} renderContext render context
  105. * @param {LibraryContext<T>} libraryContext context
  106. * @returns {Source} source with library export
  107. */
  108. renderStartup(
  109. source,
  110. module,
  111. { moduleGraph, chunk, codeGenerationResults },
  112. { options, compilation }
  113. ) {
  114. const result = new ConcatSource(source);
  115. const exportsInfo = options.export
  116. ? [
  117. moduleGraph.getExportInfo(
  118. module,
  119. Array.isArray(options.export) ? options.export[0] : options.export
  120. )
  121. ]
  122. : moduleGraph.getExportsInfo(module).orderedExports;
  123. const definitions =
  124. /** @type {BuildMeta} */
  125. (module.buildMeta).exportsFinalName || {};
  126. /** @type {string[]} */
  127. const shortHandedExports = [];
  128. /** @type {[string, string][]} */
  129. const exports = [];
  130. const isAsync = moduleGraph.isAsync(module);
  131. if (isAsync) {
  132. result.add(
  133. `${RuntimeGlobals.exports} = await ${RuntimeGlobals.exports};\n`
  134. );
  135. }
  136. const varType = compilation.outputOptions.environment.const
  137. ? "const"
  138. : "var";
  139. for (const exportInfo of exportsInfo) {
  140. if (!exportInfo.provided) continue;
  141. let shouldContinue = false;
  142. const reexport = exportInfo.findTarget(moduleGraph, _m => true);
  143. if (reexport) {
  144. const exp = moduleGraph.getExportsInfo(reexport.module);
  145. for (const reexportInfo of exp.orderedExports) {
  146. if (
  147. reexportInfo.provided === false &&
  148. reexportInfo.name !== "default" &&
  149. reexportInfo.name === /** @type {string[]} */ (reexport.export)[0]
  150. ) {
  151. shouldContinue = true;
  152. }
  153. }
  154. }
  155. if (shouldContinue) continue;
  156. const originalName = exportInfo.name;
  157. const usedName =
  158. /** @type {string} */
  159. (exportInfo.getUsedName(originalName, chunk.runtime));
  160. /** @type {string | undefined} */
  161. const definition = definitions[usedName];
  162. const finalName =
  163. definition ||
  164. `${RuntimeGlobals.exports}${Template.toIdentifier(originalName)}`;
  165. if (!definition) {
  166. result.add(
  167. `${varType} ${finalName} = ${RuntimeGlobals.exports}${propertyAccess([
  168. usedName
  169. ])};\n`
  170. );
  171. }
  172. if (
  173. finalName &&
  174. (finalName.includes(".") ||
  175. finalName.includes("[") ||
  176. finalName.includes("("))
  177. ) {
  178. if (exportInfo.isReexport()) {
  179. const { data } = codeGenerationResults.get(module, chunk.runtime);
  180. const topLevelDeclarations =
  181. (data && data.get("topLevelDeclarations")) ||
  182. (module.buildInfo && module.buildInfo.topLevelDeclarations);
  183. if (topLevelDeclarations && topLevelDeclarations.has(originalName)) {
  184. const name = `${RuntimeGlobals.exports}${Template.toIdentifier(originalName)}`;
  185. result.add(`${varType} ${name} = ${finalName};\n`);
  186. shortHandedExports.push(`${name} as ${originalName}`);
  187. } else {
  188. exports.push([originalName, finalName]);
  189. }
  190. } else {
  191. exports.push([originalName, finalName]);
  192. }
  193. } else {
  194. shortHandedExports.push(
  195. definition && finalName === originalName
  196. ? finalName
  197. : `${finalName} as ${originalName}`
  198. );
  199. }
  200. }
  201. if (shortHandedExports.length > 0) {
  202. result.add(`export { ${shortHandedExports.join(", ")} };\n`);
  203. }
  204. for (const [exportName, final] of exports) {
  205. result.add(`export ${varType} ${exportName} = ${final};\n`);
  206. }
  207. return result;
  208. }
  209. }
  210. module.exports = ModuleLibraryPlugin;