index.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.WebpackManifestPlugin = exports.getCompilerHooks = void 0;
  7. const path_1 = require("path");
  8. const webpack_1 = __importDefault(require("webpack"));
  9. const NormalModule_1 = __importDefault(require("webpack/lib/NormalModule"));
  10. const hooks_1 = require("./hooks");
  11. Object.defineProperty(exports, "getCompilerHooks", { enumerable: true, get: function () { return hooks_1.getCompilerHooks; } });
  12. const emitCountMap = new Map();
  13. const defaults = {
  14. assetHookStage: Infinity,
  15. basePath: '',
  16. fileName: 'manifest.json',
  17. filter: null,
  18. generate: void 0,
  19. map: null,
  20. publicPath: null,
  21. removeKeyHash: /([a-f0-9]{16,32}\.?)/gi,
  22. seed: void 0,
  23. serialize(manifest) {
  24. return JSON.stringify(manifest, null, 2);
  25. },
  26. sort: null,
  27. transformExtensions: /^(gz|map)$/i,
  28. useEntryKeys: false,
  29. useLegacyEmit: false,
  30. writeToFileEmit: false
  31. };
  32. class WebpackManifestPlugin {
  33. constructor(opts) {
  34. this.options = Object.assign({}, defaults, opts);
  35. }
  36. apply(compiler) {
  37. var _a, _b, _c;
  38. const moduleAssets = {};
  39. const manifestFileName = path_1.resolve(((_a = compiler.options.output) === null || _a === void 0 ? void 0 : _a.path) || './', this.options.fileName);
  40. const manifestAssetId = path_1.relative(((_b = compiler.options.output) === null || _b === void 0 ? void 0 : _b.path) || './', manifestFileName);
  41. const beforeRun = hooks_1.beforeRunHook.bind(this, { emitCountMap, manifestFileName });
  42. const emit = hooks_1.emitHook.bind(this, {
  43. compiler,
  44. emitCountMap,
  45. manifestAssetId,
  46. manifestFileName,
  47. moduleAssets,
  48. options: this.options
  49. });
  50. const normalModuleLoader = hooks_1.normalModuleLoaderHook.bind(this, { moduleAssets });
  51. const hookOptions = {
  52. name: 'WebpackManifestPlugin',
  53. stage: this.options.assetHookStage
  54. };
  55. compiler.hooks.compilation.tap(hookOptions, (compilation) => {
  56. const hook = !NormalModule_1.default.getCompilationHooks
  57. ? compilation.hooks.normalModuleLoader
  58. : NormalModule_1.default.getCompilationHooks(compilation).loader;
  59. hook.tap(hookOptions, normalModuleLoader);
  60. });
  61. if (((_c = webpack_1.default.version) === null || _c === void 0 ? void 0 : _c.startsWith('4')) || this.options.useLegacyEmit === true) {
  62. compiler.hooks.emit.tap(hookOptions, emit);
  63. }
  64. else {
  65. compiler.hooks.thisCompilation.tap(hookOptions, (compilation) => {
  66. compilation.hooks.processAssets.tap(hookOptions, () => emit(compilation));
  67. });
  68. }
  69. compiler.hooks.run.tap(hookOptions, beforeRun);
  70. compiler.hooks.watchRun.tap(hookOptions, beforeRun);
  71. }
  72. }
  73. exports.WebpackManifestPlugin = WebpackManifestPlugin;
  74. //# sourceMappingURL=index.js.map