|
1 | 1 | # Webpack Configurations
|
2 | 2 | Standard configurations for [Webpack](https://webpack.js.org/).
|
3 | 3 |
|
| 4 | +[Example](#example) |
| 5 | + |
4 | 6 | **Why?** — Webpack is the heart of our build pipeline for ReactJS
|
5 | 7 | applications. A proper Webpack configurations is necessary for many features in
|
6 | 8 | our code, and it has a serious impact on the performance of the compiled code.
|
@@ -119,3 +121,52 @@ mutation of the config object.
|
119 | 121 | - [OptimizeCssAssetsPlugin](https://www.npmjs.com/package/optimize-css-assets-webpack-plugin);
|
120 | 122 | - [UglifyJsPlugin](https://webpack.js.org/plugins/uglifyjs-webpack-plugin/).
|
121 | 123 |
|
| 124 | +### Example |
| 125 | +Say, you want to setup Webpack configuration for development build in a new |
| 126 | +project. Create the following configuration file inside `config/webpack` folder |
| 127 | +(the exact folder is just a convention we like to use across the projects): |
| 128 | +```js |
| 129 | +// config/webpack/development.js |
| 130 | + |
| 131 | +/* Note that config JS files are not processed by Babel, thus you should use |
| 132 | + * only the subset of JS syntax natively understood by the current Node version. */ |
| 133 | +const configFactory = require('topcoder-react-utils/config/webpack/development'); |
| 134 | +const path = require('path'); |
| 135 | + |
| 136 | +const standardConfig = configFactory({ |
| 137 | + /* To resolve all paths in the config relative to the root folder of your code. */ |
| 138 | + context: path.resolve(__dirname, '../..'), |
| 139 | + |
| 140 | + /* Entry point of you code. You can also pass in JS object with keys naming |
| 141 | + * the separate bundles you want to pack, and the values specifying their |
| 142 | + * entry points. */ |
| 143 | + entry: './src', |
| 144 | + |
| 145 | + /* You can use "publicPath" option here if you want to serve generated |
| 146 | + * assets from a non-root folder of your server. */ |
| 147 | +}); |
| 148 | + |
| 149 | +/* Here you can make additional modifications of the config, if you need to, |
| 150 | + * using "webpack-merge" package, or in some custom way. */ |
| 151 | + |
| 152 | +module.exports = standardConfig; |
| 153 | +``` |
| 154 | + |
| 155 | +To wire this config to your NPM build script, create another simple file in |
| 156 | +the root of your code: |
| 157 | +```js |
| 158 | +// webpack.config.js |
| 159 | + |
| 160 | +/* eslint-disable global-require */ |
| 161 | +/* eslint-disable import/no-dynamic-require */ |
| 162 | + |
| 163 | +module.exports = function buildConfig(env) { |
| 164 | + return require(`./config/webpack/${env}.js`); |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +Now you can define your NPM build script, using the necessary config, like so |
| 169 | +(the clean step is handy, it should be defined as a separate `clean` script): |
| 170 | +``` |
| 171 | +"build:dev": "npm run clean && ./node_modules/.bin/webpack --env=development --progress --profile --colors --display-optimization-bailout" |
| 172 | +``` |
0 commit comments