UNPKG

2.2 kBJavaScriptView Raw
1"use strict";
2
3var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) {
6 try {
7 step(generator.next(value));
8 } catch (e) {
9 reject(e);
10 }
11 }
12
13 function rejected(value) {
14 try {
15 step(generator["throw"](value));
16 } catch (e) {
17 reject(e);
18 }
19 }
20
21 function step(result) {
22 result.done ? resolve(result.value) : new P(function (resolve) {
23 resolve(result.value);
24 }).then(fulfilled, rejected);
25 }
26
27 step((generator = generator.apply(thisArg, _arguments || [])).next());
28 });
29};
30
31Object.defineProperty(exports, "__esModule", {
32 value: true
33});
34
35const path_1 = require("path");
36
37const fs_extra_1 = require("fs-extra");
38
39const manifest_1 = require("./manifest");
40
41const entrypoints_1 = require("./entrypoints");
42
43class AssetMetadataPlugin {
44 constructor(options = {}) {
45 this.options = Object.assign({
46 assetBasePath: false,
47 filename: 'assets.json'
48 }, options);
49 }
50
51 apply(compiler) {
52 compiler.hooks.afterEmit.tapAsync('AssetMetadataPlugin', (compilation, callback) => __awaiter(this, void 0, void 0, function* () {
53 const assetBasePath = this.options.assetBasePath || compilation.outputOptions.path;
54 const output = {
55 assets: manifest_1.getAssetManifest(assetBasePath, compilation),
56 entrypoints: {},
57 asyncAssets: manifest_1.getAsyncAssetManifest(compilation, {
58 includeIntegrity: compiler.options.mode === 'production'
59 })
60 };
61 compilation.entrypoints.forEach((_, entryName) => {
62 output.entrypoints[entryName] = entrypoints_1.getChunkDependencies(compilation, entryName);
63 });
64
65 try {
66 const file = path_1.join(compilation.outputOptions.path || '', this.options.filename);
67 yield fs_extra_1.mkdirp(path_1.dirname(file));
68 yield fs_extra_1.writeFile(file, JSON.stringify(output, null, 2));
69 } catch (err) {
70 compilation.errors.push(err);
71 } finally {
72 callback();
73 }
74 }));
75 }
76
77}
78
79exports.AssetMetadataPlugin = AssetMetadataPlugin;
\No newline at end of file