ExportPropertyLibraryPlugin.js 3.6 KB

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