Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a --visualize option to generate build output stats #528

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.git/
.vscode/
dist/
node_modules/
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ yarn.lock
.idea
.rts2*
sizes.csv
test/fixtures/visualize/stats.html
.eslintcache
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:12

WORKDIR /microbundle

ADD package.json .
RUN npm install

VOLUME test/__snapshots__
ADD . .
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ You can specify output builds in a `package.json` as follows:
"types": "dist/foo.d.ts", // TypeScript typings
```

### Analyze your bundle size

Use the `--visualize` flag in the cli to generate a `stats.html` file at build time, showing the sizes of the files that are included in your bundled output. Uses [rollup-plugin-visualizer](https://www.npmjs.com/package/rollup-plugin-visualizer).

### Mangling Properties

Libraries often wish to rename internal object properties or class members to smaller names - transforming `this._internalIdValue` to `this._i`. Microbundle doesn't currently do this by default, but it can be enabled by adding a "mangle" property to your package.json, with a pattern to control when properties should be mangled. To mangle all property names beginning an underscore, add the following:
Expand Down Expand Up @@ -151,6 +155,7 @@ Options
--raw Show raw byte size (default false)
--jsx A custom JSX pragma like React.createElement (default: h)
--tsconfig Specify the path to a custom tsconfig.json
--visualize Generate bundle size visualization (stats.html)
-h, --help Displays this message

Examples
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"prepare:babel": "babel src/*.js -d dist && npm t",
"lint": "eslint src",
"test": "npm run -s lint && npm run -s build && cross-env BABEL_ENV=test jest",
"pretest:docker": "docker build -t microbundle .",
"test:docker": "docker run -v $(pwd)/test/__snapshots__:/microbundle/test/__snapshots__ -t microbundle yarn test",
"release": "npm run -s prepare && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
},
"repository": "developit/microbundle",
Expand Down Expand Up @@ -75,6 +77,7 @@
"rollup-plugin-postcss": "^2.0.3",
"rollup-plugin-terser": "^5.1.1",
"rollup-plugin-typescript2": "^0.23.0",
"rollup-plugin-visualizer": "^3.2.2",
"sade": "^1.6.1",
"tiny-glob": "^0.2.6",
"tslib": "^1.10.0",
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import brotliSize from 'brotli-size';
import prettyBytes from 'pretty-bytes';
import typescript from 'rollup-plugin-typescript2';
import json from 'rollup-plugin-json';
import visualizer from 'rollup-plugin-visualizer';
import logError from './log-error';
import { readFile, isDir, isFile, stdout, stderr, isTruthy } from './utils';
import camelCase from 'camelcase';
Expand Down Expand Up @@ -587,6 +588,7 @@ function createConfig(options, entry, format, writeMeta) {
typescript: !!useTypescript,
},
}),
options.visualize && visualizer(),
options.compress !== false && [
terser({
sourcemap: true,
Expand Down
7 changes: 6 additions & 1 deletion src/prog.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ export default handler => {
'A custom JSX pragma like React.createElement (default: h)',
)
.option('--tsconfig', 'Specify the path to a custom tsconfig.json')
.example('microbundle build --tsconfig tsconfig.build.json');
.example('microbundle build --tsconfig tsconfig.build.json')
.option(
'--visualize',
'Generate bundle size visualization (stats.html)',
false,
);

prog
.command('build [...entries]', '', { default: true })
Expand Down
3,453 changes: 3,435 additions & 18 deletions test/__snapshots__/index.test.js.snap

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions test/fixtures/visualize/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "visualize",
"scripts": {
"build": "microbundle --visualize",
"postbuild": "mv stats.html dist/"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can remove the need for a postbuild task. The docs of rollup-plugin-visualize don't mention it, but you can pass a directory with the filename setting.

https://github.com/btd/rollup-plugin-visualizer/blob/41aeee4741ab424c8e4e1ab48930ca5cbd2d8fb1/plugin/index.js#L129

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good plan.

@marvinhagemeister Do you have any idea about the other (unrelated) snapshots that are failing?
Last time I checked master is also failing too... so not sure what to do next.

},
"peerDependencies": {
"camelcase": "^5.3.1"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/visualize/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const camelCase = require('camelcase');

module.exports = camelCase('foo-bar');
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { buildDirectory, getBuildScript } from '../tools/build-fixture';

const FIXTURES_DIR = `${__dirname}/fixtures`;
const DEFAULT_SCRIPT = 'microbundle';
const TEST_TIMEOUT = 11000;
const TEST_TIMEOUT = 18000;

const join = (arr, delimiter = '') => arr.join(delimiter);

Expand Down
19 changes: 18 additions & 1 deletion tools/build-fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { resolve } from 'path';
import { promisify } from 'es6-promisify';
import shellQuote from 'shell-quote';
import _rimraf from 'rimraf';
import { exec as _exec } from 'child_process';
import { readFile } from '../src/utils';
import createProg from '../src/prog';
import microbundle from '../src/index';

const exec = promisify(_exec);
const rimraf = promisify(_rimraf);

const FIXTURES_DIR = resolve(`${__dirname}/../test/fixtures`);
Expand All @@ -28,7 +30,7 @@ const parseScript = (() => {
};
})();

export const getBuildScript = async (fixturePath, defaultScript) => {
const getPackageJson = async (fixturePath) => {
let pkg = {};
try {
pkg = JSON.parse(
Expand All @@ -37,9 +39,19 @@ export const getBuildScript = async (fixturePath, defaultScript) => {
} catch (err) {
if (err.code !== 'ENOENT') throw err;
}
return pkg;
};

export const getBuildScript = async (fixturePath, defaultScript) => {
const pkg = await getPackageJson(fixturePath);
return (pkg && pkg.scripts && pkg.scripts.build) || defaultScript;
};

export const getPostBuildScript = async fixturePath => {
const pkg = await getPackageJson(fixturePath);
return pkg && pkg.scripts && pkg.scripts.postbuild;
};

export const buildDirectory = async fixtureDir => {
let fixturePath = resolve(FIXTURES_DIR, fixtureDir);
if (fixtureDir.endsWith('-with-cwd')) {
Expand All @@ -65,6 +77,11 @@ export const buildDirectory = async fixtureDir => {
cwd: parsedOpts.cwd !== '.' ? parsedOpts.cwd : resolve(fixturePath),
});

const postBuildScript = await getPostBuildScript(fixturePath);
if (postBuildScript) {
await exec(postBuildScript);
}

process.chdir(prevDir);

return output;
Expand Down