Skip to content

Commit 3556f19

Browse files
Update webpack docs
1 parent bb39819 commit 3556f19

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

site/jekyll/bundling/webpack.md

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,73 @@ layout: docs
33
title: Webpack
44
---
55

6-
This guide is for Webpack 1. To see the latest example for how to use webpack, check out the [sample project](https://github.com/reactjs/React.NET/tree/master/src/React.Sample.Webpack.CoreMvc).
7-
86
[Webpack](http://webpack.github.io/docs/what-is-webpack.html) is a popular module bundling system built on top of Node.js. It can handle not only combination and minification of JavaScript and CSS files, but also other assets such as image files (spriting) through the use of plugins. Webpack can be used as an alternative to Cassette or ASP.NET Combination and Minification. This guide assumes you have already [installed Webpack](http://webpack.github.io/docs/installation.html).
97

10-
In order to use Webpack with ReactJS.NET's server-side rendering, it is suggested that you create a separate bundle ("[entry point](http://webpack.github.io/docs/multiple-entry-points.html)") containing _only_ the code required to perform server-side rendering. Any components you would like to render server-side must be exposed globally so that ReactJS.NET can access them. This can be done through the [Webpack expose loader](https://github.com/webpack/expose-loader):
8+
In order to use Webpack with ReactJS.NET's server-side rendering, it is suggested that you create a separate bundle ("[entry point](http://webpack.github.io/docs/multiple-entry-points.html)") containing _only_ the code required to perform server-side rendering. Any components you would like to render server-side must be exposed globally so that ReactJS.NET can access them. This can be done by assigning properties to `global`. Even though `global` is not available in the browser, Webpack will add a shim that makes any assigned properties available in the global Javascript scope.
119

1210
```javascript
1311
// Content/components/index.js
1412

15-
module.exports = {
16-
// All the components you'd like to render server-side
17-
Avatar: require('./Avatar'),
18-
Comment: require('./Comment'),
19-
CommentsBox: require('./CommentsBox'),
20-
};
13+
// All the components you'd like to render server-side
14+
export Avatar from './Avatar';
15+
export Comment from './Comment';
16+
export CommentsBox from './CommentsBox';
2117
```
2218

2319
```javascript
2420
// Content/server.js
2521

2622
// All JavaScript in here will be loaded server-side
2723
// Expose components globally so ReactJS.NET can use them
28-
var Components = require('expose-loader?Components!./components');
24+
import Components from './components';
25+
26+
global.Components = Components;
2927
```
3028

3129
The next step is to modify the `webpack.config.js` so that it creates a bundle from `Content/server.js`. A config similar to the following could work as a good starting point:
3230

3331
```javascript
32+
// This example still uses CommonJS syntax because Node hasn't yet shipped support for ES6 module syntax at the time of writing
3433
var path = require('path');
3534

3635
module.exports = {
37-
context: path.join(__dirname, 'Content'),
3836
entry: {
39-
server: './server',
40-
client: './client',
37+
server: './Content/server.js',
38+
client: './Content/client.js',
4139
},
4240
output: {
43-
path: path.join(__dirname, 'build'),
44-
filename: '[name].bundle.js',
41+
filename: './wwwroot/[name].bundle.js',
4542
},
4643
module: {
47-
loaders: [
48-
// Transform JSX in .jsx files
49-
{ test: /\.jsx$/, loader: 'jsx-loader?harmony' },
44+
rules: [
45+
{
46+
test: /\.jsx?$/,
47+
exclude: /node_modules/,
48+
loader: 'babel-loader',
49+
},
5050
],
5151
},
5252
resolve: {
5353
// Allow require('./blah') to require blah.jsx
5454
extensions: ['', '.js', '.jsx'],
5555
},
56-
externals: {
57-
// Use external version of React (from CDN for client-side, or
58-
// bundled with ReactJS.NET for server-side)
59-
react: 'React',
60-
},
6156
};
6257
```
6358

6459
This configuration uses two entry points (`Content/server.js` for the server side and `Content/client.js` for the client side) and will create two bundles (`build/server.bundle.js` and `build/client.bundle.js` respectively). Your configuration may be more complex, but generally you should only have a single bundle with all the code required for server-side rendering.
6560

66-
Our configuration also requires installation of the "expose" and "jsx" loaders:
61+
Our configuration also requires installation of the "babel" loader:
6762

6863
```
69-
npm install --save-dev expose-loader
70-
npm install --save-dev jsx-loader
64+
npm install --save-dev babel-loader
65+
```
66+
67+
You will also need a `.babelrc` in the root of your project:
68+
69+
```
70+
{
71+
"presets": ["react", "env"]
72+
}
7173
```
7274

7375
Once Webpack has been configured, run `webpack` to build the bundles. Once you have verified that the bundle is being created correctly, you can modify your ReactJS.NET configuration (normally `App_Start\ReactConfig.cs`) to load the newly-created bundle:
@@ -86,3 +88,7 @@ This will load all your components into the `Components` global, which can be us
8688
```
8789

8890
A full example is available in [the ReactJS.NET repository](https://github.com/reactjs/React.NET/tree/master/src/React.Sample.Webpack.CoreMvc).
91+
92+
### Migrating from expose-loader
93+
94+
Mixing ES6 class syntax with CommonJS `require` has been the source of a lot of confusion, so we no longer recommend using `expose-loader`, `module.exports`, or `require`. Instead, use `import` and `export` statements throughout your whole React codebase when using Webpack. It is still fine to use `require` in webpack's config directly.

0 commit comments

Comments
 (0)