-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
362 lines (346 loc) · 14.3 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var DashboardPlugin = require('webpack-dashboard/plugin');
var OpenBrowserPlugin = require('open-browser-webpack-plugin');
var StringReplacePlugin = require("string-replace-webpack-plugin");
var loaderUtils = require("loader-utils");
var CopyWebpackPlugin = require('copy-webpack-plugin');
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
var CompressionPlugin = require("compression-webpack-plugin");
const {
CheckerPlugin
} = require('awesome-typescript-loader');
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
/**
* Env
* Get npm lifecycle event to identify the environment
*/
var ENV = process.env.NODE_ENV;
var isTest = ENV === 'test' || ENV === 'test-watch';
var isProd = ENV === 'production';
/**
* Detect if run with webpack-dev-server or only do webpack build
*/
var isDevServer = false;
var argvs = process.argv;
if (argvs !== undefined && argvs !== null) {
for (var i = 0; i < argvs.length; i++) {
if (argvs[i].includes("webpack-dev-server")) {
isDevServer = true;
break;
}
}
}
/**
* OS Platform
* Decide browser name for open browser automatic
*/
var isWin = /^win/.test(process.platform);
var isOSX = /^darwin/.test(process.platform);
var browserName = isWin ? 'chrome' : isOSX ? 'google chrome' : 'google-chrome';
var basePlugins = [
new webpack.DllReferencePlugin({
manifest: require(__dirname + '/dll/vendor-manifest.json'),
}),
new StringReplacePlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: 'common',
filename: isProd ? 'scripts/common.[hash].bundle.js' : 'scripts/common.bundle.js',
minChunks(module, count) {
var context = module.context;
return (context && context.indexOf('node_modules') >= 0) || (count >= 2);
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: "manifest",
minChunks: Infinity
}),
new AddAssetHtmlPlugin({
filepath: path.resolve(__dirname, './dll/*.js'),
outputPath: './scripts',
publicPath: './scripts',
includeSourcemap: false
}),
new HtmlWebpackPlugin({
template: './src/index.html',
title: 'Baldwin\'s Blog', //index title
minify: isProd ? {
removeComments: true, //删除注释
collapseBooleanAttributes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
collapseWhitespace: true,
conservativeCollapse: true, //删除空格
preserveLineBreaks: true
} : {},
inject: false
}),
new ExtractTextPlugin({
filename: isProd ? "css/[name].[contenthash].bundle.css" : "css/[name].bundle.css",
allChunks: true
}),
new CopyWebpackPlugin([{
from: './src/mock',
to: 'mock'
}]),
new webpack.EnvironmentPlugin(['NODE_ENV'])
];
var devPlugins = [
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false
})
];
var devPluginsForWebpackDevServer = [
new webpack.HotModuleReplacementPlugin(),
new OpenBrowserPlugin({
url: 'http://localhost:8080/www/',
browser: browserName
}),
new DashboardPlugin()
];
var prodPlugins = [
new webpack.DefinePlugin({
"process.env": {
// This has effect on the react lib size
"NODE_ENV": JSON.stringify("production")
}
}),
new webpack.optimize.UglifyJsPlugin({
minimize: true,
output: {
comments: false,
},
exclude: [/\.min\.js$/gi] // skip pre-minified libs
}),
new webpack.optimize.ModuleConcatenationPlugin(),
];
var prodPluginsForWebpackDevServer = [
new OpenBrowserPlugin({
url: 'http://localhost:8080/www/',
browser: browserName
}),
new DashboardPlugin()
];
var webpackConfig = {
// entry
entry: isProd ? {
app: ["./src/app.tsx"]
} : {
// app: ['webpack-dev-server/client?http://0.0.0.0:8384', 'webpack/hot/only-dev-server', './src/app.tsx']
app: ["react-hot-loader/patch", "./src/app.tsx"]
},
// output
output: {
path: __dirname + "/www",
publicPath: './',
filename: isProd ? 'scripts/[name].[hash].bundle.js' : 'scripts/[name].bundle.js',
chunkFilename: isProd ? 'scripts/[id].[name].[chunkhash].chunk.js' : 'scripts/[id].[name].chunk.js', //chunkhash 带版本号的hash
sourceMapFilename: '[file].map',
pathinfo: isProd ? false : true
},
// Enable sourcemaps for debugging webpack's output.
devtool: isProd ? "nosources-source-map" : "cheap-module-eval-source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js", ".json"]
},
module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
{
test: /\.tsx?$/,
// loaders: ["babel-loader", "ts-loader"],
use: [
'cache-loader',
{
loader: StringReplacePlugin.replace({
replacements: [{
pattern: /_import\(/ig,
replacement: function(match, p1, offset, string) {
return 'import(';
}
}]
})
},
{
loader: 'babel-loader'
},
{
loader: 'awesome-typescript-loader',
options: {
"configFileName": "src/tsconfig.json"
}
},
],
include: path.join(__dirname, 'src')
},
{
enforce: "pre",
test: /\.js$/,
loader: "source-map-loader"
},
{
// CSS LOADER
// Reference: https://github.com/webpack/css-loader
// Allow loading css through js
//
// Reference: https://github.com/postcss/postcss-loader
// Postprocess your css with PostCSS plugins
test: /\.css$/,
use: isTest ? 'null' : ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader?sourceMap'],
publicPath: "../"
})
},
{
// ASSET LOADER
// Reference: https://github.com/webpack/url-loader
// Copy png, jpg, jpeg, gif, woff, woff2, ttf, eot files to output
// Rename the file using the asset hash
// Pass along the updated reference to your code
// You can add here any file extension you want to get copied to your output
test: /\.(png|jpg|jpeg|gif|woff|woff2|eot|otf|ttf)$/,
use: [{
loader: 'url-loader',
options: {
limit: 8192,
name: isProd ? '[path][name].[hash].[ext]' : '[path][name].[ext]'
}
}]
},
{
// optimize svg load
test: /\.svg$/,
use: [
'cache-loader',
{
loader: 'url-loader',
options: {
limit: 8192,
name: isProd ? '[path][name].[hash].[ext]' : '[path][name].[ext]'
}
}, {
loader: 'svgo-loader',
options: {
plugins: [{
removeEditorsNSData: true
},
{
removeUnknownsAndDefaults: true
}
]
}
}
]
},
{
test: /\.scss$/,
use: isTest ? ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
'cache-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: true,
localIdentName: '[path]__[name]__[local]__[hash:base64:5]',
getLocalIdent: (context, localIdentName, localName, options) => {
// console.log("getLocalIdent : " + context + " " + localIdentName +
// " " + localName + " " + options);
// console.log("getLocalIdent : " + context.resourcePath + " " + context.resource +
// " " + options.context);
switch (localName.substr(0, 4)) {
case "ant-":
// console.log("ant-: " + localName);
return localName;
default:
if (!options.context)
options.context = context.options && typeof context.options.context === "string" ? context.options.context : context.context;
var request = path.relative(options.context, context.resourcePath);
options.content = options.hashPrefix + request + "+" + localName;
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
var hash = loaderUtils.interpolateName(context, localIdentName, options);
return hash.replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-").replace(/^((-?[0-9])|--)/, "_$1");
}
}
}
},
{
loader: 'sass-loader'
}
],
publicPath: "../"
}) : ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: true,
localIdentName: '[path]__[name]__[local]__[hash:base64:5]',
getLocalIdent: (context, localIdentName, localName, options) => {
// console.log("getLocalIdent : " + context + " " + localIdentName +
// " " + localName + " " + options);
// console.log("getLocalIdent : " + context.resourcePath + " " + context.resource +
// " " + options.context);
switch (localName.substr(0, 4)) {
case "ant-":
// console.log("ant-: " + localName);
return localName;
default:
if (!options.context)
options.context = context.options && typeof context.options.context === "string" ? context.options.context : context.context;
var request = path.relative(options.context, context.resourcePath);
options.content = options.hashPrefix + request + "+" + localName;
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
var hash = loaderUtils.interpolateName(context, localIdentName, options);
return hash.replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-").replace(/^((-?[0-9])|--)/, "_$1");
}
}
}
},
{
loader: 'sass-loader'
}
],
publicPath: "../"
})
},
{
// JSON LOADER
test: /\.json$/,
loader: 'json-loader'
}
]
},
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
// externals: {
// },
// plugins
plugins: isProd ? (isDevServer ? basePlugins.concat(prodPlugins).concat(prodPluginsForWebpackDevServer) : basePlugins.concat(prodPlugins)) : (isDevServer ? basePlugins.concat(devPlugins).concat(devPluginsForWebpackDevServer) : basePlugins.concat(devPlugins)),
devServer: {
publicPath: "/www/",
compress: true,
port: 8080,
historyApiFallback: {
index: "/www/index.html"
},
hot: isProd ? false : true,
stats: {
colors: true
},
quiet: process.env.WEBPACK_DEV_SERVER_QUITE ? true : false
}
// isProd: isProd,
// isTest: isTest
};
module.exports = webpackConfig;