You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: site/jekyll/bundling/webpack.md
+32-26Lines changed: 32 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,71 +3,73 @@ layout: docs
3
3
title: Webpack
4
4
---
5
5
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
-
8
6
[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).
9
7
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.
11
9
12
10
```javascript
13
11
// Content/components/index.js
14
12
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
+
exportAvatarfrom'./Avatar';
15
+
exportCommentfrom'./Comment';
16
+
exportCommentsBoxfrom'./CommentsBox';
21
17
```
22
18
23
19
```javascript
24
20
// Content/server.js
25
21
26
22
// All JavaScript in here will be loaded server-side
27
23
// Expose components globally so ReactJS.NET can use them
28
-
var Components =require('expose-loader?Components!./components');
24
+
importComponentsfrom'./components';
25
+
26
+
global.Components= Components;
29
27
```
30
28
31
29
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:
32
30
33
31
```javascript
32
+
// This example still uses CommonJS syntax because Node hasn't yet shipped support for ES6 module syntax at the time of writing
34
33
var path =require('path');
35
34
36
35
module.exports= {
37
-
context:path.join(__dirname, 'Content'),
38
36
entry: {
39
-
server:'./server',
40
-
client:'./client',
37
+
server:'./Content/server.js',
38
+
client:'./Content/client.js',
41
39
},
42
40
output: {
43
-
path:path.join(__dirname, 'build'),
44
-
filename:'[name].bundle.js',
41
+
filename:'./wwwroot/[name].bundle.js',
45
42
},
46
43
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
+
},
50
50
],
51
51
},
52
52
resolve: {
53
53
// Allow require('./blah') to require blah.jsx
54
54
extensions: ['', '.js', '.jsx'],
55
55
},
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
-
},
61
56
};
62
57
```
63
58
64
59
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.
65
60
66
-
Our configuration also requires installation of the "expose" and "jsx" loaders:
61
+
Our configuration also requires installation of the "babel" loader:
67
62
68
63
```
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
+
}
71
73
```
72
74
73
75
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
86
88
```
87
89
88
90
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