-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
121 lines (117 loc) · 4.2 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
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');//模板
const CleanWebpackPlugin = require('clean-webpack-plugin');//清理
const CopyWebpackPlugin = require('copy-webpack-plugin');//拷贝
const MiniCssExtractPlugin = require("mini-css-extract-plugin");//提取css
const tsImportPluginFactory = require('ts-import-plugin');//按需导入
// 打包时间戳
const time = +new Date();
// 分离css
const styleCss = new MiniCssExtractPlugin({
filename: `css/style.css?t=${time}`,
// chunkFilename: "css/[id].css"
});
// 源代码根路径
const srcPath = path.resolve(__dirname, "src");
// 插件
let plugins = [
// 把生成的文件插入到 启动页中
new HtmlWebpackPlugin({
template: './src/index.html', options: {
minimize: true
}
}),
// new CopyWebpackPlugin([{
// from: 'src/assets',
// to: 'assets'
// }]),
styleCss
];
module.exports = (evn = {}) => {
evn.Generative = evn.Generative == "true"
console.log(`-------------------------------------- ${evn.Generative ? '生产' : '开发'} --------------------------------------`);
// 生产环境 添加需要的插件
if (evn.Generative) {
plugins = [
// 清理目录
new CleanWebpackPlugin(['build']),
...plugins
]
}
const Config = {
entry: {
'app': './src/index.tsx' //应用程序
},
output: {
path: path.resolve(__dirname, "build"),
publicPath: evn.Generative ? './' : '/',
filename: `js/[name].js?t=${time}`,
chunkFilename: `js/[name].js?t=${time}`
},
devServer: {
inline: true, //热更新
port: "8001",
compress: true,//为服务的所有内容启用gzip压缩:
// hotOnly: true,//在构建失败的情况下启用无需刷新页面作为回退的热模块替换
// lazy: true,//懒惰模式
overlay: true,//显示错误
proxy: {
},
//404 页面返回 index.html
historyApiFallback: true,
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".json"],
},
module: {
rules: [
{
test: /\.(tsx|ts|js|jsx)$/,
loader: 'awesome-typescript-loader',
options: {
// 按需加载 ts 文件
// getCustomTransformers: () => ({
// before: [tsImportPluginFactory({ libraryName: 'antd-mobile', style: false })]
// }),
},
exclude: /node_modules/
},
{
test: /\.(less|css)$/,
use: [
MiniCssExtractPlugin.loader,
`css-loader?sourceMap=true&minimize=${evn.Generative}`,
`less-loader?sourceMap=true&javascriptEnabled=true`
]
},
{
test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
loader: 'url-loader?limit=50000&name=[path][name].[ext]'
},
]
},
};
return [{
...Config,
// 打包模式 development。启用NamedModulesPlugin。 production。启用UglifyJsPlugin,ModuleConcatenationPlugin和NoEmitOnErrorsPlugin。
mode: evn.Generative ? 'production' : 'development',
// 开发环境 生成 map 文件
devtool: evn.Generative ? false : 'source-map',
// webpack 4删除了CommonsChunkPlugin,以支持两个新选项(optimization.splitChunks和optimization.runtimeChunk)
// https://gist.github.com/sokra/1522d586b8e5c0f5072d7565c2bee693
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: "vendors",
chunks: "all"
}
},
}
},
plugins,
}
]
}