LibManifestPlugin.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
  12. /** @typedef {import("./Compiler")} Compiler */
  13. /** @typedef {import("./Compiler").IntermediateFileSystem} IntermediateFileSystem */
  14. /** @typedef {import("./Module").BuildMeta} BuildMeta */
  15. /** @typedef {import("./ExportsInfo").ExportInfoName} ExportInfoName */
  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. this.options = options;
  41. }
  42. /**
  43. * Applies the plugin by registering its hooks on the compiler.
  44. * @param {Compiler} compiler the compiler instance
  45. * @returns {void}
  46. */
  47. apply(compiler) {
  48. compiler.hooks.emit.tapAsync(
  49. { name: PLUGIN_NAME, stage: 110 },
  50. (compilation, callback) => {
  51. const moduleGraph = compilation.moduleGraph;
  52. // store used paths to detect issue and output an error. #18200
  53. /** @type {Set<string>} */
  54. const usedPaths = new Set();
  55. asyncLib.each(
  56. [...compilation.chunks],
  57. (chunk, callback) => {
  58. if (!chunk.canBeInitial()) {
  59. callback();
  60. return;
  61. }
  62. const chunkGraph = compilation.chunkGraph;
  63. const targetPath = compilation.getPath(this.options.path, {
  64. chunk
  65. });
  66. if (usedPaths.has(targetPath)) {
  67. callback(new Error("each chunk must have a unique path"));
  68. return;
  69. }
  70. usedPaths.add(targetPath);
  71. const name =
  72. this.options.name &&
  73. compilation.getPath(this.options.name, {
  74. chunk,
  75. contentHashType: "javascript"
  76. });
  77. const content = Object.create(null);
  78. for (const module of chunkGraph.getOrderedChunkModulesIterable(
  79. chunk,
  80. compareModulesById(chunkGraph)
  81. )) {
  82. if (
  83. this.options.entryOnly &&
  84. !someInIterable(
  85. moduleGraph.getIncomingConnections(module),
  86. (c) => c.dependency instanceof EntryDependency
  87. )
  88. ) {
  89. continue;
  90. }
  91. const ident = module.libIdent({
  92. context: this.options.context || compiler.context,
  93. associatedObjectForCache: compiler.root
  94. });
  95. if (ident) {
  96. const exportsInfo = moduleGraph.getExportsInfo(module);
  97. const providedExports = exportsInfo.getProvidedExports();
  98. /** @type {ManifestModuleData} */
  99. const data = {
  100. id: /** @type {ModuleId} */ (chunkGraph.getModuleId(module)),
  101. buildMeta: /** @type {BuildMeta} */ (module.buildMeta),
  102. exports: Array.isArray(providedExports)
  103. ? providedExports
  104. : undefined
  105. };
  106. content[ident] = data;
  107. }
  108. }
  109. const manifest = {
  110. name,
  111. type: this.options.type,
  112. content
  113. };
  114. // Apply formatting to content if format flag is true;
  115. const manifestContent = this.options.format
  116. ? JSON.stringify(manifest, null, 2)
  117. : JSON.stringify(manifest);
  118. const buffer = Buffer.from(manifestContent, "utf8");
  119. const intermediateFileSystem =
  120. /** @type {IntermediateFileSystem} */ (
  121. compiler.intermediateFileSystem
  122. );
  123. mkdirp(
  124. intermediateFileSystem,
  125. dirname(intermediateFileSystem, targetPath),
  126. (err) => {
  127. if (err) return callback(err);
  128. intermediateFileSystem.writeFile(targetPath, buffer, callback);
  129. }
  130. );
  131. },
  132. callback
  133. );
  134. }
  135. );
  136. }
  137. }
  138. module.exports = LibManifestPlugin;