LibManifestPlugin.js 4.5 KB

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