LibManifestPlugin.js 4.4 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. /**
  16. * @typedef {object} ManifestModuleData
  17. * @property {string | number} id
  18. * @property {BuildMeta} buildMeta
  19. * @property {boolean | string[] | undefined} exports
  20. */
  21. /**
  22. * @typedef {object} LibManifestPluginOptions
  23. * @property {string=} context Context of requests in the manifest file (defaults to the webpack context).
  24. * @property {boolean=} entryOnly If true, only entry points will be exposed (default: true).
  25. * @property {boolean=} format If true, manifest json file (output) will be formatted.
  26. * @property {string=} name Name of the exposed dll function (external name, use value of 'output.library').
  27. * @property {string} path Absolute path to the manifest json file (output).
  28. * @property {string=} type Type of the dll bundle (external type, use value of 'output.libraryTarget').
  29. */
  30. class LibManifestPlugin {
  31. /**
  32. * @param {LibManifestPluginOptions} options the options
  33. */
  34. constructor(options) {
  35. this.options = options;
  36. }
  37. /**
  38. * Apply the plugin
  39. * @param {Compiler} compiler the compiler instance
  40. * @returns {void}
  41. */
  42. apply(compiler) {
  43. compiler.hooks.emit.tapAsync(
  44. {
  45. name: "LibManifestPlugin",
  46. stage: 110
  47. },
  48. (compilation, callback) => {
  49. const moduleGraph = compilation.moduleGraph;
  50. // store used paths to detect issue and output an error. #18200
  51. const usedPaths = new Set();
  52. asyncLib.each(
  53. Array.from(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:
  90. this.options.context ||
  91. /** @type {string} */ (compiler.options.context),
  92. associatedObjectForCache: compiler.root
  93. });
  94. if (ident) {
  95. const exportsInfo = moduleGraph.getExportsInfo(module);
  96. const providedExports = exportsInfo.getProvidedExports();
  97. /** @type {ManifestModuleData} */
  98. const data = {
  99. id: /** @type {ModuleId} */ (chunkGraph.getModuleId(module)),
  100. buildMeta: /** @type {BuildMeta} */ (module.buildMeta),
  101. exports: Array.isArray(providedExports)
  102. ? providedExports
  103. : undefined
  104. };
  105. content[ident] = data;
  106. }
  107. }
  108. const manifest = {
  109. name,
  110. type: this.options.type,
  111. content
  112. };
  113. // Apply formatting to content if format flag is true;
  114. const manifestContent = this.options.format
  115. ? JSON.stringify(manifest, null, 2)
  116. : JSON.stringify(manifest);
  117. const buffer = Buffer.from(manifestContent, "utf8");
  118. const intermediateFileSystem =
  119. /** @type {IntermediateFileSystem} */ (
  120. compiler.intermediateFileSystem
  121. );
  122. mkdirp(
  123. intermediateFileSystem,
  124. dirname(intermediateFileSystem, targetPath),
  125. err => {
  126. if (err) return callback(err);
  127. intermediateFileSystem.writeFile(targetPath, buffer, callback);
  128. }
  129. );
  130. },
  131. callback
  132. );
  133. }
  134. );
  135. }
  136. }
  137. module.exports = LibManifestPlugin;