-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
executable file
·96 lines (92 loc) · 2.55 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
const webpack = require('webpack');
const merge = require('webpack-merge');
const { resolve, join } = require('path');
const progressBarPlugin = require('progress-bar-webpack-plugin'); //webpack编译进度条
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); //将css文件单独提取出来
const argv = require('yargs-parser')(process.argv.slice(2));
const _mode = argv.mode || 'development'; //获取环境变量,判断是开发还是生产环境
const _isDev = _mode === 'development';
const _mergeConfig = require(`./config/webpack.${_mode}.js`);
const env = require('./config/env.js')[_mode];
const cssLoaders = require('./config/cssLoader.js');
const baseCssLoaders = _isDev ? ['style-loader'] : [MiniCssExtractPlugin.loader];
const imagesLoaders = [
{
loader: 'url-loader',
options: {
limit: 10 * 1024,
name: _isDev ? 'images/[name].[ext]' : 'images/[name].[hash:5].[ext]',
publicPath: env.publicPath
}
}
];
const baseConfig = {
mode: _mode,
target: "web",
entry: {
app: join(__dirname, "./src/index.jsx")
},
output: {
path: join(__dirname, 'dist'),
filename: _isDev ? "scripts/[name].js" : "scripts/[name].[chunkhash:5].js",
publicPath: env.publicPath
},
resolve: {
extensions: ['.jsx', '.js'],
alias: {
'@pages': resolve(__dirname, './src/pages'),
'@components': resolve(__dirname, './src/components'),
'@images': resolve(__dirname, './src/assets/images'),
},
modules: [resolve('src'), 'node_modules']
},
module: {
rules: [
{
test: /\.(jsx|js)$/,
include: [resolve("src")],
exclude: /node_modules/,
loader: 'babel-loader',
options: {
//开发环境不启用路由拆分
plugins: _isDev ? ['dynamic-import-node'] : []
}
},
{
test: /.svg$/,
use: ['@svgr/webpack', 'url-loader'],
},
{
test: /\.(png|jpg|gif|svg|bmp|eot|woff|woff2|ttf)/,
use: imagesLoaders
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10 * 1024,
name: _isDev ? 'medias/[name].[ext]' : 'medias/[name].[hash:5].[ext]',
publicPath: env.publicPath
}
},
{
test: /\.(html|htm)/,
loader: 'html-withimg-loader'
},
{
test: /\.(sa|sc|c)ss$/,
use: [
...baseCssLoaders,
...cssLoaders
]
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(_mode),
}),
new progressBarPlugin(),
]
};
module.exports = merge(baseConfig, _mergeConfig);