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