Skip to content

Commit c4c5c7b

Browse files
committed
added bundle analysis
caveats: * there might be performance implications: doesdev/rollup-plugin-analyzer#3 * no detailed report yet: doesdev/rollup-plugin-analyzer#2 * missing configuration options (cf. inline comment)
1 parent 8f4d82d commit c4c5c7b

File tree

7 files changed

+81
-16
lines changed

7 files changed

+81
-16
lines changed

lib/bundle/bundler.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1+
let rollupAnalyzer = require("rollup-plugin-analyzer").plugin;
12
let rollup = require("rollup");
23

34
module.exports = function generateBundle(entryPoint, target, config, cache) {
4-
let { readConfig, writeConfig } = config;
5+
let { readConfig, writeConfig, reporting } = config;
56
let options = Object.assign({}, readConfig, {
67
input: entryPoint,
78
cache
89
});
10+
11+
if(reporting) {
12+
options.plugins.push(rollupAnalyzer({
13+
root: reporting.referenceDir,
14+
// TODO: support for options: `limit`, `filter`, `showExports`, `hideDeps`
15+
onAnalysis: res => void reporting.report({
16+
size: res.bundleSize,
17+
originalSize: res.bundleOrigSize,
18+
reduction: res.bundleReduction
19+
})
20+
}));
21+
}
22+
923
return rollup.rollup(options).
1024
then(bundle => {
1125
let modules = bundle.modules.reduce(collectModulePaths, new Set());

lib/bundle/index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ let generateBundle = require("./bundler");
44
let generateConfig = require("./config");
55
let { generateError } = require("../util");
66
let SerializedRunner = require("faucet-pipeline/lib/util/runner");
7+
let path = require("path");
78

89
let DEFAULTS = {
910
format: "iife"
1011
};
1112

1213
module.exports = class Bundle {
13-
constructor(entryPoint, target, config, { browsers, resolvePath }) {
14+
constructor(entryPoint, target, config, { browsers, referenceDir, resolvePath }) {
1415
this.entryPoint = entryPoint;
1516
this.target = target;
1617

@@ -20,6 +21,27 @@ module.exports = class Bundle {
2021
delete config.fingerprint;
2122
}
2223
this._config = generateConfig(config, { browsers, resolvePath });
24+
25+
let { reporting } = config;
26+
if(reporting !== false) {
27+
reporting = reporting || {};
28+
let threshold = reporting.threshold || 100000; // ~100 kB
29+
this._config.reporting = {
30+
referenceDir,
31+
report: ({ size, originalSize, reduction }) => {
32+
let b2kb = i => `${Math.round(i / 1024)} kB`;
33+
console.error(`${b2kb(originalSize)}${b2kb(size)} ` +
34+
`(Δ ${Math.round(reduction)} %)`);
35+
if(size > threshold) {
36+
console.error("⚠️ this bundle looks to be fairly big - " +
37+
"you might want to double-check whether " +
38+
"that's intended and consider performance " +
39+
"implications for your users: " +
40+
path.relative(referenceDir, target));
41+
}
42+
}
43+
};
44+
}
2345
}
2446

2547
// recompiles the bundle if its dependency graph includes any of the given files

lib/index.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
let Bundle = require("./bundle");
44
let { abort, repr } = require("faucet-pipeline/lib/util");
5-
let path = require("path");
65

76
module.exports = (config, assetManager, { watcher, browsers, compact } = {}) => {
87
let bundles = config.map(bundleConfig => {
98
// NB: bundle-specific configuration can override global options
109
bundleConfig = Object.assign({ compact }, bundleConfig);
11-
return makeBundle(bundleConfig, assetManager.resolvePath, { browsers });
10+
return makeBundle(bundleConfig, assetManager, { browsers });
1211
});
1312

1413
let res = bundles.map(bundle => {
@@ -38,13 +37,6 @@ module.exports = (config, assetManager, { watcher, browsers, compact } = {}) =>
3837

3938
function makeWriter(bundle, assetManager, { fingerprint } = {}) {
4039
return ({ code, error }) => {
41-
if(code.length > 100000) { // ~100 kB -- XXX: arbitrary -- TODO: configurable
42-
console.error("⚠️ this bundle looks to be fairly big, you might " +
43-
"want to double-check whether that's intended and " +
44-
"consider performance implications for your users:\n " +
45-
path.relative(assetManager.referenceDir, bundle.target));
46-
}
47-
4840
let options = { error };
4941
if(fingerprint !== undefined) {
5042
options.fingerprint = fingerprint;
@@ -53,7 +45,7 @@ function makeWriter(bundle, assetManager, { fingerprint } = {}) {
5345
};
5446
}
5547

56-
function makeBundle(config, resolvePath, { browsers }) {
48+
function makeBundle(config, { referenceDir, resolvePath }, { browsers }) {
5749
// dissect configuration for constructor
5850
config = Object.assign({}, config);
5951
let [entryPoint, target] = extract(config, "source", "target");
@@ -64,7 +56,8 @@ function makeBundle(config, resolvePath, { browsers }) {
6456

6557
entryPoint = resolvePath(entryPoint);
6658
target = resolvePath(target, { enforceRelative: true });
67-
return new Bundle(entryPoint, target, config, { browsers, resolvePath });
59+
return new Bundle(entryPoint, target, config,
60+
{ browsers, referenceDir, resolvePath });
6861
}
6962

7063
// removes properties from object, returning their respective values

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"dependencies": {
2929
"faucet-pipeline": "~1.0.0-rc.7",
3030
"rollup": "~0.62.0",
31+
"rollup-plugin-analyzer": "^2.0.4",
3132
"rollup-plugin-cleanup": "~3.0.0",
3233
"rollup-plugin-commonjs": "~9.1.3",
3334
"rollup-plugin-node-resolve": "~3.3.0"

test/cli/test_custom_config/assets.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ let path = require("path");
55
module.exports = {
66
js: [{
77
source: "./index.js",
8-
target: "./dist/bundle.js"
8+
target: "./dist/bundle.js",
9+
reporting: {
10+
threshold: 100
11+
}
912
}],
1013
plugins: {
1114
js: path.resolve(__dirname, "../../..")

test/cli/test_custom_config/expected.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ if(typeof global === "undefined" && typeof window !== "undefined") {
55
window.global = window;
66
}
77

8-
// N/A
8+
// lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
9+
// tempor incididunt ut labore et dolore magna aliqua
10+
// ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
11+
// aliquip ex ea commodo consequat
12+
// duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
13+
// eu fugiat nulla pariatur
14+
// excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
15+
// deserunt mollit anim id est laborum
16+
//
17+
// lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
18+
// tempor incididunt ut labore et dolore magna aliqua
19+
// ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
20+
// aliquip ex ea commodo consequat
21+
// duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
22+
// eu fugiat nulla pariatur
23+
// excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
24+
// deserunt mollit anim id est laborum
925

1026
}());
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
// N/A
1+
// lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
2+
// tempor incididunt ut labore et dolore magna aliqua
3+
// ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
4+
// aliquip ex ea commodo consequat
5+
// duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
6+
// eu fugiat nulla pariatur
7+
// excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
8+
// deserunt mollit anim id est laborum
9+
//
10+
// lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
11+
// tempor incididunt ut labore et dolore magna aliqua
12+
// ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
13+
// aliquip ex ea commodo consequat
14+
// duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
15+
// eu fugiat nulla pariatur
16+
// excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
17+
// deserunt mollit anim id est laborum

0 commit comments

Comments
 (0)