LibManifestPlugin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "LibManifestPlugin",
  43. (compilation, callback) => {
  44. const moduleGraph = compilation.moduleGraph;
  45. asyncLib.forEach(
  46. Array.from(compilation.chunks),
  47. (chunk, callback) => {
  48. if (!chunk.canBeInitial()) {
  49. callback();
  50. return;
  51. }
  52. const chunkGraph = compilation.chunkGraph;
  53. const targetPath = compilation.getPath(this.options.path, {
  54. chunk
  55. });
  56. const name =
  57. this.options.name &&
  58. compilation.getPath(this.options.name, {
  59. chunk,
  60. contentHashType: "javascript"
  61. });
  62. const content = Object.create(null);
  63. for (const module of chunkGraph.getOrderedChunkModulesIterable(
  64. chunk,
  65. compareModulesById(chunkGraph)
  66. )) {
  67. if (
  68. this.options.entryOnly &&
  69. !someInIterable(
  70. moduleGraph.getIncomingConnections(module),
  71. c => c.dependency instanceof EntryDependency
  72. )
  73. ) {
  74. continue;
  75. }
  76. const ident = module.libIdent({
  77. context:
  78. this.options.context ||
  79. /** @type {string} */ (compiler.options.context),
  80. associatedObjectForCache: compiler.root
  81. });
  82. if (ident) {
  83. const exportsInfo = moduleGraph.getExportsInfo(module);
  84. const providedExports = exportsInfo.getProvidedExports();
  85. /** @type {ManifestModuleData} */
  86. const data = {
  87. id: chunkGraph.getModuleId(module),
  88. buildMeta: /** @type {BuildMeta} */ (module.buildMeta),
  89. exports: Array.isArray(providedExports)
  90. ? providedExports
  91. : undefined
  92. };
  93. content[ident] = data;
  94. }
  95. }
  96. const manifest = {
  97. name,
  98. type: this.options.type,
  99. content
  100. };
  101. // Apply formatting to content if format flag is true;
  102. const manifestContent = this.options.format
  103. ? JSON.stringify(manifest, null, 2)
  104. : JSON.stringify(manifest);
  105. const buffer = Buffer.from(manifestContent, "utf8");
  106. mkdirp(
  107. compiler.intermediateFileSystem,
  108. dirname(compiler.intermediateFileSystem, targetPath),
  109. err => {
  110. if (err) return callback(err);
  111. compiler.intermediateFileSystem.writeFile(
  112. targetPath,
  113. buffer,
  114. callback
  115. );
  116. }
  117. );
  118. },
  119. callback
  120. );
  121. }
  122. );
  123. }
  124. }
  125. module.exports = LibManifestPlugin;