-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-overrides.js
200 lines (199 loc) · 6.68 KB
/
config-overrides.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const { override, fixBabelImports, addWebpackAlias, addWebpackPlugin, addBundleVisualizer, addLessLoader, setWebpackPublicPath, addWebpackModuleRule, overrideDevServer, addPostcssPlugins} = require('customize-cra')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ReplacePlugin = require('webpack-plugin-replace')
const ReplaceInFileWebpackPlugin = require('replace-in-file-webpack-plugin')
const FileManagerPlugin = require('filemanager-webpack-plugin')
const rewireHtmlWebpackPlugin = require('react-app-rewire-html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const dayjs = require('dayjs')
const webpack = require('webpack')
const { paths } = require('react-app-rewired')
const path = require('path')
const fs = require('fs')
const select = fs.readFileSync('./conf/select.js', 'utf8')
let timeStamp = process.env.npm_config_appversion || dayjs().format('YYMMDD')
const removeConsole = () => {
return (config) => {
if(config.optimization.minimizer){
config.optimization.minimizer.forEach( (minimizer) => {
if( minimizer.constructor.name === 'TerserPlugin'){
// minimizer.options.terserOptions.compress.drop_console = true // 包括log,error, warn等
minimizer.options.terserOptions.compress.pure_funcs = ['console.log']
}
})
}
return config;
}
}
const addWebpackPlugins = (plugins = []) => (config) => {
for (let val of plugins) {
config.plugins.push(val)
}
return config
}
const replaceOutputName = () => (config) => {
config.output.filename = 'static/js/[name].js'; // static/js/[name].[contenthash:8].js
config.output.chunkFilename = 'static/js/[name].chunk.js'; // static/js/[name].[contenthash:8].chunk.js
// change MiniCssExtractPlugin
config.plugins = config.plugins.filter(
p => p.constructor.name !== 'MiniCssExtractPlugin',
);
config.plugins.push(new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: 'static/css/[name].css',
chunkFilename: 'static/css/[name].chunk.css'
}));
// Media and Assets Overrides
config.module.rules[2].oneOf = config.module.rules[2].oneOf.map((one) => {
if (one.options && one.options.name) {
one.options.name = 'static/media/[name].[ext]';
}
return one;
});
// ②less文件url路径
config.module.rules[2].oneOf[7].use[0].options = {...config.module.rules[2].oneOf[7].use[0].options, publicPath: '../../'}
// ②css文件url路径
config.module.rules[2].oneOf[3].use[0].options = {...config.module.rules[2].oneOf[3].use[0].options, publicPath: '../../'}
console.log(config.module.rules[2])
// 单独设置
// config.module.rules[2].oneOf.splice(0,1,{
// test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
// use: [{
// loader: require.resolve('url-loader'),
// options: {
// limit: 10000,
// name: 'static/images/[name].[hash:8].[ext]'
// }
// }]
// });
return config;
}
const removeHtmlWebpackPlugin = () => (config) => {
// copy原来的配置, 再删除, react-app-rewire-html-webpack-plugin代替此处
const htmlWebpackPluginOptions = config.plugins.find(
plugin => plugin.constructor.name === 'HtmlWebpackPlugin'
)
htmlWebpackPluginOptions.options = {...htmlWebpackPluginOptions.options, ...{
ejs: select
}}
return config
// htmlWebpackPluginOptions = config.plugins[index].options
// console.log(htmlWebpackPluginOptions)
// config.plugins.splice(index, 1)
}
module.exports = {
webpack: override(
// 废弃, 采用②
// ①
// process.env.NODE_ENV === 'production' && addWebpackModuleRule(
// {
// test: /\.css$/,
// use: [
// {
// loader: MiniCssExtractPlugin.loader,
// options: {
// publicPath: '../'
// }
// },
// 'css-loader'
// ]
// }
// ),
fixBabelImports('import', {
libraryName: 'antd-mobile',
style: 'css'
}),
addLessLoader({
lessOptions: {
javascriptEnabled: true,
modifyVars: {
'hack': `true; @import "~@/assets/less/var.less";`
}
}
}),
addPostcssPlugins([
require('postcss-px-to-viewport')({
viewportWidth: 375,
exclude: /(\/|\\)(node_modules)(\/|\\)/
})
]),
addWebpackAlias ({
'@': paths.appSrc
}),
addWebpackPlugins([
new webpack.DefinePlugin({
'APPVERSION': timeStamp
}),
// new HtmlWebpackPlugin({
// filename: 'index.html',
// template: 'public/index.html',
// inject: true,
// title: 'add title',
// ejs: select
// }),
new ReplacePlugin({
values: {
'CSSREPLACE': 'px'
}
}),
new CopyWebpackPlugin({patterns: [{from: 'conf/**'}]}),
process.env.NODE_ENV === 'production' ? new FileManagerPlugin({
onEnd: {
mkdir: [`./tar`],
archive: [
{
source: `${path.resolve(__dirname, 'build')}`,
destination: `./tar/build.tar.gz`,
format: 'tar',
options: {
gzip: true,
gzipOptions: {
level: 9
},
globOptions: {
nomount: true
}
}
}
]
}
}) : () => {} // 防止npm run start错误
]),
// process.env.NODE_ENV === 'production' && addWebpackPlugin(
// new ReplaceInFileWebpackPlugin([{
// dir: paths.appBuild,
// test: [/\.css$/, /\.js$/],
// rules: [{
// search: /"JSREPLACE"/g,
// replace: 'window.REPLACED'
// }]
// }])
// ),
(config, env) => {
rewireHtmlWebpackPlugin(config, env, {
title: 'add title',
ejs: select
})
// config.output.publicPath = 'https://luchanan.github.io/antd-mobile-cli/build/'
return config
},
// removeHtmlWebpackPlugin(),
removeConsole(),
addBundleVisualizer({}, true),
replaceOutputName(),
// ③, 当②与同时开启,且env没有定义PUBLIC_URL时,css内url被②覆盖,且index.html的PUBLIC_URL未被代替
// process.env.REACT_APP_DETAIL_ENV === 'prod' ? setWebpackPublicPath('https://luchanan.github.io/antd-mobile-cli/build/'): () => {} // index.html的PUBLIC_URL需要在env配置
),
// for development or production.
paths: function(paths, env) {
paths.appBuild = path.resolve(__dirname, 'build')
return paths;
},
devServer: overrideDevServer(
(config, env) => {
return config
}
)
}