ModernModuleLibraryPlugin.js 3.7 KB

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