ModuleLibraryPlugin.js 3.1 KB

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