LibManifestPlugin.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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("async");
  8. class LibManifestPlugin {
  9. constructor(options) {
  10. this.options = options;
  11. }
  12. apply(compiler) {
  13. compiler.plugin("emit", (compilation, callback) => {
  14. asyncLib.forEach(compilation.chunks, (chunk, callback) => {
  15. if(!chunk.isInitial()) {
  16. callback();
  17. return;
  18. }
  19. const targetPath = compilation.getPath(this.options.path, {
  20. hash: compilation.hash,
  21. chunk
  22. });
  23. const name = this.options.name && compilation.getPath(this.options.name, {
  24. hash: compilation.hash,
  25. chunk
  26. });
  27. const manifest = {
  28. name,
  29. type: this.options.type,
  30. content: chunk.modules.reduce((obj, module) => {
  31. if(module.libIdent) {
  32. const ident = module.libIdent({
  33. context: this.options.context || compiler.options.context
  34. });
  35. if(ident) {
  36. obj[ident] = {
  37. id: module.id,
  38. meta: module.meta,
  39. exports: Array.isArray(module.providedExports) ? module.providedExports : undefined
  40. };
  41. }
  42. }
  43. return obj;
  44. }, {})
  45. };
  46. const content = new Buffer(JSON.stringify(manifest), "utf8"); //eslint-disable-line
  47. compiler.outputFileSystem.mkdirp(path.dirname(targetPath), err => {
  48. if(err) return callback(err);
  49. compiler.outputFileSystem.writeFile(targetPath, content, callback);
  50. });
  51. }, callback);
  52. });
  53. }
  54. }
  55. module.exports = LibManifestPlugin;