LibManifestPlugin.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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[]=} 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. const PLUGIN_NAME = "LibManifestPlugin";
  31. class LibManifestPlugin {
  32. /**
  33. * @param {LibManifestPluginOptions} options the options
  34. */
  35. constructor(options) {
  36. this.options = options;
  37. }
  38. /**
  39. * Apply the plugin
  40. * @param {Compiler} compiler the compiler instance
  41. * @returns {void}
  42. */
  43. apply(compiler) {
  44. compiler.hooks.emit.tapAsync(
  45. { name: PLUGIN_NAME, stage: 110 },
  46. (compilation, callback) => {
  47. const moduleGraph = compilation.moduleGraph;
  48. // store used paths to detect issue and output an error. #18200
  49. const usedPaths = new Set();
  50. asyncLib.each(
  51. [...compilation.chunks],
  52. (chunk, callback) => {
  53. if (!chunk.canBeInitial()) {
  54. callback();
  55. return;
  56. }
  57. const chunkGraph = compilation.chunkGraph;
  58. const targetPath = compilation.getPath(this.options.path, {
  59. chunk
  60. });
  61. if (usedPaths.has(targetPath)) {
  62. callback(new Error("each chunk must have a unique path"));
  63. return;
  64. }
  65. usedPaths.add(targetPath);
  66. const name =
  67. this.options.name &&
  68. compilation.getPath(this.options.name, {
  69. chunk,
  70. contentHashType: "javascript"
  71. });
  72. const content = Object.create(null);
  73. for (const module of chunkGraph.getOrderedChunkModulesIterable(
  74. chunk,
  75. compareModulesById(chunkGraph)
  76. )) {
  77. if (
  78. this.options.entryOnly &&
  79. !someInIterable(
  80. moduleGraph.getIncomingConnections(module),
  81. c => c.dependency instanceof EntryDependency
  82. )
  83. ) {
  84. continue;
  85. }
  86. const ident = module.libIdent({
  87. context:
  88. this.options.context ||
  89. /** @type {string} */
  90. (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: /** @type {ModuleId} */ (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;