forked from ifuyun/iFuyun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
81 lines (80 loc) · 2.45 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* jslint nomen:true */
/**
* WebPack config file
* @author Fuyun
*/
const webpack = require('webpack');
const Md5Hash = require('webpack-md5-hash');
const md5HashPlugin = new Md5Hash();
const path = require('path');
const glob = require('glob');
const fs = require('fs');
const argv = require('yargs').argv;
const isDev = argv.env === 'development';
const getEntry = function () {
let entry = {};
glob.sync('./public/src/js/module/**/*.js').forEach(function (name) {
var n = name.slice(name.indexOf('module/') + 7, name.length - 3);
entry[n] = name;
});
return entry;
};
const replaceHash = function () {
this.plugin('done', function (stats) {
if (!isDev) {
const chunks = stats.compilation.namedChunks;
const htmlFiles = glob.sync('./.tmp/step2/views/**/*.html');
for (const curFile of htmlFiles) {
for (const chunkKey of Object.keys(chunks)) {
const chunk = chunks[chunkKey];
const curHtml = fs.readFileSync(curFile, 'utf8');
const newHtml = curHtml.replace(new RegExp(chunk.name + '[\\-_0-9a-zA-Z]*\\.js', 'ig'), chunk.files[0]);
fs.writeFileSync(curFile, newHtml);
}
}
}
});
};
const commonsPlugin = new webpack.optimize.CommonsChunkPlugin({
name: 'common',
filename: isDev ? 'common.js' : 'common-[chunkhash:8].js',
minChunks: 3
});
const uglifyPlugin = new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
exclude: [
// path.resolve(__dirname, 'public/src/js/plugin'),
// path.resolve(__dirname, 'public/src/js/vendor')
/\/(plugins|vendor)\/[\w\W]+(?:.js)?/i
]
});
const providePlugin = new webpack.ProvidePlugin({
'$': 'jquery',
'jQuery': 'jquery'
});
module.exports = {
entry: getEntry(),
output: {
path: path.resolve(__dirname, 'public', isDev ? 'dev/js' : 'dist/js'),
publicPath: './js/',
filename: isDev ? '[name].js' : '[name]-[chunkhash:8].js'
},
plugins: [commonsPlugin, uglifyPlugin, providePlugin, md5HashPlugin, replaceHash],
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
exclude: [
/\/(plugin|vendor)\/[\w\W]+(?:.js)?/i
]
}
]
},
resolve: {
extensions: ['.js'],
alias: {}
}
};