Skip to content

Commit f6d9ebc

Browse files
committed
fix(webpack): use babel-plugin-webpack-loaders for ssr of assets everywhere
1 parent 405fc45 commit f6d9ebc

File tree

10 files changed

+32
-35
lines changed

10 files changed

+32
-35
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ MERN is a scaffolding tool which makes it easy to build isomorphic apps using Mo
2727

2828
### Webpack Configs
2929

30-
MERN uses Webpack for bundling modules. There are three types of Webpack configs provided `webpack.config.dev.js` (for development), `webpack.config.prod.js` (for production) and `webpack.config.server.js` (for bundling server in production).
30+
MERN uses Webpack for bundling modules. There are four types of Webpack configs provided `webpack.config.dev.js` (for development), `webpack.config.prod.js` (for production), `webpack.config.server.js` (for bundling server in production) and `webpack.config.babel.js` (for [babel-plugin-webpack-loaders](https://github.com/istarkov/babel-plugin-webpack-loaders) for server rendering of assets included through webpack).
3131

3232
The Webpack configuration is minimal and beginner-friendly. You can customise and add more features to it for production build.
3333

@@ -100,9 +100,6 @@ This folder contains all the common components which are used throughout the pro
100100
#### index.js
101101
Index.js simply does client side rendering using the data provided from `window.__INITIAL_STATE__`.
102102

103-
#### assets
104-
All the assets images, fonts goes here. All of these will be copied to `dist` folder while running in production and served from there.
105-
106103
#### modules
107104
Modules are the way of organising different domain-specific modules in the project. A typical module contains the following
108105
```
@@ -136,6 +133,9 @@ Modules are the way of organising different domain-specific modules in the proje
136133

137134
## Misc
138135

136+
### Importing Assets
137+
Assets can be kept where you want and can be imported into your js files or css files. Those fill be served by webpack in development mode and copied to the dist folder during production.
138+
139139
### ES6 support
140140
We use babel to transpile code in both server and client with `stage-0` plugin. So, you can use both ES6 and experimental ES7 features.
141141

client/modules/App/components/Footer/Footer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React from 'react';
44
import styles from './Footer.css';
55

66
// Import Images
7-
import bg from 'assets/header-bk.png';
7+
import bg from '../../header-bk.png';
88

99
function Footer() {
1010
return (

client/modules/App/components/Header/Header.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.header {
2-
background: #eee url('assets/header-bk.png') center;
2+
background: #eee url('../../header-bk.png') center;
33
background-size: cover;
44
border-bottom: 1px solid #ccc;
55
}
File renamed without changes.

index.js

-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ if (process.env.NODE_ENV === 'production') {
88
// In production, serve the webpacked server file.
99
require('./dist/server.bundle.js');
1010
} else {
11-
process.env.webpackAssets = JSON.stringify({});
12-
process.env.webpackChunkAssets = JSON.stringify({});
1311
// Babel polyfill to convert ES6 code in runtime
1412
require('babel-register')({
1513
"plugins": [
@@ -24,11 +22,5 @@ if (process.env.NODE_ENV === 'production') {
2422
});
2523
require('babel-polyfill');
2624

27-
// CSS modules hook to inject css-modules classes in the final html.
28-
require('css-modules-require-hook')({
29-
generateScopedName: '[name]__[local]__[hash:base64:5]',
30-
devMode: true
31-
});
32-
3325
require('./server/server');
3426
}

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"start:prod": "cross-env NODE_ENV=production node index.js",
1212
"bs": "npm run clean && npm run build && npm run build:server && npm run start:prod",
1313
"build": "cross-env NODE_ENV=production webpack --config webpack.config.prod.js",
14-
"build:server": "cross-env NODE_ENV=production webpack --config webpack.config.server.js",
14+
"build:server": "cross-env BABEL_DISABLE_CACHE=1 NODE_ENV=production webpack --config webpack.config.server.js",
1515
"clean": "rimraf dist",
1616
"slate": "rimraf node_modules && npm install",
1717
"lint": "eslint client server"
@@ -53,7 +53,6 @@
5353
"ava": "^0.15.2",
5454
"babel-eslint": "^6.0.4",
5555
"babel-loader": "^6.2.4",
56-
"babel-plugin-css-modules-transform": "^0.1.1",
5756
"babel-plugin-webpack-loaders": "^0.5.0",
5857
"babel-polyfill": "^6.9.1",
5958
"babel-preset-es2015": "^6.9.0",
@@ -75,7 +74,6 @@
7574
"eslint-plugin-jsx-a11y": "^1.3.0",
7675
"eslint-plugin-react": "^5.1.1",
7776
"extract-text-webpack-plugin": "^1.0.1",
78-
"fake-url-loader": "^1.0.1",
7977
"file-loader": "^0.8.5",
8078
"jsdom": "^9.2.1",
8179
"json-loader": "^0.5.4",

server/server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ const renderFullPage = (html, initialState) => {
5858
const head = Helmet.rewind();
5959

6060
// Import Manifests
61-
const assetsManifest = JSON.parse(process.env.webpackAssets);
62-
const chunkManifest = JSON.parse(process.env.webpackChunkAssets);
61+
const assetsManifest = process.env.webpackAssets && JSON.parse(process.env.webpackAssets);
62+
const chunkManifest = process.env.webpackChunkAssets && JSON.parse(process.env.webpackChunkAssets);
6363

6464
return `
6565
<!doctype html>

webpack.config.babel.js

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
var cssnext = require('postcss-cssnext');
2+
var postcssFocus = require('postcss-focus');
3+
var postcssReporter = require('postcss-reporter');
4+
15
module.exports = {
26
output: {
7+
publicPath: '/',
38
libraryTarget: 'commonjs2',
49
},
510
resolve: {
@@ -11,10 +16,24 @@ module.exports = {
1116
},
1217
module: {
1318
loaders: [
19+
{
20+
test: /\.css$/,
21+
exclude: /node_modules/,
22+
loader: 'style-loader!css-loader?localIdentName=[name]__[local]__[hash:base64:5]&modules&importLoaders=1&sourceMap!postcss-loader',
23+
},
1424
{
1525
test: /\.jpe?g$|\.gif$|\.png$|\.svg$/i,
1626
loader: 'url-loader?limit=10000',
1727
},
1828
],
1929
},
30+
postcss: () => [
31+
postcssFocus(),
32+
cssnext({
33+
browsers: ['last 2 versions', 'IE > 10'],
34+
}),
35+
postcssReporter({
36+
clearMessages: true,
37+
}),
38+
],
2039
};

webpack.config.prod.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = {
5151
loader: 'babel',
5252
}, {
5353
test: /\.jpe?g$|\.gif$|\.png$|\.svg$/i,
54-
loader: 'url-loader?limit=10000&name=assets/[name].[ext]',
54+
loader: 'url-loader?limit=10000',
5555
}, {
5656
test: /\.json$/,
5757
loader: 'json-loader',
@@ -74,13 +74,6 @@ module.exports = {
7474
new ManifestPlugin({
7575
basePath: '/',
7676
}),
77-
new CopyWebpackPlugin([
78-
{
79-
context: __dirname + '/client/assets',
80-
from: '**/*',
81-
to: __dirname + '/dist/assets',
82-
},
83-
]),
8477
new ChunkManifestPlugin({
8578
filename: "chunk-manifest.json",
8679
manifestVariable: "webpackManifest",

webpack.config.server.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,16 @@ module.exports = {
4040
],
4141
plugins: [
4242
[
43-
'css-modules-transform', {
44-
generateScopedName: '[hash:base64]',
43+
'babel-plugin-webpack-loaders', {
44+
'config': './webpack.config.babel.js',
45+
"verbose": false
4546
}
4647
]
4748
]
4849
},
4950
}, {
5051
test: /\.json$/,
5152
loader: 'json-loader',
52-
}, {
53-
test: /\.css$/,
54-
loader: 'null-loader',
55-
}, {
56-
test: /\.jpe?g$|\.gif$|\.png$|\.svg$/i,
57-
loader: 'fake-url-loader?limit=10000&name=assets/[name].[ext]',
5853
},
5954
],
6055
},

0 commit comments

Comments
 (0)