-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
201 lines (193 loc) · 5.03 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const path = require('path');
const webpack = require('webpack');
var fs = require('fs');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var OpenBrowserPlugin = require('open-browser-webpack-plugin');
var useIE8 = true;
module.exports = function(env) {
const appList = {
mobile: {
html: true,
path: "./src/entry/mobile/index.js"
},
pc: {
html: true,
path: "./src/entry/pc/index.js"
},
};
const nodeEnv = env.env || 'development';
const action = env.action || 'start';
const isBuild = action === 'build';
var plugins = [];
var entryAndHtmlPlugin = getEntryAndHtmlPlugin(appList, isBuild);
var entry = entryAndHtmlPlugin.entry;
if (!useIE8) {
plugins.push(new webpack.HotModuleReplacementPlugin());
}
if (isBuild) {
rmdirSync('./dist');
} else {
const openurl = env.openurl || '';
if (openurl.length > 0) {
plugins.push(new OpenBrowserPlugin({
url: openurl
}));
}
}
plugins = plugins.concat(entryAndHtmlPlugin.htmlplugins);
if(useIE8) {
plugins.push(new UglifyJSPlugin({
uglifyOptions: {
ie8: true,
output: {
comments: false,
beautify: false
},
warnings: false
}
}));
}
return {
entry: entry,
output: {
filename: '[name].[chunkhash:8].js',
chunkFilename: isBuild ? '[name].[chunkhash:8].min.js' : '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: isBuild ? './': '/'
},
devServer: {
hot: !useIE8,
inline: !useIE8
},
plugins: plugins,
// cheap-module-eval-source-map 在调试ie8时候报错 如果要chrome断点调试换成 cheap-module-eval-source-map
// devtool: isBuild ? 'cheap-module-source-map': 'cheap-module-eval-source-map',
devtool: isBuild ? 'cheap-module-source-map': 'cheap-module-source-map',
module: {
rules: [{
test: /\.js$/,
include: [path.resolve(__dirname, "src"),],
use: {
loader: "babel-loader",
options: {
"presets": [
"es2015",
"stage-3"
],
"plugins": [
"syntax-dynamic-import",
"transform-class-properties",
"transform-runtime"]
}
},
},
{
test: /\.(png|jpg|jpeg|gif|woff)$/,
loader: 'url-loader?limit=6144&name=imgs/[path][name].[ext]'
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader?name=fonts/[name].[ext]'
},
{
test: /\.css$/,
use: ['style-loader', {
loader: "css-loader",
},
{
loader: "postcss-loader",
options: {
plugins: (loader) => [require('postcss-import')({
root: loader.resourcePath
}), require('autoprefixer')(), ]
}
}],
},
{
test: /\.less$/,
use: [{
loader: "style-loader"
},
{
loader: "css-loader",
},
{
loader: "less-loader",
options: {
javascriptEnabled: true
}
}]
},
{
test: require.resolve('jquery'),
use: [{
loader: 'expose-loader',
options: 'jQuery'
},{
loader: 'expose-loader',
options: '$'
}]
}
]
}
};
};
var rmdirSync = (function() {
function iterator(url, dirs) {
var stat = fs.statSync(url);
if (stat.isDirectory()) {
dirs.unshift(url); //收集目录
inner(url, dirs);
} else if (stat.isFile()) {
fs.unlinkSync(url); //直接删除文件
}
}
function inner(path, dirs) {
var arr = fs.readdirSync(path);
for (var i = 0,
el; el = arr[i++];) {
iterator(path + "/" + el, dirs);
}
}
return function(dir, cb) {
cb = cb ||
function() {};
var dirs = [];
try {
iterator(dir, dirs);
for (var i = 0,
el; el = dirs[i++];) {
fs.rmdirSync(el); //一次性删除所有收集到的目录
}
cb()
} catch(e) { //如果文件或目录本来就不存在,fs.statSync会报错,不过我们还是当成没有异常发生
e.code === "ENOENT" ? cb() : cb(e);
}
}
})();
function getEntryAndHtmlPlugin(siteMaps, isBuild) {
var re = {
entry: {},
htmlplugins: []
};
for (var siteName in siteMaps) {
var sitInfo = siteMaps[siteName];
// "es5-shim", "es5-shim/es5-sham",
re.entry[siteName] = ["babel-polyfill", sitInfo.path]; //js多入口字典对象
if (sitInfo.html) {
re.htmlplugins.push(new HtmlWebpackPlugin({
// 正常的:filename: siteName+'.html', //打包出来的html名字
filename: (siteName) + '.html',
//打包出来的html名字
template: './src/entry/' + siteName + '/index.html',
//模版路径
inject: 'body',
chunks: [siteName],
//js注入的名字
hash: true
}));
}
}
return re;
}