LibManifestPlugin.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const asyncLib = require("neo-async");
  7. const EntryDependency = require("../dependencies/EntryDependency");
  8. const { someInIterable } = require("../util/IterableHelpers");
  9. const { compareModulesById } = require("../util/comparators");
  10. const { dirname, mkdirp } = require("../util/fs");
  11. /** @import { ModuleId } from "../ChunkGraph" */
  12. /** @import Compiler from "../Compiler" */
  13. /** @import { ExportInfoName } from "../Dependency" */
  14. /** @import { BuildMeta } from "../Module" */
  15. /** @import { IntermediateFileSystem } from "../util/fs" */
  16. /**
  17. * Defines the manifest module data type used by this module.
  18. * @typedef {object} ManifestModuleData
  19. * @property {ModuleId} id
  20. * @property {BuildMeta=} buildMeta
  21. * @property {ExportInfoName[]=} exports
  22. */
  23. /**
  24. * Defines the lib manifest plugin options type used by this module.
  25. * @typedef {object} LibManifestPluginOptions
  26. * @property {string=} context Context of requests in the manifest file (defaults to the webpack context).
  27. * @property {boolean=} entryOnly If true, only entry points will be exposed (default: true).
  28. * @property {boolean=} format If true, manifest json file (output) will be formatted.
  29. * @property {string=} name Name of the exposed dll function (external name, use value of 'output.library').
  30. * @property {string} path Absolute path to the manifest json file (output).
  31. * @property {string=} type Type of the dll bundle (external type, use value of 'output.libraryTarget').
  32. */
  33. const PLUGIN_NAME = "LibManifestPlugin";
  34. class LibManifestPlugin {
  35. /**
  36. * Creates an instance of LibManifestPlugin.
  37. * @param {LibManifestPluginOptions} options the options
  38. */
  39. constructor(options) {
  40. /** @type {LibManifestPluginOptions} */
  41. this.options = options;
  42. }
  43. /**
  44. * Applies the plugin by registering its hooks on the compiler.
  45. * @param {Compiler} compiler the compiler instance
  46. * @returns {void}
  47. */
  48. apply(compiler) {
  49. compiler.hooks.emit.tapAsync(
  50. { name: PLUGIN_NAME, stage: 110 },
  51. (compilation, callback) => {
  52. const moduleGraph = compilation.moduleGraph;
  53. // store used paths to detect issue and output an error. #18200
  54. /** @type {Set<string>} */
  55. const usedPaths = new Set();
  56. asyncLib.each(
  57. [...compilation.chunks],
  58. (chunk, callback) => {
  59. if (!chunk.canBeInitial()) {
  60. callback();
  61. return;
  62. }
  63. const chunkGraph = compilation.chunkGraph;
  64. const targetPath = compilation.getPath(this.options.path, {
  65. chunk
  66. });
  67. if (usedPaths.has(targetPath)) {
  68. callback(new Error("each chunk must have a unique path"));
  69. return;
  70. }
  71. usedPaths.add(targetPath);
  72. const name =
  73. this.options.name &&
  74. compilation.getPath(this.options.name, {
  75. chunk,
  76. contentHashType: "javascript"
  77. });
  78. const content = Object.create(null);
  79. for (const module of chunkGraph.getOrderedChunkModulesIterable(
  80. chunk,
  81. compareModulesById(chunkGraph)
  82. )) {
  83. if (
  84. this.options.entryOnly &&
  85. !someInIterable(
  86. moduleGraph.getIncomingConnections(module),
  87. (c) => c.dependency instanceof EntryDependency
  88. )
  89. ) {
  90. continue;
  91. }
  92. const ident = module.libIdent({
  93. context: this.options.context || compiler.context,
  94. associatedObjectForCache: compiler.root
  95. });
  96. if (ident) {
  97. const exportsInfo = moduleGraph.getExportsInfo(module);
  98. const providedExports = exportsInfo.getProvidedExports();
  99. /** @type {ManifestModuleData} */
  100. const data = {
  101. id: /** @type {ModuleId} */ (chunkGraph.getModuleId(module)),
  102. buildMeta: /** @type {BuildMeta} */ (module.buildMeta),
  103. exports: Array.isArray(providedExports)
  104. ? providedExports
  105. : undefined
  106. };
  107. content[ident] = data;
  108. }
  109. }
  110. const manifest = {
  111. name,
  112. type: this.options.type,
  113. content
  114. };
  115. // Apply formatting to content if format flag is true;
  116. const manifestContent = this.options.format
  117. ? JSON.stringify(manifest, null, 2)
  118. : JSON.stringify(manifest);
  119. const buffer = Buffer.from(manifestContent, "utf8");
  120. const intermediateFileSystem =
  121. /** @type {IntermediateFileSystem} */ (
  122. compiler.intermediateFileSystem
  123. );
  124. mkdirp(
  125. intermediateFileSystem,
  126. dirname(intermediateFileSystem, targetPath),
  127. (err) => {
  128. if (err) return callback(err);
  129. intermediateFileSystem.writeFile(targetPath, buffer, callback);
  130. }
  131. );
  132. },
  133. callback
  134. );
  135. }
  136. );
  137. }
  138. }
  139. module.exports = LibManifestPlugin;