Skip to content

Commit 101743e

Browse files
authored
Avoid including full Lodash build (#45)
* Transform Lodash imports via Babel plugin (build: 513 kB -> 450 kB)
1 parent a8e2d13 commit 101743e

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

bin/build.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env node
2+
3+
// webpack.config.prod.js checks this.
4+
process.env.NODE_ENV = 'production';
5+
6+
// Load the create-react-app config.
7+
const webpackConfigProd = require( 'react-scripts/config/webpack.config.prod' );
8+
9+
// Modify the config according to our needs.
10+
11+
const babelLoader = webpackConfigProd.module.loaders[ 0 ];
12+
if ( babelLoader.loader !== 'babel' ) {
13+
console.error( webpackConfigProd.module.loaders );
14+
throw new Error( 'Couldn\'t find the babel loader config.' );
15+
}
16+
17+
babelLoader.query.plugins = ( babelLoader.query.plugins || [] )
18+
.filter( pluginName => pluginName !== 'lodash' )
19+
.concat( 'lodash' );
20+
21+
console.log( 'Added lodash babel plugin to build' );
22+
23+
// Run the build.
24+
require( 'react-scripts/scripts/build' );

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"devDependencies": {
66
"babel-eslint": "^7.1.0",
7+
"babel-plugin-lodash": "^3.2.9",
78
"deep-freeze": "0.0.1",
89
"eslint": "^3.9.1",
910
"eslint-plugin-react": "^6.6.0",
@@ -33,10 +34,10 @@
3334
},
3435
"scripts": {
3536
"start": "react-scripts start",
36-
"build": "npm run lint && react-scripts build",
37+
"build": "npm run lint && node bin/build.js",
3738
"test": "npm run lint && react-scripts test --env=jsdom",
3839
"eject": "react-scripts eject",
39-
"lint": "eslint --ext js,jsx .eslintrc.js src/",
40+
"lint": "eslint --ext js,jsx .eslintrc.js src/ bin/",
4041
"deploy": "npm run build && surge -p ./build -d wpconsole.surge.sh"
4142
}
4243
}

0 commit comments

Comments
 (0)