ExportPropertyLibraryPlugin.js 3.8 KB

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