ModernModuleLibraryPlugin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 ConcatenatedModule = require("../optimize/ConcatenatedModule");
  8. const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
  9. /** @typedef {import("webpack-sources").Source} Source */
  10. /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
  11. /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
  12. /** @typedef {import("../Chunk")} Chunk */
  13. /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
  14. /** @typedef {import("../Compiler")} Compiler */
  15. /** @typedef {import("../Module")} Module */
  16. /** @typedef {import("../Module").BuildMeta} BuildMeta */
  17. /** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
  18. /** @typedef {import("../util/Hash")} Hash */
  19. /** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */
  20. /**
  21. * @typedef {object} ModernModuleLibraryPluginOptions
  22. * @property {LibraryType} type
  23. */
  24. /**
  25. * @typedef {object} ModernModuleLibraryPluginParsed
  26. * @property {string} name
  27. */
  28. /**
  29. * @typedef {ModernModuleLibraryPluginParsed} T
  30. * @extends {AbstractLibraryPlugin<ModernModuleLibraryPluginParsed>}
  31. */
  32. class ModernModuleLibraryPlugin extends AbstractLibraryPlugin {
  33. /**
  34. * Apply the plugin
  35. * @param {Compiler} compiler the compiler instance
  36. * @returns {void}
  37. */
  38. apply(compiler) {
  39. super.apply(compiler);
  40. compiler.hooks.compilation.tap("ModernModuleLibraryPlugin", compilation => {
  41. const { exportsDefinitions } =
  42. ConcatenatedModule.getCompilationHooks(compilation);
  43. exportsDefinitions.tap("ModernModuleLibraryPlugin", () => true);
  44. });
  45. }
  46. /**
  47. * @param {ModernModuleLibraryPluginOptions} options the plugin options
  48. */
  49. constructor(options) {
  50. super({
  51. pluginName: "ModernModuleLibraryPlugin",
  52. type: options.type
  53. });
  54. }
  55. /**
  56. * @param {LibraryOptions} library normalized library option
  57. * @returns {T | false} preprocess as needed by overriding
  58. */
  59. parseOptions(library) {
  60. const { name } = library;
  61. if (name) {
  62. throw new Error(
  63. `Library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
  64. );
  65. }
  66. const _name = /** @type {string} */ (name);
  67. return {
  68. name: _name
  69. };
  70. }
  71. /**
  72. * @param {Source} source source
  73. * @param {Module} module module
  74. * @param {StartupRenderContext} renderContext render context
  75. * @param {LibraryContext<T>} libraryContext context
  76. * @returns {Source} source with library export
  77. */
  78. renderStartup(
  79. source,
  80. module,
  81. { moduleGraph, chunk },
  82. { options, compilation }
  83. ) {
  84. const result = new ConcatSource(source);
  85. const exportsInfo = moduleGraph.getExportsInfo(module);
  86. const definitions =
  87. /** @type {BuildMeta} */
  88. (module.buildMeta).exportsFinalName;
  89. const exports = [];
  90. for (const exportInfo of exportsInfo.orderedExports) {
  91. let shouldContinue = false;
  92. const reexport = exportInfo.findTarget(moduleGraph, _m => true);
  93. if (reexport) {
  94. const exp = moduleGraph.getExportsInfo(reexport.module);
  95. for (const reexportInfo of exp.orderedExports) {
  96. if (
  97. !reexportInfo.provided &&
  98. reexportInfo.name === /** @type {string[]} */ (reexport.export)[0]
  99. ) {
  100. shouldContinue = true;
  101. }
  102. }
  103. }
  104. if (shouldContinue) continue;
  105. const webpackExportsProperty = exportInfo.getUsedName(
  106. exportInfo.name,
  107. chunk.runtime
  108. );
  109. const finalName =
  110. definitions[
  111. /** @type {string} */
  112. (webpackExportsProperty)
  113. ];
  114. exports.push(
  115. finalName === exportInfo.name
  116. ? finalName
  117. : `${finalName} as ${exportInfo.name}`
  118. );
  119. }
  120. if (exports.length > 0) {
  121. result.add(`export { ${exports.join(", ")} };\n`);
  122. }
  123. return result;
  124. }
  125. }
  126. module.exports = ModernModuleLibraryPlugin;