LibManifestPlugin.js 3.9 KB

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