-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebpack.config.js
165 lines (161 loc) · 3.94 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
const fs = require("fs");
const path = require("path");
const dir = path.join.bind(path, __dirname);
const isProd = process.env.NODE_ENV === "production";
const fm = (list, file) => {
const txt = list.map(
v => fs.readFileSync(dir(v), "utf-8")
).join("\n") || "";
const target = dir("build");
fs.existsSync(target) || fs.mkdirSync(target);
fs.writeFileSync(dir("build", file), txt, "utf-8");
};
const jsList = [
"jquery/dist/jquery",
"jquery-ui-dist/jquery-ui",
].map(v => isProd
? `node_modules/${v}.min.js`
: `node_modules/${v}.js`
);
fm(jsList, "jquery.dll.js");
const webpack = require("webpack");
const merge = require("webpack-merge");
const HappyPack = require("happypack");
const Es3ifyPlugin = require("es3ify-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const currentConfig = require(isProd ? "./webpack.cfg" : "./webpack.cfg.dev");
const commonConfig = {
entry: {
shim: [
"es5-shim", // 支持 IE8 所必须,且顺序在babel-polyfill前
"es5-shim/es5-sham",
"console-polyfill",
"babel-polyfill",
"media-match", // 支持 antd 所必须
],
public: [
dir("src/utils/public.js"),
],
},
output: {
path: dir("dist"),
filename: "js/[name].[chunkhash:5].js",
// 用import()按需加载 https://doc.webpack-china.org/api/module-methods/#import-
chunkFilename: "js/[name].[chunkhash:5].js",
publicPath: "./",
},
module: {
postLoaders: [{
test: /\.jsx?$/i,
loader: "happypack/loader?cacheDirectory=true&id=pre",
}],
loaders: [
{
test: /\.jsx?$/i,
loader: "happypack/loader?cacheDirectory=true&id=jsx",
include: dir("src"),
exclude: dir("src/static"),
},
{
test: /\.(jpe?g|png|gif|bmp|ico)(\?.*)?$/i,
loader: "url-loader?limit=6144&name=img/[name].[hash:5].[ext]",
},
{
test: /\.(woff2?|svg|ttf|otf|eot)(\?.*)?$/i,
loader: "url-loader?limit=6144&name=font/[name].[hash:5].[ext]",
},
],
},
plugins: [
new HappyPack({
id: "pre",
threads: 4,
loaders: [{
loader: "export-from-ie8/loader",
options: {
cacheDirectory: true,
},
}],
}),
new HappyPack({
id: "jsx",
threads: 4,
loaders: [{
loader: "babel-loader",
options: {
cacheDirectory: true,
},
}],
}),
new CopyWebpackPlugin([
{
from: "src/static",
to: "static",
},
{
context: "build",
from: "*.dll.js",
to: "static/js",
},
]),
new webpack.optimize.CommonsChunkPlugin({
name: "runtime",
minChunks: (module, count) => {
const { resource } = module || {};
return resource && /\.(vue|jsx?)$/i.test(resource) &&
resource.indexOf(dir("node_modules")) === 0;
},
}),
new webpack.optimize.CommonsChunkPlugin({
name: "manifest",
chunks: ["runtime"],
}),
/*new webpack.ContextReplacementPlugin(
/moment[\\/]locale$/i,
/^\.\/zh-cn$/i,
),*/
new webpack.IgnorePlugin(/^\.\/locale$/i, /moment$/i),
new Es3ifyPlugin(),
],
resolve: {
alias: {
/*api: dir("src/api"),
components: dir("src/components"),
containers: dir("src/containers"),
constants: dir("src/constants"),
reducers: dir("src/reducers"),
actions: dir("src/actions"),
routes: dir("src/routes"),
styles: dir("src/styles"),
views: dir("src/views"),
utils: dir("src/utils"),
"@": dir("src"),*/
},
extensions: ["", ".js", ".jsx", ".json"],
},
performance: {
hints: false,
},
};
const addPagePlugin = name => {
const app = name || "index";
commonConfig.entry[app] = [
dir("src/views/" + app + ".js"),
];
commonConfig.plugins.push(
new HtmlWebpackPlugin({
filename: app + ".html",
template: dir("src/index.html"),
title: "Home Page " + app,
chunks: ["manifest", "runtime", "shim", "public", app],
chunksSortMode: "manual",
inject: true,
xhtml: true,
hash: true,
})
);
};
const pageList = [""]; // 多页面打包
pageList.forEach(v => addPagePlugin(v));
module.exports = merge(commonConfig, currentConfig);