Files
kiss-translator/config-overrides.js

227 lines
6.2 KiB
JavaScript
Raw Normal View History

2023-07-20 13:45:41 +08:00
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");
2023-08-05 15:32:51 +08:00
const webpack = require("webpack");
2023-08-07 12:28:05 +08:00
console.log("process.env.REACT_APP_CLIENT", process.env.REACT_APP_CLIENT);
2023-08-05 15:32:51 +08:00
// 扩展
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 = {
2023-08-09 16:30:08 +08:00
popup: paths.appSrc + "/popup.js",
2023-08-05 15:32:51 +08:00
options: paths.appSrc + "/options.js",
background: paths.appSrc + "/background.js",
content: paths.appSrc + "/content.js",
2025-10-07 16:35:00 +08:00
injector: paths.appSrc + "/injector.js",
2023-08-05 15:32:51 +08:00
};
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) => {
2023-08-06 21:12:01 +08:00
const banner = `// ==UserScript==
2023-08-10 22:41:35 +08:00
// @name ${process.env.REACT_APP_NAME}
2023-08-06 21:12:01 +08:00
// @namespace ${process.env.REACT_APP_HOMEPAGE}
// @version ${process.env.REACT_APP_VERSION}
2023-10-13 10:20:14 +08:00
// @description A simple bilingual translation extension & Greasemonkey script (一个简约的双语对照翻译扩展 & 油猴脚本)
2023-08-06 21:12:01 +08:00
// @author Gabe<yugang2002@gmail.com>
// @homepageURL ${process.env.REACT_APP_HOMEPAGE}
2023-08-10 21:38:49 +08:00
// @license GPL-3.0
2023-08-06 21:12:01 +08:00
// @match *://*/*
2023-08-09 20:51:43 +08:00
// @icon ${process.env.REACT_APP_LOGOURL}
// @downloadURL ${process.env.REACT_APP_USERSCRIPT_DOWNLOADURL}
// @updateURL ${process.env.REACT_APP_USERSCRIPT_DOWNLOADURL}
2023-08-18 13:19:40 +08:00
// @grant GM.xmlHttpRequest
2023-08-21 14:03:39 +08:00
// @grant GM.registerMenuCommand
2023-12-15 10:58:49 +08:00
// @grant GM.unregisterMenuCommand
2023-08-06 21:12:01 +08:00
// @grant GM.setValue
// @grant GM.getValue
// @grant GM.deleteValue
2023-08-21 23:50:14 +08:00
// @grant GM.info
2023-08-31 20:57:51 +08:00
// @grant unsafeWindow
2023-08-06 21:12:01 +08:00
// @connect translate.googleapis.com
// @connect translate-pa.googleapis.com
2025-10-11 11:19:33 +08:00
// @connect generativelanguage.googleapis.com
2023-08-06 21:12:01 +08:00
// @connect api-edge.cognitive.microsofttranslator.com
// @connect edge.microsoft.com
2025-10-13 11:29:22 +08:00
// @connect bing.com
2023-09-03 00:10:07 +08:00
// @connect api-free.deepl.com
// @connect api.deepl.com
// @connect www2.deepl.com
2023-08-06 21:12:01 +08:00
// @connect api.openai.com
2023-12-21 14:15:14 +08:00
// @connect generativelanguage.googleapis.com
2023-08-15 12:02:45 +08:00
// @connect openai.azure.com
2023-08-15 12:19:48 +08:00
// @connect workers.dev
2023-08-20 19:27:29 +08:00
// @connect github.io
2025-10-14 22:41:18 +08:00
// @connect github.com
2023-08-20 19:27:29 +08:00
// @connect githubusercontent.com
// @connect kiss-translator.rayjar.com
2023-08-21 21:35:53 +08:00
// @connect ghproxy.com
2023-09-19 11:56:19 +08:00
// @connect dav.jianguoyun.com
2023-10-11 09:48:52 +08:00
// @connect fanyi.baidu.com
// @connect transmart.qq.com
// @connect niutrans.com
2025-06-30 21:34:37 +08:00
// @connect translate.volcengine.com
2025-10-11 11:19:33 +08:00
// @connect dict.youdao.com
// @connect api.anthropic.com
// @connect api.cloudflare.com
// @connect openrouter.ai
2025-07-01 17:55:57 +08:00
// @connect localhost
// @connect 127.0.0.1
// @run-at document-end
2023-08-06 21:12:01 +08:00
// ==/UserScript==
2023-08-05 15:32:51 +08:00
2023-08-06 21:12:01 +08:00
`;
2023-08-05 15:32:51 +08:00
2023-08-09 16:30:08 +08:00
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";
2023-08-09 17:33:51 +08:00
config.output.publicPath = env === "production" ? "./" : "/";
2023-08-05 15:32:51 +08:00
config.optimization.splitChunks = { cacheGroups: { default: false } };
config.optimization.runtimeChunk = false;
config.optimization.minimize = false;
2023-08-09 16:30:08 +08:00
config.plugins = config.plugins.filter(
(plugin) => !names.includes(plugin.constructor.name)
);
2023-08-06 21:12:01 +08:00
config.plugins.push(
2023-08-09 16:30:08 +08:00
new HtmlWebpackPlugin({
inject: true,
chunks: ["main"],
template: paths.appHtml,
filename: "index.html",
}),
new HtmlWebpackPlugin({
inject: true,
chunks: ["options"],
template: paths.appHtml,
filename: "options.html",
}),
2023-08-06 21:12:01 +08:00
new webpack.BannerPlugin({
banner,
raw: true,
entryOnly: true,
2023-08-09 16:30:08 +08:00
include: "kiss-translator.user",
2023-08-06 21:12:01 +08:00
})
);
return config;
};
2023-08-09 16:34:15 +08:00
// 开发
2023-08-06 21:12:01 +08:00
const webWebpack = (config, env) => {
const names = ["HtmlWebpackPlugin"];
config.entry = {
2023-08-09 16:30:08 +08:00
main: paths.appIndexJs,
options: paths.appSrc + "/options.js",
content: paths.appSrc + "/userscript.js",
2023-08-06 21:12:01 +08:00
};
2023-08-06 21:45:05 +08:00
config.output.filename = "[name].js";
2023-08-09 16:30:08 +08:00
config.output.publicPath = "/";
2023-08-06 21:45:05 +08:00
2023-08-05 15:32:51 +08:00
config.plugins = config.plugins.filter(
(plugin) => !names.includes(plugin.constructor.name)
);
config.plugins.push(
2023-08-05 20:11:02 +08:00
new HtmlWebpackPlugin({
inject: true,
2023-08-06 21:12:01 +08:00
chunks: ["main"],
2023-08-05 20:11:02 +08:00
template: paths.appHtml,
2023-08-06 21:12:01 +08:00
filename: "index.html",
2023-08-05 20:11:02 +08:00
}),
2023-08-06 21:12:01 +08:00
new HtmlWebpackPlugin({
inject: true,
chunks: ["options"],
template: paths.appHtml,
filename: "options.html",
2023-08-09 16:30:08 +08:00
}),
new HtmlWebpackPlugin({
inject: true,
chunks: ["content"],
template: paths.appPublic + "/content.html",
filename: "content.html",
2023-08-05 15:32:51 +08:00
})
);
return config;
};
2023-07-20 13:45:41 +08:00
2023-08-06 21:12:01 +08:00
let webpackConfig;
switch (process.env.REACT_APP_CLIENT) {
case "userscript":
webpackConfig = userscriptWebpack;
break;
case "web":
webpackConfig = webWebpack;
break;
default:
webpackConfig = extWebpack;
}
2023-07-20 13:45:41 +08:00
module.exports = {
2023-08-06 21:12:01 +08:00
webpack: webpackConfig,
2023-08-04 07:57:25 +08:00
};