-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwebpack.config.js
118 lines (115 loc) · 2.85 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
const fs = require('fs');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const autoprefixer = require('autoprefixer');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const resolveApp = relativePath => path.resolve(__dirname, relativePath);
const babelLoader = require.resolve('babel-loader');
const urlLoader = {
loader: require.resolve('url-loader'),
options: {
limit: 8192, // 小于8k的图片自动转成base64格式,并且不会存在实体图片
outputPath: 'assets/img/' // 图片打包后存放的目录
}
};
const fileLoader = {
loader: require.resolve('file-loader'),
options: {
outputPath: 'fonts/'
}
};
const cssLoader = {
loader: require.resolve('css-loader'),
options: {
minimize: true,
importLoaders: 2
}
};
const postcssLoader = {
loader: require.resolve('postcss-loader'),
options: {
plugins: () => [autoprefixer]
}
};
const sassLoader = {
loader: require.resolve('sass-loader'),
options: {
outputStyle: 'expanded'
}
};
const miniCssExtractPlugin = new MiniCssExtractPlugin({
filename: 'css/[name].[hash:4].css'
});
const htmlWebpackPlugin = new HtmlWebpackPlugin({
inject: false,
template: resolveApp('./src/page/index.html'),
favicon: resolveApp('./src/assets/favicon.png'),
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
minifyHTML: true
}
});
const terserPlugin = new TerserPlugin({
terserOptions: {
ie8: true,
keep_fnames: true
}
});
const cleanWebpackPlugin = new CleanWebpackPlugin('dist/**')
module.exports = {
mode: 'production',
entry: {
index: resolveApp('./src/page/index.js'),
'es5-polyfill': 'es5-polyfill'
},
output: {
path: resolveApp('./dist'),
filename: 'js/[name].[hash:4].js',
publicPath: '/'
},
devtool: 'source-map',
resolve: {
symlinks: true,
extensions: ['.js', '.jsx', '.ts', '.tsx'],
alias: {
react: 'nervjs',
'react-dom': 'nervjs'
}
},
module: {
rules: [
{
test: /\.[tj]sx?$/i,
exclude: /node_modules/,
loader: babelLoader
},
{
test: /\.(jpe?g|png|gif)$/,
use: [urlLoader]
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: fileLoader
},
{
test: /\.s?css$/,
use: [MiniCssExtractPlugin.loader, cssLoader, postcssLoader, sassLoader]
}
]
},
plugins: [cleanWebpackPlugin, htmlWebpackPlugin, miniCssExtractPlugin],
optimization: {
minimizer: [uglifyJSPlugin]
}
};