UNPKG

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