-
Notifications
You must be signed in to change notification settings - Fork 370
Expand file tree
/
Copy pathconfig-overrides.js
More file actions
252 lines (227 loc) · 6.92 KB
/
config-overrides.js
File metadata and controls
252 lines (227 loc) · 6.92 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const paths = require("react-scripts/config/paths");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { WebpackManifestPlugin } = require("webpack-manifest-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require("terser-webpack-plugin");
// const webpack = require("webpack");
console.log("process.env.REACT_APP_CLIENT", process.env.REACT_APP_CLIENT);
// 扩展
const extWebpack = (config, env) => {
const isEnvProduction = env === "production";
const minify = isEnvProduction && {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
};
const names = [
"HtmlWebpackPlugin",
"WebpackManifestPlugin",
"MiniCssExtractPlugin",
];
config.entry = {
popup: paths.appSrc + "/popup.js",
options: paths.appSrc + "/options.js",
background: paths.appSrc + "/background.js",
content: paths.appSrc + "/content.js",
"injector-subtitle": paths.appSrc + "/injector-subtitle.js",
"injector-shadowroot": paths.appSrc + "/injector-shadowroot.js",
};
config.output.filename = "[name].js";
config.output.assetModuleFilename = "media/[name][ext]";
config.optimization.splitChunks = { cacheGroups: { default: false } };
config.optimization.runtimeChunk = false;
config.plugins = config.plugins.filter(
(plugin) => !names.includes(plugin.constructor.name)
);
config.plugins.push(
new HtmlWebpackPlugin({
inject: true,
chunks: ["options"],
template: paths.appHtml,
filename: "options.html",
minify,
}),
new HtmlWebpackPlugin({
inject: true,
chunks: ["popup"],
template: paths.appHtml,
filename: "popup.html",
minify,
}),
new WebpackManifestPlugin({
fileName: "asset-manifest.json",
}),
new MiniCssExtractPlugin({
filename: "css/[name].css",
})
);
return config;
};
// 油猴
const userscriptWebpack = (config, env) => {
const banner = `// ==UserScript==
// @name ${process.env.REACT_APP_NAME}
// @namespace ${process.env.REACT_APP_HOMEPAGE}
// @version ${process.env.REACT_APP_VERSION}
// @description A simple bilingual translation extension & Greasemonkey script (一个简约的双语对照翻译扩展 & 油猴脚本)
// @author Gabe<yugang2002@gmail.com>
// @homepageURL ${process.env.REACT_APP_HOMEPAGE}
// @license GPL-3.0
// @match *://*/*
// @icon ${process.env.REACT_APP_LOGOURL}
// @downloadURL ${process.env.REACT_APP_USERSCRIPT_DOWNLOADURL}
// @updateURL ${process.env.REACT_APP_USERSCRIPT_DOWNLOADURL}
// @grant GM.xmlHttpRequest
// @grant GM_xmlhttpRequest
// @grant GM.registerMenuCommand
// @grant GM_registerMenuCommand
// @grant GM.unregisterMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM.setValue
// @grant GM_setValue
// @grant GM.getValue
// @grant GM_getValue
// @grant GM.deleteValue
// @grant GM_deleteValue
// @grant GM.info
// @grant GM_info
// @grant unsafeWindow
// @connect translate.googleapis.com
// @connect translate-pa.googleapis.com
// @connect generativelanguage.googleapis.com
// @connect api-edge.cognitive.microsofttranslator.com
// @connect edge.microsoft.com
// @connect bing.com
// @connect api-free.deepl.com
// @connect api.deepl.com
// @connect www2.deepl.com
// @connect api.openai.com
// @connect generativelanguage.googleapis.com
// @connect openai.azure.com
// @connect workers.dev
// @connect github.io
// @connect github.com
// @connect githubusercontent.com
// @connect kiss-translator.rayjar.com
// @connect ghproxy.com
// @connect dav.jianguoyun.com
// @connect fanyi.baidu.com
// @connect transmart.qq.com
// @connect niutrans.com
// @connect translate.volcengine.com
// @connect dict.youdao.com
// @connect api.anthropic.com
// @connect api.siliconflow.cn
// @connect api.cloudflare.com
// @connect openrouter.ai
// @connect localhost
// @connect 127.0.0.1
// @run-at document-end
// ==/UserScript==
`;
const names = ["HtmlWebpackPlugin"];
config.entry = {
main: paths.appIndexJs,
options: paths.appSrc + "/options.js",
"kiss-translator.user": paths.appSrc + "/userscript.js",
};
config.output.filename = "[name].js";
config.output.publicPath = env === "production" ? "./" : "/";
config.optimization.splitChunks = { cacheGroups: { default: false } };
config.optimization.runtimeChunk = false;
config.optimization.minimize = env === "production";
if (config.optimization.minimize) {
config.optimization.minimizer = [
new TerserPlugin({
extractComments: false,
terserOptions: {
format: {
comments: false,
preamble: banner,
},
},
}),
];
}
if (env === "production") config.devtool = false;
config.plugins = config.plugins.filter(
(plugin) => !names.includes(plugin.constructor.name)
);
config.plugins.push(
new HtmlWebpackPlugin({
inject: true,
chunks: ["main"],
template: paths.appHtml,
filename: "index.html",
}),
new HtmlWebpackPlugin({
inject: true,
chunks: ["options"],
template: paths.appHtml,
filename: "options.html",
})
// new webpack.BannerPlugin({
// banner,
// raw: true,
// entryOnly: true,
// include: "kiss-translator.user",
// })
);
return config;
};
// 开发
const webWebpack = (config, env) => {
const names = ["HtmlWebpackPlugin"];
config.entry = {
main: paths.appIndexJs,
options: paths.appSrc + "/options.js",
content: paths.appSrc + "/userscript.js",
};
config.output.filename = "[name].js";
config.output.publicPath = "/";
config.plugins = config.plugins.filter(
(plugin) => !names.includes(plugin.constructor.name)
);
config.plugins.push(
new HtmlWebpackPlugin({
inject: true,
chunks: ["main"],
template: paths.appHtml,
filename: "index.html",
}),
new HtmlWebpackPlugin({
inject: true,
chunks: ["options"],
template: paths.appHtml,
filename: "options.html",
}),
new HtmlWebpackPlugin({
inject: true,
chunks: ["content"],
template: paths.appPublic + "/content.html",
filename: "content.html",
})
);
return config;
};
let webpackConfig;
switch (process.env.REACT_APP_CLIENT) {
case "userscript":
webpackConfig = userscriptWebpack;
break;
case "web":
webpackConfig = webWebpack;
break;
default:
webpackConfig = extWebpack;
}
module.exports = {
webpack: webpackConfig,
};