ExportPropertyLibraryPlugin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 { UsageState } = require("../ExportsInfo");
  8. const propertyAccess = require("../util/propertyAccess");
  9. const { getEntryRuntime } = require("../util/runtime");
  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("../Compiler")} Compiler */
  16. /** @typedef {import("../Module")} Module */
  17. /** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
  18. /** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */
  19. /**
  20. * @typedef {Object} ExportPropertyLibraryPluginParsed
  21. * @property {string | string[]} export
  22. */
  23. /**
  24. * @typedef {Object} ExportPropertyLibraryPluginOptions
  25. * @property {LibraryType} type
  26. * @property {boolean} nsObjectUsed the namespace object is used
  27. */
  28. /**
  29. * @typedef {ExportPropertyLibraryPluginParsed} T
  30. * @extends {AbstractLibraryPlugin<ExportPropertyLibraryPluginParsed>}
  31. */
  32. class ExportPropertyLibraryPlugin extends AbstractLibraryPlugin {
  33. /**
  34. * @param {ExportPropertyLibraryPluginOptions} options options
  35. */
  36. constructor({ type, nsObjectUsed }) {
  37. super({
  38. pluginName: "ExportPropertyLibraryPlugin",
  39. type
  40. });
  41. this.nsObjectUsed = nsObjectUsed;
  42. }
  43. /**
  44. * @param {LibraryOptions} library normalized library option
  45. * @returns {T | false} preprocess as needed by overriding
  46. */
  47. parseOptions(library) {
  48. return {
  49. export: library.export
  50. };
  51. }
  52. /**
  53. * @param {Module} module the exporting entry module
  54. * @param {string} entryName the name of the entrypoint
  55. * @param {LibraryContext<T>} libraryContext context
  56. * @returns {void}
  57. */
  58. finishEntryModule(
  59. module,
  60. entryName,
  61. { options, compilation, compilation: { moduleGraph } }
  62. ) {
  63. const runtime = getEntryRuntime(compilation, entryName);
  64. if (options.export) {
  65. const exportsInfo = moduleGraph.getExportInfo(
  66. module,
  67. Array.isArray(options.export) ? options.export[0] : options.export
  68. );
  69. exportsInfo.setUsed(UsageState.Used, runtime);
  70. exportsInfo.canMangleUse = false;
  71. } else {
  72. const exportsInfo = moduleGraph.getExportsInfo(module);
  73. if (this.nsObjectUsed) {
  74. exportsInfo.setUsedInUnknownWay(runtime);
  75. } else {
  76. exportsInfo.setAllKnownExportsUsed(runtime);
  77. }
  78. }
  79. moduleGraph.addExtraReason(module, "used as library export");
  80. }
  81. /**
  82. * @param {Chunk} chunk the chunk
  83. * @param {Set<string>} set runtime requirements
  84. * @param {LibraryContext<T>} libraryContext context
  85. * @returns {void}
  86. */
  87. runtimeRequirements(chunk, set, libraryContext) {}
  88. /**
  89. * @param {Source} source source
  90. * @param {Module} module module
  91. * @param {StartupRenderContext} renderContext render context
  92. * @param {LibraryContext<T>} libraryContext context
  93. * @returns {Source} source with library export
  94. */
  95. renderStartup(source, module, renderContext, { options }) {
  96. if (!options.export) return source;
  97. const postfix = `__webpack_exports__ = __webpack_exports__${propertyAccess(
  98. Array.isArray(options.export) ? options.export : [options.export]
  99. )};\n`;
  100. return new ConcatSource(source, postfix);
  101. }
  102. }
  103. module.exports = ExportPropertyLibraryPlugin;