ExportPropertyLibraryPlugin.js 3.8 KB

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