LibManifestPlugin.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /**
  13. * @typedef {Object} ManifestModuleData
  14. * @property {string | number} id
  15. * @property {Object} buildMeta
  16. * @property {boolean | string[]} exports
  17. */
  18. class LibManifestPlugin {
  19. constructor(options) {
  20. this.options = options;
  21. }
  22. /**
  23. * Apply the plugin
  24. * @param {Compiler} compiler the compiler instance
  25. * @returns {void}
  26. */
  27. apply(compiler) {
  28. compiler.hooks.emit.tapAsync(
  29. "LibManifestPlugin",
  30. (compilation, callback) => {
  31. const moduleGraph = compilation.moduleGraph;
  32. asyncLib.forEach(
  33. Array.from(compilation.chunks),
  34. (chunk, callback) => {
  35. if (!chunk.canBeInitial()) {
  36. callback();
  37. return;
  38. }
  39. const chunkGraph = compilation.chunkGraph;
  40. const targetPath = compilation.getPath(this.options.path, {
  41. chunk
  42. });
  43. const name =
  44. this.options.name &&
  45. compilation.getPath(this.options.name, {
  46. chunk
  47. });
  48. const content = Object.create(null);
  49. for (const module of chunkGraph.getOrderedChunkModulesIterable(
  50. chunk,
  51. compareModulesById(chunkGraph)
  52. )) {
  53. if (
  54. this.options.entryOnly &&
  55. !someInIterable(
  56. moduleGraph.getIncomingConnections(module),
  57. c => c.dependency instanceof EntryDependency
  58. )
  59. ) {
  60. continue;
  61. }
  62. const ident = module.libIdent({
  63. context: this.options.context || compiler.options.context,
  64. associatedObjectForCache: compiler.root
  65. });
  66. if (ident) {
  67. const exportsInfo = moduleGraph.getExportsInfo(module);
  68. const providedExports = exportsInfo.getProvidedExports();
  69. /** @type {ManifestModuleData} */
  70. const data = {
  71. id: chunkGraph.getModuleId(module),
  72. buildMeta: module.buildMeta,
  73. exports: Array.isArray(providedExports)
  74. ? providedExports
  75. : undefined
  76. };
  77. content[ident] = data;
  78. }
  79. }
  80. const manifest = {
  81. name,
  82. type: this.options.type,
  83. content
  84. };
  85. // Apply formatting to content if format flag is true;
  86. const manifestContent = this.options.format
  87. ? JSON.stringify(manifest, null, 2)
  88. : JSON.stringify(manifest);
  89. const buffer = Buffer.from(manifestContent, "utf8");
  90. mkdirp(
  91. compiler.intermediateFileSystem,
  92. dirname(compiler.intermediateFileSystem, targetPath),
  93. err => {
  94. if (err) return callback(err);
  95. compiler.intermediateFileSystem.writeFile(
  96. targetPath,
  97. buffer,
  98. callback
  99. );
  100. }
  101. );
  102. },
  103. callback
  104. );
  105. }
  106. );
  107. }
  108. }
  109. module.exports = LibManifestPlugin;