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: README.md
+67Lines changed: 67 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -491,6 +491,73 @@ module.exports = {
491
491
}
492
492
```
493
493
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:
0 commit comments