Skip to content
This repository was archived by the owner on May 27, 2023. It is now read-only.

Commit 90234e5

Browse files
committed
feat: use only webpack for build, use hashed filename scripts
1 parent 6e587fd commit 90234e5

File tree

6 files changed

+120
-3
lines changed

6 files changed

+120
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
**/webpack-stats.json
12
coverage
23
node_modules
34
npm-debug.log

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"version": "0.0.3",
44
"description": "An ES6 isomorphic Flux/ReactJS boilerplate",
55
"main": "server/index.js",
6-
"scripts": {},
6+
"scripts": {
7+
"build": "./node_modules/.bin/webpack --stats --config ./webpack/prod.config.js"
8+
},
79
"repository": {
810
"type": "git",
911
"url": "https://github.com/iam4x/isomorphic-flux-boilerplate.git"
@@ -42,6 +44,7 @@
4244
"chai": "^2.2.0",
4345
"del": "^1.1.1",
4446
"eslint": "^0.18.0",
47+
"eslint-loader": "^0.9.0",
4548
"esprima-fb": "^14001.1.0-dev-harmony-fb",
4649
"gulp": "^3.8.11",
4750
"gulp-autoprefixer": "^2.1.0",

server/router.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export default function *() {
4444
});
4545

4646
const content = yield render(router);
47-
yield this.render('main', {content});
47+
const assets = require('./webpack-stats.json');
48+
49+
yield this.render('main', {content, assets});
4850
}
4951
};

server/views/layouts/index.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
</head>
99
<body>
1010
{{{body}}}
11-
<script src="/assets/js/app.js" async defer></script>
11+
{{#each assets.script}}
12+
<script src={{this}}></script>
13+
{{/each}}
1214
</body>
1315
</html>

webpack/prod.config.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict';
2+
3+
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
4+
/* eslint camelcase: 0 */
5+
6+
require('babel/register');
7+
8+
var path = require('path');
9+
var webpack = require('webpack');
10+
11+
var writeStats = require('./utils/write-stats');
12+
13+
module.exports = {
14+
devtool: 'source-map',
15+
entry: {
16+
app: './app/index.js'
17+
},
18+
output: {
19+
path: path.join(__dirname, '../dist/js'),
20+
filename: '[name]-[hash].js',
21+
publicPath: '/assets/js/'
22+
},
23+
module: {
24+
preLoaders: [
25+
{
26+
test: /\.js$|.jsx$/,
27+
exclude: /node_modules/,
28+
loaders: ['eslint-loader']
29+
}
30+
],
31+
loaders: [
32+
{
33+
test: /\.js$|.jsx$/,
34+
exclude: /node_modules/,
35+
loaders: ['babel-loader']
36+
}
37+
]
38+
},
39+
plugins: [
40+
new webpack.DefinePlugin({
41+
'process.env': {
42+
BROWSER: JSON.stringify(true),
43+
NODE_ENV: JSON.stringify('production')
44+
}
45+
}),
46+
new webpack.optimize.DedupePlugin(),
47+
new webpack.optimize.OccurenceOrderPlugin(),
48+
new webpack.optimize.UglifyJsPlugin({
49+
compress: {
50+
warnings: false,
51+
screw_ie8: true,
52+
sequences: true,
53+
dead_code: true,
54+
drop_debugger: true,
55+
comparisons: true,
56+
conditionals: true,
57+
evaluate: true,
58+
booleans: true,
59+
loops: true,
60+
unused: true,
61+
hoist_funs: true,
62+
if_return: true,
63+
join_vars: true,
64+
cascade: true,
65+
drop_console: true
66+
},
67+
output: {
68+
comments: false
69+
}
70+
}),
71+
function () { this.plugin('done', writeStats); }
72+
],
73+
resolve: {
74+
extensions: ['', '.js', '.json', '.jsx'],
75+
modulesDirectories: ['node_modules', 'app']
76+
}
77+
};

webpack/utils/write-stats.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
// borrowed from https://github.com/gpbl/isomorphic500/blob/master/webpack%2Futils%2Fwrite-stats.js
4+
import fs from 'fs';
5+
import path from 'path';
6+
7+
const filepath = path.resolve(__dirname, '../../server/webpack-stats.json');
8+
9+
export default function (stats) {
10+
const publicPath = this.options.output.publicPath;
11+
const json = stats.toJson();
12+
13+
// get chunks by name and extensions
14+
const getChunks = function (name, ext) {
15+
ext = ext || 'js';
16+
let chunk = json.assetsByChunkName[name];
17+
18+
// a chunk could be a string or an array, so make sure it is an array
19+
if (!(Array.isArray(chunk))) {
20+
chunk = [chunk];
21+
}
22+
23+
return chunk
24+
.filter(chunk => path.extname(chunk) === `.${ext}`) // filter by extension
25+
.map(chunk => `${publicPath}${chunk}`); // add public path to it
26+
};
27+
28+
const script = getChunks('app', 'js');
29+
const content = {script};
30+
31+
fs.writeFileSync(filepath, JSON.stringify(content));
32+
};

0 commit comments

Comments
 (0)