-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
116 lines (113 loc) · 3.31 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
const webpack = require("webpack");
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const copyWebpackPlugin = require('copy-webpack-plugin');
//分离css
const ExtractTextPlugin = require('extract-text-webpack-plugin');
let env = process.env;
console.log(env.NODE_ENV)
let proxy = null;
let devtool = null;
// let proxyDev = {
// '/api':{
// target: "http://localhost:9313",
// secure: false,
// changeOrigin: true,
// pathRewrite: { '^/api' : '' }
// },
// '/admin':{
// target: "http://localhost:9313",
// secure: false,
// changeOrigin: true,
// pathRewrite: { '^/admin' : '' }
// }
// }
let proxyDev=[
{
context: [
'/api', '/admin'
],
target: 'http://localhost:9313',
secure: false,
changeOrigin: true
}
]
let proxyProd = {
'/api':{
target: "http://localhost:9313",
secure: false,
changeOrigin: true,
pathRewrite: { '^/api' : '' }
}
}
if(env.NODE_ENV==="dev"){
proxy = proxyDev;
devtool = "cheap-module-eval-source-map";
}else if(env.NODE_ENV==="prod"){
proxy = proxyProd;
devtool = "cheap-module-eval-source-map";
}
console.log(proxy)
module.exports = {
entry: __dirname + '/src/index.js',//入口文件
output: {
path: __dirname + '/dist',//打包后的文件位置
filename: 'index.js'//打包后的文件
},
devServer: {
contentBase: './src',//默认本地服务器在跟目录
historyApiFallback: true,//不跳转,默认会跳转且都跳到index.html上
inline: true,//源文件改变时刷新页面
hot: true,
port: 8085//更改端口号,默认8080
,proxy
},
devtool,//好几种模式
mode:'development',
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [{
loader: "babel-loader",
options: {presets: ["react", "es2015", "stage-0"]}
}],
},
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {}
}
]
},
{
test: /\.(scss|css)$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: ['css-loader', 'sass-loader']
})
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new CompressionWebpackPlugin({
filename: '[path].gz[query]',// 目标文件名,1.x 这个配置项为asset
algorithm: 'gzip',// 使用gzip压缩
test: /\.js$|\.html$/,
threshold: 10240,// 资源文件大于10240B=10kB时会被压缩
minRatio: 0.8 // 最小压缩比达到0.8时才会被压缩
}),
new ExtractTextPlugin("index.css"),
new copyWebpackPlugin([{
from: __dirname + '/README.md',
to: __dirname + '/docs/README.md',
}]),
new copyWebpackPlugin([{
from: __dirname + '/README.md',
to:'./docs' //打包到 dist 下面的public
}]),
]
}