ModuleLibraryPlugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Template = require("../Template");
  8. const propertyAccess = require("../util/propertyAccess");
  9. const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
  10. /** @typedef {import("webpack-sources").Source} Source */
  11. /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
  12. /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
  13. /** @typedef {import("../Chunk")} Chunk */
  14. /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
  15. /** @typedef {import("../Compiler")} Compiler */
  16. /** @typedef {import("../Module")} Module */
  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} ModuleLibraryPluginOptions
  22. * @property {LibraryType} type
  23. */
  24. /**
  25. * @typedef {Object} ModuleLibraryPluginParsed
  26. * @property {string} name
  27. */
  28. /**
  29. * @typedef {ModuleLibraryPluginParsed} T
  30. * @extends {AbstractLibraryPlugin<ModuleLibraryPluginParsed>}
  31. */
  32. class ModuleLibraryPlugin extends AbstractLibraryPlugin {
  33. /**
  34. * @param {ModuleLibraryPluginOptions} options the plugin options
  35. */
  36. constructor(options) {
  37. super({
  38. pluginName: "ModuleLibraryPlugin",
  39. type: options.type
  40. });
  41. }
  42. /**
  43. * @param {LibraryOptions} library normalized library option
  44. * @returns {T | false} preprocess as needed by overriding
  45. */
  46. parseOptions(library) {
  47. const { name } = library;
  48. if (name) {
  49. throw new Error(
  50. `Library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
  51. );
  52. }
  53. return {
  54. name: /** @type {string} */ (name)
  55. };
  56. }
  57. /**
  58. * @param {Source} source source
  59. * @param {Module} module module
  60. * @param {StartupRenderContext} renderContext render context
  61. * @param {LibraryContext<T>} libraryContext context
  62. * @returns {Source} source with library export
  63. */
  64. renderStartup(
  65. source,
  66. module,
  67. { moduleGraph, chunk },
  68. { options, compilation }
  69. ) {
  70. const result = new ConcatSource(source);
  71. const exportsInfo = moduleGraph.getExportsInfo(module);
  72. const exports = [];
  73. const isAsync = moduleGraph.isAsync(module);
  74. if (isAsync) {
  75. result.add(`__webpack_exports__ = await __webpack_exports__;\n`);
  76. }
  77. for (const exportInfo of exportsInfo.orderedExports) {
  78. if (!exportInfo.provided) continue;
  79. const varName = `__webpack_exports__${Template.toIdentifier(
  80. exportInfo.name
  81. )}`;
  82. result.add(
  83. `var ${varName} = __webpack_exports__${propertyAccess([
  84. exportInfo.getUsedName(exportInfo.name, chunk.runtime)
  85. ])};\n`
  86. );
  87. exports.push(`${varName} as ${exportInfo.name}`);
  88. }
  89. if (exports.length > 0) {
  90. result.add(`export { ${exports.join(", ")} };\n`);
  91. }
  92. return result;
  93. }
  94. }
  95. module.exports = ModuleLibraryPlugin;