-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
100 lines (92 loc) · 2.66 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var path = require('path');
var webpack = require('webpack');
var fs = require('fs');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var ENV = process.env.ENV;
var isDev = ENV === 'dev'
var pages = fs.readdirSync(path.join(__dirname, './src/pages'));
var config = {
devtool: ENV == 'dev' ? 'cheap-module-eval-source-map' : 'cheap-module-source-map',
entry: [ path.join(__dirname, './src/main.js') ],
output: {
path: path.join(__dirname, 'public'),
publicPath: '/',
filename: 'js/[name].[hash:8].js'
},
plugins: [
new webpack.DefinePlugin({
'ENV': JSON.stringify(ENV)
}),
new CleanWebpackPlugin(['public/*', 'views/*']),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
filename: 'js/vendor.js'
}),
new HtmlWebpackPlugin({
alwaysWriteToDisk: true,
filename: path.join(__dirname, './views/index.html'),
template: path.join(__dirname, './src/index.html'),
inject: 'body'
}),
new ExtractTextPlugin({
filename: 'styles/[name].[hash:8].css',
allChunks: !isDev
}),
new webpack.LoaderOptionsPlugin({
options: {
htmlLoader: {
ignoreCustomFragments: [/\{\{.*?}}/],
attrs: ['img:src', 'link:href']
}
}
})
],
module: {
rules: [
{
test: /.html$/,
use: ['html-loader']
}, {
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader?minimize='+ !isDev, 'postcss-loader']
})
}, {
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader?minimize='+ !isDev, 'postcss-loader', 'sass-loader']
})
}, {
test: /\.(png|jpg)$/,
use: 'url-loader?limit=8192&name=imgs/[name].[hash:8].[ext]'
}
]
}
}
pages.forEach(function(page) {
var conf = {
alwaysWriteToDisk: true,
filename: path.join(__dirname, './views/'+ page + '.html'),
template: path.join(__dirname, './src/pages/'+ page + '/'+ page + '.html'),
inject: false
};
config.plugins.push(new HtmlWebpackPlugin(conf));
});
// build环境
if(ENV === 'build') {
config.plugins.push(new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
minimize: true,
sourceMap: true
}));
} else {
config.plugins.push(new HtmlWebpackHarddiskPlugin());
}
module.exports = config;