LibManifestPlugin.js 4.4 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("./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. Array.from(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} */ (compiler.options.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;