-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebpack.config.js
115 lines (113 loc) · 3.23 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var webpack = require('webpack');
var debug = process.env.NODE_ENV !== 'prod';
var RootDir = process.cwd();
var config = {
entry: {
bundle: './source/js/scripts.js'
},
output: {
path: __dirname + (debug?'/source/build/dev':'/source/build/public'), // 指定某些loader 的输出路径(js/img)
filename: debug?'js/[name].js':'js/[name].[chunkhash:8].min.js',
publicPath: debug?'/build/dev/':'/build/public/', // generate URL
chunkFilename: 'chunks/[name].chunk[chunkhash:8].js'
},
resolve: {
alias: {
'jquery': RootDir + './source/js/jquery-1.11.1.min.js',
'root': RootDir
},
// 自动扩展的文件名后缀
extensions: ['.js', '.css', '.scss', '.png', '.jpg']
},
module: {
rules: [
{ // use bable-loader for *.js files
test: /\.js$/,
loader: 'babel-loader',
query: {
compact: false,
plugins: ['syntax-dynamic-import','transform-runtime','transform-object-rest-spread'],
presets: ['es2015']
},
exclude: /node_modules/ // regex
},
{
test: /\.(png|jpe?g|gif|svg)$/,
loaders: [
'url?limit=10000&name=img/[hash:8].[name].[ext]'
]
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
// use style-loader in development
fallback: 'style-loader',
//resolve-url-loader may be chained before sass-loader if necessary
use: ['css-loader', 'sass-loader']
})
},
{
test: /\.ejs$/,
loader: ['ejs-loader?variable=data']
},
{
test: /\.txt$/,
use: 'raw-loader'
}
],
},
externals: {
jquery: 'jQuery',
partial: "partial"
},
plugins: debug ? [
new ExtractTextPlugin({filename: 'css/[name].css', allChunks: true}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
}),
new HtmlWebpackPlugin({
template: '!!raw-loader!./layout/casper/layout.txt',
filename: './../../../layout/layout.ejs',
}),
] : [
new ExtractTextPlugin({filename: 'css/[name].[contenthash:8].min.css', allChunks: true}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new HtmlWebpackPlugin({
template: '!!raw-loader!./layout/casper/layout.txt',
filename: './../../../layout/layout.ejs',
}),
]
}
if (!debug) {
//生产模式标识
config.plugins.push(
new webpack.DefinePlugin({
'process.env':{
'NODE_ENV': JSON.stringify('production')
}
})
);
//压缩
config.plugins.push(
new webpack.optimize.UglifyJsPlugin({compress:{warnings:false}})
);
//清空文件夹
config.plugins.push(
new CleanWebpackPlugin(['public', 'dev'], {
root: RootDir + '/source/build', // 根路径(绝对路径)
verbose: true, // 控制台打印日志
dry: false // 不清空任何文件,用于测试
})
);
config.output.sourceMapFilename = '[file].map';
config.devtool = 'source-map';
}
module.exports = config;