LibManifestPlugin.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const path = require("path");
  7. const asyncLib = require("neo-async");
  8. const SingleEntryDependency = require("./dependencies/SingleEntryDependency");
  9. class LibManifestPlugin {
  10. constructor(options) {
  11. this.options = options;
  12. }
  13. apply(compiler) {
  14. compiler.hooks.emit.tapAsync(
  15. "LibManifestPlugin",
  16. (compilation, callback) => {
  17. asyncLib.forEach(
  18. compilation.chunks,
  19. (chunk, callback) => {
  20. if (!chunk.isOnlyInitial()) {
  21. callback();
  22. return;
  23. }
  24. const targetPath = compilation.getPath(this.options.path, {
  25. hash: compilation.hash,
  26. chunk
  27. });
  28. const name =
  29. this.options.name &&
  30. compilation.getPath(this.options.name, {
  31. hash: compilation.hash,
  32. chunk
  33. });
  34. const manifest = {
  35. name,
  36. type: this.options.type,
  37. content: Array.from(chunk.modulesIterable, module => {
  38. if (
  39. this.options.entryOnly &&
  40. !module.reasons.some(
  41. r => r.dependency instanceof SingleEntryDependency
  42. )
  43. ) {
  44. return;
  45. }
  46. if (module.libIdent) {
  47. const ident = module.libIdent({
  48. context: this.options.context || compiler.options.context
  49. });
  50. if (ident) {
  51. return {
  52. ident,
  53. data: {
  54. id: module.id,
  55. buildMeta: module.buildMeta
  56. }
  57. };
  58. }
  59. }
  60. })
  61. .filter(Boolean)
  62. .reduce((obj, item) => {
  63. obj[item.ident] = item.data;
  64. return obj;
  65. }, Object.create(null))
  66. };
  67. const content = Buffer.from(JSON.stringify(manifest), "utf8");
  68. compiler.outputFileSystem.mkdirp(path.dirname(targetPath), err => {
  69. if (err) return callback(err);
  70. compiler.outputFileSystem.writeFile(
  71. targetPath,
  72. content,
  73. callback
  74. );
  75. });
  76. },
  77. callback
  78. );
  79. }
  80. );
  81. }
  82. }
  83. module.exports = LibManifestPlugin;