UNPKG

3.72 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
4
5var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } };
6
7var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); };
8
9var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
10
11var fs = _interopRequire(require("fs"));
12
13var path = _interopRequire(require("path"));
14
15var RequestShortener = _interopRequire(require("webpack/lib/RequestShortener"));
16
17function ExtractAssets(modules, requestShortener, publicPath) {
18 var emitted = false;
19 var assets = modules.map(function (m) {
20 var assets = Object.keys(m.assets || {});
21
22 if (assets.length === 0) {
23 return undefined;
24 }
25
26 var asset = assets[0];
27 emitted = emitted || m.assets[asset].emitted;
28
29 return {
30 name: m.readableIdentifier(requestShortener),
31 asset: asset
32 };
33 }).filter(function (m) {
34 return m !== undefined;
35 }).reduce(function (acc, m) {
36 acc[m.name] = path.join(publicPath, m.asset);
37 return acc;
38 }, {});
39
40 return [emitted, assets];
41}
42
43function ExtractChunks(chunks, publicPath) {
44 var emitted = false;
45 var chunks = chunks.map(function (c) {
46 return {
47 name: c.name,
48 files: c.files.filter(function (f) {
49 return path.extname(f) !== ".map";
50 }).map(function (f) {
51 return path.join(publicPath, f);
52 })
53 };
54 }).reduce(function (acc, c) {
55 acc[c.name] = c.files;
56 return acc;
57 }, {});
58
59 return [emitted, chunks];
60}
61
62var AssetMapPlugin = (function () {
63 /**
64 * AssetMapPlugin
65 *
66 * @param {string} outputFile - Where to write the asset map file
67 * @param {string} [relativeTo] - Key assets relative to this path, otherwise defaults to be relative to the directory where the outputFile is written
68 */
69 function AssetMapPlugin(outputFile, relativeTo) {
70 _classCallCheck(this, AssetMapPlugin);
71
72 this.outputFile = outputFile;
73 this.relativeTo = relativeTo;
74 }
75
76 _prototypeProperties(AssetMapPlugin, null, {
77 apply: {
78 value: function apply(compiler) {
79 var _this = this;
80 compiler.plugin("done", function (stats) {
81 var publicPath = stats.compilation.outputOptions.publicPath;
82 var requestShortener = new RequestShortener(_this.relativeTo || path.dirname(_this.outputFile));
83
84 var _ExtractAssets = ExtractAssets(stats.compilation.modules, requestShortener, publicPath);
85
86 var _ExtractAssets2 = _slicedToArray(_ExtractAssets, 2);
87
88 var assetsEmitted = _ExtractAssets2[0];
89 var assets = _ExtractAssets2[1];
90 var _ExtractChunks = ExtractChunks(stats.compilation.chunks, publicPath);
91
92 var _ExtractChunks2 = _slicedToArray(_ExtractChunks, 2);
93
94 var chunksEmitted = _ExtractChunks2[0];
95 var chunks = _ExtractChunks2[1];
96
97
98 if (assetsEmitted || chunksEmitted) {
99 fs.writeFileSync(_this.outputFile, JSON.stringify({ assets: assets, chunks: chunks }));
100 }
101 });
102 },
103 writable: true,
104 configurable: true
105 }
106 });
107
108 return AssetMapPlugin;
109})();
110
111module.exports = AssetMapPlugin;
\No newline at end of file