|
| 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). |
0 commit comments