Skip to content

Commit 0e659da

Browse files
committed
feat(webpack): css hot-reloading and image loading through webpack
1 parent 510101f commit 0e659da

File tree

13 files changed

+66
-265
lines changed

13 files changed

+66
-265
lines changed

.eslintrc

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
"browser": true,
66
"node": true,
77
"mocha": true
8-
},
9-
"ecmaFeatures": {
8+
},
9+
"ecmaFeatures": {
1010
"jsx": true,
1111
"es6": true,
1212
"classes": true
13-
},
14-
"rules": {
13+
},
14+
"rules": {
1515
"comma-dangle": [1, "always-multiline"],
1616
"no-underscore-dangle" : 0,
1717
"max-len": [1, 180, 4],
1818
"arrow-body-style": [0],
1919
"react/jsx-no-bind": [0]
20-
}
20+
},
21+
"globals": {
22+
"webpackIsomorphicTools": true
23+
}
2124
}

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ npm-debug.log
55
dump.rdb
66
.vscode/
77
public/*
8-
static/dist
9-
static/css/app.min.css
8+
dist
File renamed without changes.

client/components/.keep

Whitespace-only changes.

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

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

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

index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
// If the server is webpacked, css modules is not working. Have to find another way to inject css modules classnames in html without css-modules-require-hook.
44
// if (process.env.NODE_ENV === 'production') {
5-
// require('./static/dist/server.bundle.js');
5+
// require('./dist/server.bundle.js');
66
// }
77

8+
9+
// Ignore css and image files in babel
10+
const noop = () => { };
11+
require.extensions['.css'] = noop();
12+
813
require('babel-register');
9-
// Ignore css files in babel
10-
require.extensions['.css'] = () => {
11-
return;
12-
};
1314
require('babel-polyfill');
1415
require('css-modules-require-hook')({
1516
generateScopedName: process.env.NODE_ENV === 'production' ? '[hash:base64]' : '[name]__[local]__[hash:base64:5]',

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"bs": "npm run clean && npm run build && npm run start:prod",
1111
"build": "cross-env NODE_ENV=production webpack --config webpack.config.prod.js",
1212
"build:server": "webpack --config webpack.config.server.js",
13-
"clean": "rimraf static/dist",
13+
"clean": "rimraf dist",
1414
"slate": "rimraf node_modules && npm install",
1515
"lint": "eslint client server"
1616
},
@@ -59,6 +59,7 @@
5959
"babel-register": "^6.9.0",
6060
"chai": "^3.5.0",
6161
"chunk-manifest-webpack-plugin": "0.1.0",
62+
"copy-webpack-plugin": "^3.0.1",
6263
"css-loader": "^0.23.1",
6364
"css-modules-require-hook": "^4.0.1",
6465
"cssnano": "^3.6.2",

server/server.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import webpackHotMiddleware from 'webpack-hot-middleware';
1212
// Initialize the Express App
1313
const app = new Express();
1414

15-
if (process.env.NODE_ENV !== 'production') {
15+
if (process.env.NODE_ENV === 'development') {
1616
const compiler = webpack(config);
1717
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }));
1818
app.use(webpackHotMiddleware(compiler));
@@ -47,16 +47,16 @@ mongoose.connect(serverConfig.mongoURL, (error) => {
4747
// Apply body Parser and server public assets and routes
4848
app.use(bodyParser.json({ limit: '20mb' }));
4949
app.use(bodyParser.urlencoded({ limit: '20mb', extended: false }));
50-
app.use(Express.static(path.resolve(__dirname, '../static')));
50+
app.use(Express.static(path.resolve(__dirname, '../dist')));
5151
app.use('/api', posts);
5252

5353
// Render Initial HTML
5454
const renderFullPage = (html, initialState) => {
5555
const head = Helmet.rewind();
5656

5757
// Import Manifests
58-
const assetsManifest = process.env.NODE_ENV === 'production' ? require('../static/dist/manifest.json') : {}; // eslint-disable-line
59-
const chunkManifest = process.env.NODE_ENV === 'production' ? require('../static/dist/chunk-manifest.json') : {}; // eslint-disable-line
58+
const assetsManifest = process.env.NODE_ENV === 'production' ? require('../dist/manifest.json') : {}; // eslint-disable-line
59+
const chunkManifest = process.env.NODE_ENV === 'production' ? require('../dist/chunk-manifest.json') : {}; // eslint-disable-line
6060

6161
return `
6262
<!doctype html>
@@ -68,7 +68,7 @@ const renderFullPage = (html, initialState) => {
6868
${head.link.toString()}
6969
${head.script.toString()}
7070
71-
<link rel="stylesheet" href='/dist/app.css' />
71+
${process.env.NODE_ENV === 'production' ? `<link rel='stylesheet' href='${assetsManifest['/app.css']}' />` : ''}
7272
<link href='https://fonts.googleapis.com/css?family=Lato:400,300,700' rel='stylesheet' type='text/css'/>
7373
<link rel="shortcut icon" href="http://res.cloudinary.com/hashnode/image/upload/v1455629445/static_imgs/mern/mern-favicon-circle-fill.png" type="image/png" />
7474
</head>
@@ -77,12 +77,12 @@ const renderFullPage = (html, initialState) => {
7777
<script>
7878
window.__INITIAL_STATE__ = ${JSON.stringify(initialState)};
7979
${process.env.NODE_ENV === 'production' ?
80-
`//<![CDATA[
80+
`//<![CDATA[
8181
window.webpackManifest = ${JSON.stringify(chunkManifest)};
8282
//]]>` : ''}
8383
</script>
84-
<script src='${process.env.NODE_ENV === 'production' ? assetsManifest['/dist/vendor.js'] : '/dist/vendor.js'}'></script>
85-
<script src='${process.env.NODE_ENV === 'production' ? assetsManifest['/dist/app.js'] : '/dist/app.js'}'></script>
84+
<script src='${process.env.NODE_ENV === 'production' ? assetsManifest['/vendor.js'] : '/vendor.js'}'></script>
85+
<script src='${process.env.NODE_ENV === 'production' ? assetsManifest['/app.js'] : '/app.js'}'></script>
8686
</body>
8787
</html>
8888
`;

static/css/app.css

-221
This file was deleted.

0 commit comments

Comments
 (0)