ExportPropertyLibraryPlugin.js 3.5 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("../../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. * @template T
  22. * @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T>
  23. */
  24. /**
  25. * @typedef {object} ExportPropertyLibraryPluginParsed
  26. * @property {LibraryExport=} export
  27. */
  28. /**
  29. * @typedef {object} ExportPropertyLibraryPluginOptions
  30. * @property {LibraryType} type
  31. */
  32. /**
  33. * @typedef {ExportPropertyLibraryPluginParsed} T
  34. * @extends {AbstractLibraryPlugin<ExportPropertyLibraryPluginParsed>}
  35. */
  36. class ExportPropertyLibraryPlugin extends AbstractLibraryPlugin {
  37. /**
  38. * @param {ExportPropertyLibraryPluginOptions} options options
  39. */
  40. constructor({ type }) {
  41. super({
  42. pluginName: "ExportPropertyLibraryPlugin",
  43. type
  44. });
  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. exportsInfo.setUsedInUnknownWay(runtime);
  77. }
  78. moduleGraph.addExtraReason(module, "used as library export");
  79. }
  80. /**
  81. * @param {Chunk} chunk the chunk
  82. * @param {RuntimeRequirements} set runtime requirements
  83. * @param {LibraryContext<T>} libraryContext context
  84. * @returns {void}
  85. */
  86. runtimeRequirements(chunk, set, libraryContext) {
  87. set.add(RuntimeGlobals.exports);
  88. }
  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;