Skip to content

Commit ffe1f5e

Browse files
committed
fix(webpack): proper isomorphic assets loading
1 parent afe63c8 commit ffe1f5e

13 files changed

+66
-31
lines changed

.babelrc

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@
44
"env": {
55
"production": {
66
"presets": ["es2015", "react", "react-optimize", "es2015-native-modules", "stage-0"]
7+
},
8+
"development": {
9+
"plugins": [
10+
[
11+
"babel-plugin-webpack-loaders",
12+
{
13+
"config": "./webpack.config.babel.js",
14+
"verbose": false
15+
}
16+
]
17+
]
18+
},
19+
"test": {
20+
"plugins": [
21+
[
22+
"babel-plugin-webpack-loaders",
23+
{
24+
"config": "${CONFIG}",
25+
"verbose": false
26+
}
27+
]
28+
]
729
}
830
}
931
}

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
webpack.config.dev.js
22
webpack.config.prod.js
33
webpack.config.server.js
4+
webpack.config.babel.js
45
index.js
56
server/tests/

.eslintrc

+2-8
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@
2222
"no-underscore-dangle" : 0,
2323
"max-len": [1, 180, 4],
2424
"arrow-body-style": [0],
25-
"react/jsx-no-bind": [0]
26-
},
27-
"settings": {
28-
"import/resolver": {
29-
"webpack": {
30-
"config": "./webpack.config.dev.js"
31-
}
32-
}
25+
"react/jsx-no-bind": [0],
26+
"import/no-unresolved": [0] // Until import plugin supports webpack 2 resolveModules
3327
}
3428
}

client/modules/App/components/Footer/Footer.css

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.footer{
22
text-align: center;
33
padding: 56px 0;
4-
background: #FFF url('header-bk.png') center;
54
background-size: cover;
65
}
76

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import React from 'react';
33
// Import Style
44
import styles from './Footer.css';
55

6+
// Import Images
7+
import bg from 'assets/header-bk.png';
8+
69
function Footer() {
710
return (
8-
<div className={styles.footer}>
11+
<div style={{ background: `#FFF url(${bg}) center` }} className={styles.footer}>
912
<p>&copy; 2016 &middot; Hashnode &middot; LinearBytes Inc.</p>
1013
<p>We are on Twitter : <a href="https://twitter.com/@mern_io" target="_Blank">@mern_io</a></p>
1114
</div>

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('header-bk.png') center;
2+
background: #eee url('assets/header-bk.png') center;
33
background-size: cover;
44
border-bottom: 1px solid #ccc;
55
}

index.js

-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ if (process.env.NODE_ENV === 'production') {
66
// In production, serve the webpacked server file.
77
require('./dist/server.bundle.js');
88
} else {
9-
// Ignore css and image files in babel
10-
const noop = () => {
11-
};
12-
require.extensions['.css'] = noop();
13-
149
// Babel polyfill to convert ES6 code in runtime
1510
require('babel-register');
1611
require('babel-polyfill');

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
"version": "1.0.0",
44
"description": "Boilerplate project for building Isomorphic apps using React and Redux",
55
"scripts": {
6-
"test": "cross-env NODE_ENV=test PORT=8080 MONGO_URL=mongodb://localhost:27017/mern-test ava",
6+
"test": "cross-env CONFIG=$(pwd)/webpack.config.babel.js BABEL_DISABLE_CACHE=1 NODE_ENV=test PORT=8080 MONGO_URL=mongodb://localhost:27017/mern-test ava",
77
"watch:test": "npm run test -- --watch",
88
"cover": "nyc npm run test",
99
"check-coverage": "nyc check-coverage --statements 100 --branches 100 --functions 100 --lines 100",
10-
"start": "cross-env NODE_ENV=development nodemon index.js",
10+
"start": "cross-env BABEL_DISABLE_CACHE=1 NODE_ENV=development nodemon index.js",
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": "webpack --config webpack.config.server.js",
14+
"build:server": "cross-env 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"
@@ -54,6 +54,7 @@
5454
"babel-eslint": "^6.0.4",
5555
"babel-loader": "^6.2.4",
5656
"babel-plugin-css-modules-transform": "^0.1.1",
57+
"babel-plugin-webpack-loaders": "^0.5.0",
5758
"babel-polyfill": "^6.9.1",
5859
"babel-preset-es2015": "^6.9.0",
5960
"babel-preset-es2015-native-modules": "^6.6.0",
@@ -70,12 +71,11 @@
7071
"enzyme": "^2.3.0",
7172
"eslint": "^2.11.1",
7273
"eslint-config-airbnb": "^9.0.1",
73-
"eslint-import-resolver-webpack": "^0.3.0",
7474
"eslint-plugin-ava": "^2.4.0",
75-
"eslint-plugin-import": "^1.8.1",
7675
"eslint-plugin-jsx-a11y": "^1.3.0",
7776
"eslint-plugin-react": "^5.1.1",
7877
"extract-text-webpack-plugin": "^1.0.1",
78+
"fake-url-loader": "^1.0.1",
7979
"file-loader": "^0.8.5",
8080
"jsdom": "^9.2.1",
8181
"json-loader": "^0.5.4",

server/util/setup-test-env.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Ignore webpack assets during testing
2-
require.extensions['.css'] = noop => noop;
3-
41
// CSS modules hook to inject css-modules classes in the final html.
52
require('css-modules-require-hook')({
63
generateScopedName: '[name]__[local]__[hash:base64:5]',

webpack.config.babel.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
output: {
3+
libraryTarget: 'commonjs2',
4+
},
5+
resolve: {
6+
extensions: ['', '.js', '.jsx'],
7+
modules: [
8+
'client',
9+
'node_modules',
10+
],
11+
},
12+
module: {
13+
loaders: [
14+
{
15+
test: /\.jpe?g$|\.gif$|\.png$|\.svg$/i,
16+
loader: 'url-loader?limit=10000',
17+
},
18+
],
19+
},
20+
};

webpack.config.dev.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ module.exports = {
2828
resolve: {
2929
extensions: ['', '.js', '.jsx'],
3030
modules: [
31-
'assets',
32-
'components',
33-
'modules',
31+
'client',
3432
'node_modules',
3533
],
3634
},

webpack.config.prod.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ module.exports = {
3030
resolve: {
3131
extensions: ['', '.js', '.jsx'],
3232
modules: [
33-
'assets',
34-
'components',
35-
'modules',
33+
'client',
3634
'node_modules',
3735
],
3836
},

webpack.config.server.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ module.exports = {
1818
__dirname: true,
1919
},
2020

21+
resolve: {
22+
extensions: ['', '.js', '.jsx'],
23+
modules: [
24+
'client',
25+
'node_modules',
26+
],
27+
},
28+
2129
module: {
2230
loaders: [
2331
{
@@ -46,7 +54,7 @@ module.exports = {
4654
loader: 'null-loader',
4755
}, {
4856
test: /\.jpe?g$|\.gif$|\.png$|\.svg$/i,
49-
loader: 'null-loader',
57+
loader: 'fake-url-loader?limit=10000&name=assets/[name].[ext]',
5058
},
5159
],
5260
},

0 commit comments

Comments
 (0)