ModuleLibraryPlugin.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 (!chunk.hasRuntime()) {
  74. return false;
  75. }
  76. }
  77. return true;
  78. });
  79. });
  80. }
  81. /**
  82. * @param {LibraryOptions} library normalized library option
  83. * @returns {T | false} preprocess as needed by overriding
  84. */
  85. parseOptions(library) {
  86. const { name } = library;
  87. if (name) {
  88. throw new Error(
  89. `Library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
  90. );
  91. }
  92. const _name = /** @type {string} */ (name);
  93. return {
  94. name: _name,
  95. export: library.export
  96. };
  97. }
  98. /**
  99. * @param {Source} source source
  100. * @param {Module} module module
  101. * @param {StartupRenderContext} renderContext render context
  102. * @param {LibraryContext<T>} libraryContext context
  103. * @returns {Source} source with library export
  104. */
  105. renderStartup(
  106. source,
  107. module,
  108. { moduleGraph, chunk, codeGenerationResults },
  109. { options, compilation }
  110. ) {
  111. const result = new ConcatSource(source);
  112. const exportsInfo = options.export
  113. ? [
  114. moduleGraph.getExportInfo(
  115. module,
  116. Array.isArray(options.export) ? options.export[0] : options.export
  117. )
  118. ]
  119. : moduleGraph.getExportsInfo(module).orderedExports;
  120. const definitions =
  121. /** @type {BuildMeta} */
  122. (module.buildMeta).exportsFinalName || {};
  123. /** @type {string[]} */
  124. const shortHandedExports = [];
  125. /** @type {[string, string][]} */
  126. const exports = [];
  127. const isAsync = moduleGraph.isAsync(module);
  128. if (isAsync) {
  129. result.add(
  130. `${RuntimeGlobals.exports} = await ${RuntimeGlobals.exports};\n`
  131. );
  132. }
  133. const varType = compilation.outputOptions.environment.const
  134. ? "const"
  135. : "var";
  136. for (const exportInfo of exportsInfo) {
  137. if (!exportInfo.provided) continue;
  138. let shouldContinue = false;
  139. const reexport = exportInfo.findTarget(moduleGraph, _m => true);
  140. if (reexport) {
  141. const exp = moduleGraph.getExportsInfo(reexport.module);
  142. for (const reexportInfo of exp.orderedExports) {
  143. if (
  144. reexportInfo.provided === false &&
  145. reexportInfo.name !== "default" &&
  146. reexportInfo.name === /** @type {string[]} */ (reexport.export)[0]
  147. ) {
  148. shouldContinue = true;
  149. }
  150. }
  151. }
  152. if (shouldContinue) continue;
  153. const originalName = exportInfo.name;
  154. const usedName =
  155. /** @type {string} */
  156. (exportInfo.getUsedName(originalName, chunk.runtime));
  157. /** @type {string | undefined} */
  158. const definition = definitions[usedName];
  159. const finalName =
  160. definition ||
  161. `${RuntimeGlobals.exports}${Template.toIdentifier(originalName)}`;
  162. if (!definition) {
  163. result.add(
  164. `${varType} ${finalName} = ${RuntimeGlobals.exports}${propertyAccess([
  165. usedName
  166. ])};\n`
  167. );
  168. }
  169. if (
  170. finalName &&
  171. (finalName.includes(".") ||
  172. finalName.includes("[") ||
  173. finalName.includes("("))
  174. ) {
  175. if (exportInfo.isReexport()) {
  176. const { data } = codeGenerationResults.get(module, chunk.runtime);
  177. const topLevelDeclarations =
  178. (data && data.get("topLevelDeclarations")) ||
  179. (module.buildInfo && module.buildInfo.topLevelDeclarations);
  180. if (topLevelDeclarations && topLevelDeclarations.has(originalName)) {
  181. const name = `${RuntimeGlobals.exports}${Template.toIdentifier(originalName)}`;
  182. result.add(`${varType} ${name} = ${finalName};\n`);
  183. shortHandedExports.push(`${name} as ${originalName}`);
  184. } else {
  185. exports.push([originalName, finalName]);
  186. }
  187. } else {
  188. exports.push([originalName, finalName]);
  189. }
  190. } else {
  191. shortHandedExports.push(
  192. definition && finalName === originalName
  193. ? finalName
  194. : `${finalName} as ${originalName}`
  195. );
  196. }
  197. }
  198. if (shortHandedExports.length > 0) {
  199. result.add(`export { ${shortHandedExports.join(", ")} };\n`);
  200. }
  201. for (const [exportName, final] of exports) {
  202. result.add(`export ${varType} ${exportName} = ${final};\n`);
  203. }
  204. return result;
  205. }
  206. }
  207. module.exports = ModuleLibraryPlugin;