LibManifestPlugin.js 4.3 KB

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