-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
89 lines (88 loc) · 2.61 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
const webpack = require('webpack');
const path = require('path');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const DEV = process.env.NODE_ENV === 'development';
let config = {
entry: {
cityselector: path.resolve(__dirname, './src/CitySelector.jsx'),
example: path.resolve(__dirname, './demo/examples.js')
},
output: {
publicPath: DEV ? 'dist': './',
filename: '[name].js',
path: path.resolve(__dirname, './dist')
},
module: {
rules: [{
test: /\.(jsx|js)$/,
use: [{
loader: 'babel-loader',
options: {
presets: ["react", "env"]
}
}]
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
resolve: {
alias: {
'react-city-selector': path.resolve(__dirname, 'src/CitySelector'),
},
extensions: ['.js', '.jsx', '.css'] //后缀名自动补全
},
plugins: []
};
if (DEV) {
//新版的webpack要定义配置模式一般只有development 和 production两种
config.mode = 'development',
//webpack压缩资源到devServer资源的一个映射,这个用eval过的版本比较好,
config.devtool = 'cheap-module-eval-source-map';
//webpack devserver的配置
config.devServer = {
//端口
port: 8888,
//主机
host: '0.0.0.0',
overlay: {
errors: true
},
//热替换
hot: true
};
//自动打开浏览器和热替换的插件,貌似react不支持
config.plugins.push(
new OpenBrowserPlugin({ url: 'http://127.0.0.1:8888/demo/index.html' }),
new webpack.HotModuleReplacementPlugin()
);
} else {
config.mode = 'production';
config.plugins.push(new ExtractTextPlugin('cityselector.css'));
//webpack4将压缩插件的配置移到了配置的optimization
config.optimization = {
minimizer: [
new UglifyJSPlugin({
uglifyOptions: {
compress: true,
sourceMap: false,
commits: false
}
})
]
};
config.module.rules[1].use = ExtractTextPlugin.extract({
fallback: "style-loader",
use: [{
loader: 'css-loader',
options: {
minimize: true //css压缩
}
}]
});
}
module.exports = config;