Skip to content

Commit 7b09c2e

Browse files
authored
README.md: server-side rendering example.
1 parent 630579d commit 7b09c2e

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,73 @@ module.exports = {
491491
}
492492
```
493493

494+
### Server-side rendering
495+
496+
Suppose that you want to create a webpack build of your [isomorphic](https://www.lullabot.com/articles/what-is-an-isomorphic-application)/[universal](http://www.acuriousanimal.com/2016/08/10/universal-applications.html) web application that pre-renders pages on the server side. Your app is written in something like ECMA6 and SASS and requires transpilation, and also has CSS dependencies, such as `bootstrap.min.css`, installed from `node_modules`.
497+
498+
There are several caveats in setting up webpack and css-loader for server-side environment:
499+
500+
1) As of February, 2018, `style-loader` [is not isomorphic](https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/503), it doesn't work in server-side node.js because it refers to `window` global when trying to inline your `css` into the javascript bundle. So, when building you webpack bundle on server side, you HAVE to use [extract-text-webpack-plugin](https://github.com/webpack-contrib/extract-text-webpack-plugin) to extract styles into a separate file (and [you shouldn't even fallback to ExtractTextPlugin](https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/503) as it was suggested in previous section). `css-loader` should pass the extracted css straight to `ExtractTextPlugin`.
501+
2) You have to set [target: node](https://webpack.js.org/concepts/targets/#usage) for webpack to keep standard node.js modules outside of the bundle.
502+
3) You have to use (webpack-node-externals)[https://github.com/liady/webpack-node-externals] to keep your javascript dependencies from `node_modules` outside of the webpack bundle. Webpack bundle will just `require()` them straight from `node_modules` folder.
503+
4) However, with default `webpack-node-externals` setup, css that resides in `node_modules` will also get excluded from the bundle and webpack bundle will just try to `require()` them as if they were a javascript, resulting in `SyntaxError`. Thus, you have to [whitelist any non-javascript dependencies](https://github.com/liady/webpack-node-externals#how-can-i-bundle-required-assets-ie-css-files-from-node_modules) (including css) that you are importing from `node_modules`.
504+
505+
Here is an example of webpack configuration for server-side rendering:
506+
507+
```js
508+
const path = require('path');
509+
510+
const webpack = require('webpack');
511+
const ExtractTextPlugin = require('extract-text-webpack-plugin');
512+
const nodeExternals = require('webpack-node-externals');
513+
514+
module.exports = {
515+
target: 'node', // don't bundle standard node.js modules such as http or path
516+
externals: [nodeExternals({ // don't bundle dependencies from node_modules folder, except by non-javascript files
517+
whitelist: [ // non-javascript files in node_modules should go to the bundle and be processed by ExtractTextPlugin
518+
/\.(?!(?:jsx?|json)$).{1,5}$/i,
519+
],
520+
})],
521+
entry: path.resolve(__dirname, 'src', 'server', 'app.jsx'),
522+
output: {
523+
path: path.resolve(__dirname, 'dist'),
524+
publicPath: '/dist/',
525+
filename: 'server.js'
526+
},
527+
resolve: {
528+
modules: [path.join(__dirname, 'src'), path.join(__dirname, 'node_modules')]
529+
},
530+
plugins: [
531+
new ExtractTextPlugin('server.css') // extract css and sass files into server.css bundle
532+
],
533+
module: {
534+
rules: [
535+
...
536+
{
537+
test: /\.(scss|sass)$/,
538+
use: ExtractTextPlugin.extract({
539+
use: [
540+
{ loader: 'css-loader', options: {sourceMap: true} },
541+
{ loader: 'sass-loader', options: {sourceMap: true} }
542+
]
543+
})
544+
},
545+
{
546+
test: /\.css$/,
547+
use: ExtractTextPlugin.extract({
548+
use: [
549+
{ loader: 'css-loader', options: {sourceMap: true} }
550+
]
551+
})
552+
},
553+
...
554+
]
555+
}
556+
};
557+
```
558+
559+
560+
494561
<h2 align="center">Maintainers</h2>
495562

496563
<table>

0 commit comments

Comments
 (0)