Skip to content

Commit 783b4e3

Browse files
committed
Add basic guide for using Webpack with ReactJS.NET
1 parent 5fbf250 commit 783b4e3

File tree

4 files changed

+93
-7
lines changed

4 files changed

+93
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Features
1717
libraries:
1818
* [ASP.NET Bundling and Minification](http://reactjs.net/guides/weboptimizer.html)
1919
* [Cassette](http://reactjs.net/guides/cassette.html)
20+
* [Webpack](http://reactjs.net/guides/webpack.html)
2021
* [Server-side component rendering](http://reactjs.net/guides/server-side-rendering.html)
2122
to make your initial render super-fast (experimental!)
2223
* [Runs on Linux](http://reactjs.net/guides/mono.html) via Mono and V8

site/jekyll/guides/cassette.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,8 @@ project for an example.
4747
Precompilation
4848
==============
4949

50-
**New in ReactJS.NET 0.2.0**
51-
5250
Cassette supports using an MSBuild task to minify and combine your assets before
53-
deployment. This makes the start time of your application a lot quicker, and is
54-
also required to use JSX files on Linux via Mono (as the JSX transformer does
55-
not currently support Mono).
56-
57-
Refer to the Cassette
51+
deployment. This makes the start time of your application a lot quicker. Refer
52+
to the Cassette
5853
[compile-time bundle generation using MSBuild](http://getcassette.net/documentation/v2/msbuild)
5954
documentation for more information.

site/jekyll/guides/webpack.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
layout: docs
3+
title: Webpack
4+
---
5+
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).
7+
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 through the [Webpack expose loader](https://github.com/webpack/expose-loader):
9+
10+
```javascript
11+
// Content/components/index.js
12+
13+
module.exports = {
14+
// All the components you'd like to render server-side
15+
Avatar: require('./Avatar'),
16+
Comment: require('./Comment'),
17+
CommentsBox: require('./CommentsBox')
18+
};
19+
```
20+
```javascript
21+
// Content/server.js
22+
23+
// All JavaScript in here will be loaded server-side
24+
// Expose components globally so ReactJS.NET can use them
25+
var Components = require('expose?Components!./components');
26+
```
27+
28+
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:
29+
30+
```javascript
31+
var path = require('path');
32+
33+
module.exports = {
34+
context: path.join(__dirname, 'Content'),
35+
entry: {
36+
server: './server',
37+
client: './client'
38+
},
39+
output: {
40+
path: path.join(__dirname, 'build'),
41+
filename: '[name].bundle.js'
42+
},
43+
module: {
44+
loaders: [
45+
// Transform JSX in .jsx files
46+
{ test: /\.jsx$/, loader: 'jsx-loader?harmony' }
47+
],
48+
},
49+
resolve: {
50+
// Allow require('./blah') to require blah.jsx
51+
extensions: ['', '.js', '.jsx']
52+
},
53+
externals: {
54+
// Use external version of React (from CDN for client-side, or
55+
// bundled with ReactJS.NET for server-side)
56+
react: 'React'
57+
}
58+
};
59+
```
60+
61+
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.
62+
63+
Our configuration also requires installation of the "expose" and "jsx" loaders:
64+
65+
```
66+
npm install --save-dev expose-loader
67+
npm install --save-dev jsx-loader
68+
```
69+
70+
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:
71+
72+
```csharp
73+
ReactSiteConfiguration.Configuration
74+
.AddScript("~/build/server.bundle.js");
75+
```
76+
77+
This will load all your components into the `Components` global, which can be used from `Html.React` to render any of the components:
78+
79+
```csharp
80+
@Html.React("Components.CommentsBox", new {
81+
initialComments = Model.Comments
82+
})
83+
```
84+
85+
A full example is available in [the ReactJS.NET repository](https://github.com/reactjs/React.NET/tree/master/src/React.Sample.Webpack).

site/jekyll/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ var HelloWorld = React.createClass({
7070
Reference your JSX files and they will be included in your bundles
7171
along with your other JavaScript files.
7272
</p>
73+
<p>
74+
If you're a fan of Node.js-based libraries, you can use
75+
[Webpack](/guides/webpack.html) or Browserify instead, and still
76+
take advantage of ReactJS.NET's server-side rendering.
77+
</p>
7378
</div>
7479
<div class="example-code">
7580

0 commit comments

Comments
 (0)