-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.babel.js
136 lines (135 loc) · 4.13 KB
/
webpack.config.babel.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import glob from 'glob';
import path from 'path';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import Visualizer from 'webpack-visualizer-plugin';
import webpack from 'webpack';
import WebpackMd5Hash from 'webpack-md5-hash';
import HtmlWebpackPlugin from 'html-webpack-plugin';
export default function({ mode = 'development' } = {}) {
// const qiniuPlugin = mode === 'development' ? {} : require('./qiniu.js');
const viewRoot = './client/views/';
const staticRoot = './static';
const jsFiles = glob.sync(viewRoot + '*/main.js');
const entry = jsFiles.reduce((entry, filename) => {
const name = path.posix.relative(viewRoot, filename).replace(/\/main\.js/, '');
entry[name] = filename;
return entry;
}, {});
const autoSWFiles = glob.sync(viewRoot + '*/*/sw.js');
autoSWFiles.forEach(filename => {
const name = path.posix.relative(viewRoot, filename).replace(/\/sw\.js/, '-sw');
entry[name] = filename;
});
const htmlFiles = glob.sync(viewRoot + '*/main.html');
const htmlWebpackPlugins = htmlFiles.map(template => {
const [ , path ] = template.match(/\.\/client\/views\/([^\/]+)\/main\.html/);
const options = {
inject: false,
chunks: [ path ],
filename: '../views/' + path + '.html',
template,
};
if (mode !== 'development') {
options.minify = {
removeComments: true,
collapseWhitespace: true,
};
}
return new HtmlWebpackPlugin(options);
});
console.log(entry);
console.log('\x1b[35m%s\x1b[0m', '[' + new Date().toLocaleString() + ']', '--webpack start');
const cssLoader = ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [ 'css-loader?minimize', 'postcss-loader' ],
});
return {
entry,
output: {
filename: mode === 'development' ? 'js/[name].js' : 'js/[name]-[chunkhash].js',
// chunkFilename: mode === 'development' ? 'js/[name].js' : 'js/[name]-[chunkhash].js',
path: path.resolve(staticRoot),
publicPath: '/static/',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules$/,
use: [{
loader: 'babel-loader',
options: {
babelrc: false,
cacheDirectory: './.babel_cache/',
presets: [
[ 'es2015', { loose: true, modules: false }],
],
plugins: [ 'async-to-promises' ],
},
}],
},
{
test: /\.css$/,
use: cssLoader,
},
{
test: /\.(png|jpg|jpeg|gif)$/,
use: 'url-loader?limit=10000&name=img/[name]-[hash].[ext]',
},
{
test: /\.(otf|ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,
use: 'url-loader?limit=10000&name=font/[name]-[hash].[ext]',
},
],
},
resolve: {
// 模块别名定义,方便后续直接引用别名,无须多写长长的地址
alias: {
utils: path.resolve('./client/views/common/utils.js'),
store: path.resolve('./client/views/common/store.js'),
raven: path.resolve('./client/views/common/raven.js'),
},
enforceExtension: false,
modules: [
path.resolve('./client'),
'node_modules',
],
},
plugins: [
new webpack.LoaderOptionsPlugin({
options: {
output: {
publicPath: '/static/',
},
},
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"' + mode + '"',
},
}),
new ExtractTextPlugin({ filename: mode === 'development' ? 'css/[name].css' : 'css/[name]-[contenthash].css' }),
...htmlWebpackPlugins,
new Visualizer(),
].concat(mode !== 'development'
? [
new WebpackMd5Hash(),
// css minify will finished by stc
new webpack.optimize.UglifyJsPlugin({
test: /\.js$/,
comments: false,
sourceMap: true,
compress: {
// remove warnings
warnings: false,
// Drop console statements
drop_console: true,
},
}),
// qiniuPlugin,
]
: [
]
),
};
}