LibManifestPlugin.js 3.1 KB

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