Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cf78ade

Browse files
committedJan 28, 2018
Create bundle stats with new build script
As discussed in facebook#1858, we'd like to make it easy for users to view bundle stats without ejecting. This PR adds a new script that, along with building the site for production, creates a `build-stats.json` file. This file can be used with tools like Webpack Bundle Analyzer or Webpack Visualizer, to help the user optimize their bundles. Conflicts: package.json packages/react-scripts/package.json
1 parent 75f8e7d commit cf78ade

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed
 

Diff for: ‎package.json

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
],
66
"scripts": {
77
"build": "cd packages/react-scripts && node bin/react-scripts.js build",
8+
"build:with-stats":
9+
"cd packages/react-scripts && node scripts/build.js --stats",
810
"changelog": "lerna-changelog",
911
"create-react-app": "node tasks/cra.js",
1012
"e2e": "tasks/e2e-simple.sh",

Diff for: ‎packages/react-scripts/bin/react-scripts.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ process.on('unhandledRejection', err => {
1818
const spawn = require('react-dev-utils/crossSpawn');
1919
const args = process.argv.slice(2);
2020

21-
const scriptIndex = args.findIndex(
22-
x => x === 'build' || x === 'eject' || x === 'start' || x === 'test'
23-
);
21+
const scriptKeys = ['build', 'build:with-stats', 'eject', 'start', 'test'];
22+
const scriptIndex = args.findIndex(x => scriptKeys.includes(x));
23+
2424
const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
2525
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
2626

2727
switch (script) {
2828
case 'build':
29+
case 'build:with-stats':
2930
case 'eject':
3031
case 'start':
3132
case 'test': {

Diff for: ‎packages/react-scripts/package.json

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "react-scripts",
3-
"version": "1.0.17",
3+
"version": "1.1.0",
44
"description": "Configuration and scripts for Create React App.",
5-
"repository": "facebookincubator/create-react-app",
5+
"repository": "facebook/create-react-app",
66
"license": "MIT",
77
"engines": {
88
"node": ">=6"
99
},
1010
"bugs": {
11-
"url": "https://github.com/facebookincubator/create-react-app/issues"
11+
"url": "https://github.com/facebook/create-react-app/issues"
1212
},
1313
"files": [
1414
"bin",
@@ -21,21 +21,22 @@
2121
"react-scripts": "./bin/react-scripts.js"
2222
},
2323
"dependencies": {
24-
"@babel/core": "7.0.0-beta.37",
25-
"@babel/runtime": "7.0.0-beta.37",
24+
"@babel/core": "7.0.0-beta.38",
25+
"@babel/runtime": "7.0.0-beta.38",
2626
"autoprefixer": "7.2.5",
2727
"babel-core": "7.0.0-bridge.0",
2828
"babel-eslint": "8.2.1",
29-
"babel-jest": "22.0.6",
29+
"babel-jest": "22.1.0",
3030
"babel-loader": "8.0.0-beta.0",
31-
"babel-preset-react-app": "^3.1.0",
31+
"babel-preset-react-app": "^3.1.1",
32+
"bfj": "^5.2.0",
3233
"case-sensitive-paths-webpack-plugin": "2.1.1",
3334
"chalk": "2.3.0",
34-
"css-loader": "0.28.8",
35+
"css-loader": "0.28.9",
3536
"dotenv": "4.0.0",
3637
"dotenv-expand": "4.0.1",
3738
"eslint": "4.15.0",
38-
"eslint-config-react-app": "^2.0.1",
39+
"eslint-config-react-app": "^2.1.0",
3940
"eslint-loader": "1.9.0",
4041
"eslint-plugin-flowtype": "2.41.0",
4142
"eslint-plugin-import": "2.8.0",
@@ -45,14 +46,16 @@
4546
"file-loader": "1.1.6",
4647
"fs-extra": "5.0.0",
4748
"html-webpack-plugin": "2.30.1",
48-
"jest": "22.0.6",
49+
"identity-obj-proxy": "3.0.0",
50+
"jest": "22.1.2",
4951
"object-assign": "4.1.1",
5052
"postcss-flexbugs-fixes": "3.2.0",
5153
"postcss-loader": "2.0.10",
5254
"promise": "8.0.1",
5355
"raf": "3.4.0",
54-
"react-dev-utils": "^4.2.1",
56+
"react-dev-utils": "^5.0.0",
5557
"style-loader": "0.19.1",
58+
"svgr": "1.6.0",
5659
"sw-precache-webpack-plugin": "0.11.4",
5760
"thread-loader": "1.1.2",
5861
"uglifyjs-webpack-plugin": "1.1.6",
@@ -67,6 +70,19 @@
6770
"react-dom": "^16.0.0"
6871
},
6972
"optionalDependencies": {
70-
"fsevents": "1.1.2"
73+
"fsevents": "1.1.3"
74+
},
75+
"browserslist": {
76+
"development": [
77+
"last 2 chrome versions",
78+
"last 2 firefox versions",
79+
"last 2 edge versions"
80+
],
81+
"production": [
82+
">1%",
83+
"last 4 versions",
84+
"Firefox ESR",
85+
"not ie < 11"
86+
]
7187
}
7288
}

Diff for: ‎packages/react-scripts/scripts/build.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const path = require('path');
3333
const chalk = require('chalk');
3434
const fs = require('fs-extra');
3535
const webpack = require('webpack');
36+
const bfj = require('bfj');
3637
const config = require('../config/webpack.config.prod');
3738
const paths = require('../config/paths');
3839
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
@@ -55,6 +56,10 @@ if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
5556
process.exit(1);
5657
}
5758

59+
// Process CLI arguments
60+
const argv = process.argv.slice(2);
61+
const writeStatsJson = argv.indexOf('--stats') !== -1;
62+
5863
// First, read the current file sizes in build directory.
5964
// This lets us display how much they changed later.
6065
measureFileSizesBeforeBuild(paths.appBuild)
@@ -148,11 +153,20 @@ function build(previousFileSizes) {
148153
);
149154
return reject(new Error(messages.warnings.join('\n\n')));
150155
}
151-
return resolve({
156+
157+
const resolveArgs = {
152158
stats,
153159
previousFileSizes,
154160
warnings: messages.warnings,
155-
});
161+
};
162+
if (writeStatsJson) {
163+
return bfj
164+
.write('./build/build-stats.json', stats.toJson())
165+
.then(() => resolve(resolveArgs))
166+
.catch(error => reject(new Error(error)));
167+
}
168+
169+
return resolve(resolveArgs);
156170
});
157171
});
158172
}

0 commit comments

Comments
 (0)
Please sign in to comment.