Skip to content

Commit ddf30dd

Browse files
authored
Update webpack.md with regarding url-loader, multi-compiler (#2748)
* Update webpack.md with regarding url-loader And made the file locations more explicit. * Update webpack.md
1 parent 44f98fe commit ddf30dd

File tree

1 file changed

+49
-46
lines changed

1 file changed

+49
-46
lines changed

docs/webpack.md

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ if you do need to customize or add a new loader, this is where you would go.
1414

1515
Here is how you can modify webpack configuration:
1616

17+
You might add separate files to keep your code more organized.
1718
```js
1819
// config/webpack/custom.js
1920
module.exports = {
@@ -27,7 +28,11 @@ module.exports = {
2728
}
2829
}
2930
}
31+
```
32+
33+
Then `require` this file in your `config/webpack/environment.js`:
3034

35+
```js
3136
// config/webpack/environment.js
3237
const { environment } = require('@rails/webpacker')
3338
const customConfig = require('./custom')
@@ -38,6 +43,8 @@ environment.config.set('output.filename', '[name].js')
3843

3944
// Merge custom config
4045
environment.config.merge(customConfig)
46+
47+
// Merge other options
4148
environment.config.merge({ devtool: 'none' })
4249

4350
// Delete a property
@@ -132,6 +139,7 @@ module.exports = environment
132139
To use react svg loader, you should append svg loader before file loader:
133140

134141
```js
142+
// config/webpack/environment.js
135143
const { environment } = require('@rails/webpacker')
136144

137145
const babelLoader = environment.loaders.get('babel')
@@ -155,61 +163,31 @@ fileLoader.exclude = /\.(svg)$/i
155163

156164
### Url Loader
157165

166+
Be sure to add the default options from the file loader, as those are applied with the file loader if the size is greater than the `limit`.
167+
158168
```js
159-
// config/webpack/loaders/url.js
169+
// config/webpack/environment.js
160170

161-
module.exports = {
171+
const { environment } = require('@rails/webpacker');
172+
const rules = environment.loaders;
173+
174+
const urlFileSizeCutover = 10000;
175+
const urlLoaderOptions = Object.assign({ limit: urlFileSizeCutover }, fileLoader.use[0].options);
176+
const urlLoader = {
162177
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
163-
use: [{
178+
use: {
164179
loader: 'url-loader',
165-
options: {
166-
limit: 10000,
167-
name: '[name]-[hash].[ext]'
168-
}
169-
}]
170-
}
171-
172-
// config/webpack/environment.js
173-
174-
const { environment } = require('@rails/webpacker')
175-
const url = require('./loaders/url')
180+
options: urlLoaderOptions,
181+
},
182+
};
176183

177-
environment.loaders.prepend('url', url)
184+
environment.loaders.prepend('url', urlLoader)
178185

179186
// avoid using both file and url loaders
187+
// Note, this list should take into account the config value for static_assets_extensions
180188
environment.loaders.get('file').test = /\.(tiff|ico|svg|eot|otf|ttf|woff|woff2)$/i
181189
```
182190

183-
### Overriding Loader Options in webpack 3+ (for CSS Modules etc.)
184-
185-
In webpack 3+, if you'd like to specify additional or different options for a loader, edit `config/webpack/environment.js` and provide an options object to override. This is similar to the technique shown above, but the following example shows specifically how to apply CSS Modules, which is what you may be looking for:
186-
187-
```javascript
188-
const { environment } = require('@rails/webpacker')
189-
const merge = require('webpack-merge')
190-
191-
const myCssLoaderOptions = {
192-
modules: {
193-
localIdentName: '[name]__[local]___[hash:base64:5]'
194-
},
195-
sourceMap: true,
196-
}
197-
198-
const CSSLoader = environment.loaders.get('sass').use.find(el => el.loader === 'css-loader')
199-
200-
CSSLoader.options = merge(CSSLoader.options, myCssLoaderOptions)
201-
202-
module.exports = environment
203-
```
204-
205-
See [issue #756](https://github.com/rails/webpacker/issues/756#issuecomment-327148547) for additional discussion of this.
206-
207-
For this to work, don't forget to use the `stylesheet_pack_tag`, for example:
208-
209-
```
210-
<%= stylesheet_pack_tag 'YOUR_PACK_NAME_HERE' %>
211-
```
212-
213191
## Plugins
214192

215193
The process for adding or modifying webpack plugins is the same as the process
@@ -253,7 +231,7 @@ const { environment } = require('@rails/webpacker')
253231
environment.resolvedModules.append('vendor', 'vendor')
254232
```
255233

256-
### Add SplitChunks (Webpack V4)
234+
### Add SplitChunks (Webpack V4+)
257235
Originally, chunks (and modules imported inside them) were connected by a parent-child relationship in the internal webpack graph. The CommonsChunkPlugin was used to avoid duplicated dependencies across them, but further optimizations were not possible.
258236

259237
Since webpack v4, the CommonsChunkPlugin was removed in favor of optimization.splitChunks.
@@ -310,3 +288,28 @@ You can preload your assets with the `preload_pack_asset` helper if you have Rai
310288
**Warning:** You don't want to preload the css, you want to preload the fonts and images inside the css so that fonts, css, and images can all be downloaded in parallel instead of waiting for the browser to parse the css.
311289

312290
More detailed guides available here: [webpack guides](https://webpack.js.org/guides/)
291+
292+
## Webpack Multi-Compiler and Server-Side Rendering
293+
You can export an Array of Object to have both `bin/webpack` and `bin/webpack-dev-server`
294+
use multiple configurations. This is commonly done for React server-side rendering (SSR).
295+
296+
For an example of this, see the configuration within the [`/config/webpack` dir of the React on Rails Example](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/config/webpack).
297+
298+
Take special care in that you need to make a deep copy of the output from the the basic "client" configuration.
299+
300+
In the example below, you _cannot_ modify the clientConfigObject as that would mutate the "environment" that is global.
301+
302+
```js
303+
const environment = require('./environment');
304+
305+
// make a deep copy
306+
const clientConfigObject = environment.toWebpackConfig();
307+
const serverWebpackConfig = merge({}, clientConfigObject);
308+
309+
// make whatever changes you want for the serverWebpackConfig
310+
311+
// No splitting of chunks for a server bundle
312+
serverWebpackConfig.optimization = {
313+
minimize: false,
314+
};
315+
```

0 commit comments

Comments
 (0)