Skip to content

Commit bd4c8b2

Browse files
committed
A fix of Webpack config + update of its docs with an example of use
1 parent 05674ec commit bd4c8b2

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

config/webpack/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const webpack = require('webpack');
4343
module.exports = function configFactory(ops) {
4444
const o = _.defaults(_.clone(ops), {
4545
cssLocalIdent: '[hash:base64:6]',
46+
publicPath: '',
4647
});
4748

4849
/* Stores misc build info into the local ".build-info" file in the context

docs/webpack-config.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Webpack Configurations
22
Standard configurations for [Webpack](https://webpack.js.org/).
33

4+
[Example](#example)
5+
46
**Why?** — Webpack is the heart of our build pipeline for ReactJS
57
applications. A proper Webpack configurations is necessary for many features in
68
our code, and it has a serious impact on the performance of the compiled code.
@@ -119,3 +121,52 @@ mutation of the config object.
119121
- [OptimizeCssAssetsPlugin](https://www.npmjs.com/package/optimize-css-assets-webpack-plugin);
120122
- [UglifyJsPlugin](https://webpack.js.org/plugins/uglifyjs-webpack-plugin/).
121123

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

Comments
 (0)