@parcel/reporter-bundle-stats 2.9.4-nightly.2962
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/lib/BundleStatsReporter.js +104 -0
- package/package.json +25 -0
- package/src/BundleStatsReporter.js +99 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017-present Devon Govett
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
exports.getBundleStats = getBundleStats;
|
|
8
|
+
function _plugin() {
|
|
9
|
+
const data = require("@parcel/plugin");
|
|
10
|
+
_plugin = function () {
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
return data;
|
|
14
|
+
}
|
|
15
|
+
function _utils() {
|
|
16
|
+
const data = require("@parcel/utils");
|
|
17
|
+
_utils = function () {
|
|
18
|
+
return data;
|
|
19
|
+
};
|
|
20
|
+
return data;
|
|
21
|
+
}
|
|
22
|
+
function _assert() {
|
|
23
|
+
const data = _interopRequireDefault(require("assert"));
|
|
24
|
+
_assert = function () {
|
|
25
|
+
return data;
|
|
26
|
+
};
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
function _path() {
|
|
30
|
+
const data = _interopRequireDefault(require("path"));
|
|
31
|
+
_path = function () {
|
|
32
|
+
return data;
|
|
33
|
+
};
|
|
34
|
+
return data;
|
|
35
|
+
}
|
|
36
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
37
|
+
var _default = new (_plugin().Reporter)({
|
|
38
|
+
async report({
|
|
39
|
+
event,
|
|
40
|
+
options
|
|
41
|
+
}) {
|
|
42
|
+
if (event.type !== 'buildSuccess') {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
let bundlesByTarget = new (_utils().DefaultMap)(() => []);
|
|
46
|
+
for (let bundle of event.bundleGraph.getBundles()) {
|
|
47
|
+
bundlesByTarget.get(bundle.target.name).push(bundle);
|
|
48
|
+
}
|
|
49
|
+
let reportsDir = _path().default.join(options.projectRoot, 'parcel-bundle-reports');
|
|
50
|
+
await options.outputFS.mkdirp(reportsDir);
|
|
51
|
+
await Promise.all([...bundlesByTarget.entries()].map(([targetName, bundles]) => options.outputFS.writeFile(_path().default.join(reportsDir, `${targetName}-stats.json`), JSON.stringify(getBundleStats(bundles, options), null, 2))));
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
exports.default = _default;
|
|
55
|
+
function getBundleStats(bundles, options) {
|
|
56
|
+
let bundlesByName = new Map();
|
|
57
|
+
let assetsById = new Map();
|
|
58
|
+
|
|
59
|
+
// let seen = new Map();
|
|
60
|
+
|
|
61
|
+
for (let bundle of bundles) {
|
|
62
|
+
let bundleName = _path().default.relative(options.projectRoot, bundle.filePath);
|
|
63
|
+
|
|
64
|
+
// If we've already seen this bundle, we can skip it... right?
|
|
65
|
+
if (bundlesByName.has(bundleName)) {
|
|
66
|
+
var _bundlesByName$get;
|
|
67
|
+
// Sanity check: this is the same bundle, right?
|
|
68
|
+
(0, _assert().default)(((_bundlesByName$get = bundlesByName.get(bundleName)) === null || _bundlesByName$get === void 0 ? void 0 : _bundlesByName$get.size) === bundle.stats.size);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
let assets = [];
|
|
72
|
+
bundle.traverseAssets(({
|
|
73
|
+
id,
|
|
74
|
+
filePath,
|
|
75
|
+
stats: {
|
|
76
|
+
size
|
|
77
|
+
}
|
|
78
|
+
}) => {
|
|
79
|
+
assets.push(id);
|
|
80
|
+
let assetName = _path().default.relative(options.projectRoot, filePath);
|
|
81
|
+
if (assetsById.has(id)) {
|
|
82
|
+
var _assetsById$get, _assetsById$get2, _assetsById$get3;
|
|
83
|
+
(0, _assert().default)(((_assetsById$get = assetsById.get(id)) === null || _assetsById$get === void 0 ? void 0 : _assetsById$get.name) === assetName);
|
|
84
|
+
(0, _assert().default)(((_assetsById$get2 = assetsById.get(id)) === null || _assetsById$get2 === void 0 ? void 0 : _assetsById$get2.size) === size);
|
|
85
|
+
(_assetsById$get3 = assetsById.get(id)) === null || _assetsById$get3 === void 0 ? void 0 : _assetsById$get3.bundles.push(bundleName);
|
|
86
|
+
} else {
|
|
87
|
+
assetsById.set(id, {
|
|
88
|
+
name: assetName,
|
|
89
|
+
size,
|
|
90
|
+
bundles: [bundleName]
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
bundlesByName.set(bundleName, {
|
|
95
|
+
id: bundle.id,
|
|
96
|
+
size: bundle.stats.size,
|
|
97
|
+
assets
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
bundles: Object.fromEntries(bundlesByName),
|
|
102
|
+
assets: Object.fromEntries(assetsById)
|
|
103
|
+
};
|
|
104
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@parcel/reporter-bundle-stats",
|
|
3
|
+
"version": "2.9.4-nightly.2962+2d2400ded",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"main": "lib/BundleStatsReporter.js",
|
|
8
|
+
"source": "src/BundleStatsReporter.js",
|
|
9
|
+
"bin": {
|
|
10
|
+
"parcel-bundle-stats": "bin.js"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">= 12.0.0",
|
|
14
|
+
"parcel": "2.0.0-nightly.1337+2d2400ded"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@parcel/core": "2.0.0-nightly.1337+2d2400ded",
|
|
18
|
+
"@parcel/plugin": "2.0.0-nightly.1339+2d2400ded",
|
|
19
|
+
"@parcel/utils": "2.0.0-nightly.1339+2d2400ded"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@parcel/types": "2.0.0-nightly.1339+2d2400ded"
|
|
23
|
+
},
|
|
24
|
+
"gitHead": "2d2400ded4615375ee6bd53ef77b4857ad1591dd"
|
|
25
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// @flow strict-local
|
|
2
|
+
|
|
3
|
+
import type {PackagedBundle, PluginOptions} from '@parcel/types';
|
|
4
|
+
|
|
5
|
+
import {Reporter} from '@parcel/plugin';
|
|
6
|
+
import {DefaultMap} from '@parcel/utils';
|
|
7
|
+
|
|
8
|
+
import assert from 'assert';
|
|
9
|
+
import path from 'path';
|
|
10
|
+
|
|
11
|
+
export type AssetStat = {|
|
|
12
|
+
size: number,
|
|
13
|
+
name: string,
|
|
14
|
+
bundles: Array<string>,
|
|
15
|
+
|};
|
|
16
|
+
|
|
17
|
+
export type BundleStat = {|
|
|
18
|
+
size: number,
|
|
19
|
+
id: string,
|
|
20
|
+
assets: Array<string>,
|
|
21
|
+
|};
|
|
22
|
+
|
|
23
|
+
export type BundleStats = {|
|
|
24
|
+
bundles: {[key: string]: BundleStat},
|
|
25
|
+
assets: {[key: string]: AssetStat},
|
|
26
|
+
|};
|
|
27
|
+
|
|
28
|
+
export default (new Reporter({
|
|
29
|
+
async report({event, options}) {
|
|
30
|
+
if (event.type !== 'buildSuccess') {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let bundlesByTarget: DefaultMap<
|
|
35
|
+
string /* target name */,
|
|
36
|
+
Array<PackagedBundle>,
|
|
37
|
+
> = new DefaultMap(() => []);
|
|
38
|
+
for (let bundle of event.bundleGraph.getBundles()) {
|
|
39
|
+
bundlesByTarget.get(bundle.target.name).push(bundle);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let reportsDir = path.join(options.projectRoot, 'parcel-bundle-reports');
|
|
43
|
+
await options.outputFS.mkdirp(reportsDir);
|
|
44
|
+
|
|
45
|
+
await Promise.all(
|
|
46
|
+
[...bundlesByTarget.entries()].map(([targetName, bundles]) =>
|
|
47
|
+
options.outputFS.writeFile(
|
|
48
|
+
path.join(reportsDir, `${targetName}-stats.json`),
|
|
49
|
+
JSON.stringify(getBundleStats(bundles, options), null, 2),
|
|
50
|
+
),
|
|
51
|
+
),
|
|
52
|
+
);
|
|
53
|
+
},
|
|
54
|
+
}): Reporter);
|
|
55
|
+
|
|
56
|
+
export function getBundleStats(
|
|
57
|
+
bundles: Array<PackagedBundle>,
|
|
58
|
+
options: PluginOptions,
|
|
59
|
+
): BundleStats {
|
|
60
|
+
let bundlesByName = new Map<string, BundleStat>();
|
|
61
|
+
let assetsById = new Map<string, AssetStat>();
|
|
62
|
+
|
|
63
|
+
// let seen = new Map();
|
|
64
|
+
|
|
65
|
+
for (let bundle of bundles) {
|
|
66
|
+
let bundleName = path.relative(options.projectRoot, bundle.filePath);
|
|
67
|
+
|
|
68
|
+
// If we've already seen this bundle, we can skip it... right?
|
|
69
|
+
if (bundlesByName.has(bundleName)) {
|
|
70
|
+
// Sanity check: this is the same bundle, right?
|
|
71
|
+
assert(bundlesByName.get(bundleName)?.size === bundle.stats.size);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let assets = [];
|
|
76
|
+
bundle.traverseAssets(({id, filePath, stats: {size}}) => {
|
|
77
|
+
assets.push(id);
|
|
78
|
+
let assetName = path.relative(options.projectRoot, filePath);
|
|
79
|
+
if (assetsById.has(id)) {
|
|
80
|
+
assert(assetsById.get(id)?.name === assetName);
|
|
81
|
+
assert(assetsById.get(id)?.size === size);
|
|
82
|
+
assetsById.get(id)?.bundles.push(bundleName);
|
|
83
|
+
} else {
|
|
84
|
+
assetsById.set(id, {name: assetName, size, bundles: [bundleName]});
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
bundlesByName.set(bundleName, {
|
|
89
|
+
id: bundle.id,
|
|
90
|
+
size: bundle.stats.size,
|
|
91
|
+
assets,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
bundles: Object.fromEntries(bundlesByName),
|
|
97
|
+
assets: Object.fromEntries(assetsById),
|
|
98
|
+
};
|
|
99
|
+
}
|