-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathwebpack.config.js
More file actions
105 lines (99 loc) · 3.65 KB
/
webpack.config.js
File metadata and controls
105 lines (99 loc) · 3.65 KB
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
var path = require('path')
var glob = require('glob')
var webpack = require('webpack')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin
var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin
const debug = process.env.NODE_ENV !== 'production'
var entries = getEntry('src/scripts/page/**/*.js', 'src/scripts/page/')
var chunks = Object.keys(entries)
var config = {
entry: entries,
output: {
path: path.join(__dirname, 'public'),
publicPath: '/static/',
filename: 'scripts/[name].js',
chunkFilename: 'scripts/[id].chunk.js?[chunkhash]'
},
module: {
loaders: [ // 加载器
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style', 'css')
}, {
test: /\.less$/,
loader: ExtractTextPlugin.extract('css!less')
}, {
test: /\.html$/,
loader: 'html?-minimize' // 避免压缩html,https://github.com/webpack/html-loader/issues/50
}, {
test: /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader?name=fonts/[name].[ext]'
}, {
test: /\.(png|jpe?g|gif)$/,
loader: 'url-loader?limit=8192&name=imgs/[name]-[hash].[ext]'
}
]
},
plugins: [
new webpack.ProvidePlugin({ // 加载jq
$: 'jquery'
}),
new CommonsChunkPlugin({
name: 'vendors', // 将公共模块提取,生成名为`vendors`的chunk
chunks: chunks,
minChunks: chunks.length // 提取所有entry共同依赖的模块
}),
new ExtractTextPlugin('styles/[name].css'), // 单独使用link标签加载css并设置路径,相对于output配置中的publickPath
debug ? function () {} : new UglifyJsPlugin({ // 压缩代码
compress: {
warnings: false
},
except: ['$super', '$', 'exports', 'require'] // 排除关键字
})
]
}
var pages = Object.keys(getEntry('src/views/**/*.html', 'src/views/'))
pages.forEach(function (pathname) {
var conf = {
filename: '../views/' + pathname + '.html', // 生成的html存放路径,相对于path
template: 'src/views/' + pathname + '.html', // html模板路径
inject: false // js插入的位置,true/'head'/'body'/false
/*
* 压缩这块,调用了html-minify,会导致压缩时候的很多html语法检查问题,
* 如在html标签属性上使用{{...}}表达式,很多情况下并不需要在此配置压缩项,
* 另外,UglifyJsPlugin会在压缩代码的时候连同html一起压缩。
* 为避免压缩html,需要在html-loader上配置'html?-minimize',见loaders中html-loader的配置。
*/
// minify: { //压缩HTML文件
// removeComments: true, //移除HTML中的注释
// collapseWhitespace: false //删除空白符与换行符
// }
}
if (pathname in config.entry) {
conf.favicon = path.resolve(__dirname, 'src/imgs/favicon.ico')
conf.inject = 'body'
conf.chunks = ['vendors', pathname]
conf.hash = true
}
config.plugins.push(new HtmlWebpackPlugin(conf))
})
module.exports = config
function getEntry (globPath, pathDir) {
var files = glob.sync(globPath)
var entries = {}, entry, dirname, basename, pathname, extname
for (var i = 0; i < files.length; i++) {
entry = files[i]
dirname = path.dirname(entry)
extname = path.extname(entry)
basename = path.basename(entry, extname)
pathname = path.normalize(path.join(dirname, basename))
pathDir = path.normalize(pathDir)
if (pathname.startsWith(pathDir)) {
pathname = pathname.substring(pathDir.length)
}
entries[pathname] = ['./' + entry]
}
return entries
}