ExportPropertyLibraryPlugin.js 4.0 KB

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