ModuleLibraryPlugin.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. const _name = /** @type {string} */ (name);
  55. return {
  56. name: _name
  57. };
  58. }
  59. /**
  60. * @param {Source} source source
  61. * @param {Module} module module
  62. * @param {StartupRenderContext} renderContext render context
  63. * @param {LibraryContext<T>} libraryContext context
  64. * @returns {Source} source with library export
  65. */
  66. renderStartup(
  67. source,
  68. module,
  69. { moduleGraph, chunk },
  70. { options, compilation }
  71. ) {
  72. const result = new ConcatSource(source);
  73. const exportsInfo = moduleGraph.getExportsInfo(module);
  74. const exports = [];
  75. const isAsync = moduleGraph.isAsync(module);
  76. if (isAsync) {
  77. result.add(
  78. `${RuntimeGlobals.exports} = await ${RuntimeGlobals.exports};\n`
  79. );
  80. }
  81. for (const exportInfo of exportsInfo.orderedExports) {
  82. if (!exportInfo.provided) continue;
  83. const varName = `${RuntimeGlobals.exports}${Template.toIdentifier(
  84. exportInfo.name
  85. )}`;
  86. result.add(
  87. `var ${varName} = ${RuntimeGlobals.exports}${propertyAccess([
  88. /** @type {string} */
  89. (exportInfo.getUsedName(exportInfo.name, chunk.runtime))
  90. ])};\n`
  91. );
  92. exports.push(`${varName} as ${exportInfo.name}`);
  93. }
  94. if (exports.length > 0) {
  95. result.add(`export { ${exports.join(", ")} };\n`);
  96. }
  97. return result;
  98. }
  99. }
  100. module.exports = ModuleLibraryPlugin;