2022-07-01 03:39:28 +02:00
|
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
2022-02-06 03:20:53 +01:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2022-07-01 03:39:28 +02:00
|
|
|
|
const webpack = require("webpack");
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const CopyPlugin = require('copy-webpack-plugin');
|
|
|
|
|
const BuildManifest = require('./webpack.manifest');
|
|
|
|
|
const srcDir = '../src/';
|
|
|
|
|
const fs = require("fs");
|
|
|
|
|
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
|
2022-10-07 23:12:16 +02:00
|
|
|
|
const configDiffPlugin = require('./configDiffPlugin');
|
2020-01-29 04:16:48 +01:00
|
|
|
|
|
2022-01-14 22:07:24 +01:00
|
|
|
|
const edgeLanguages = [
|
|
|
|
|
"de",
|
|
|
|
|
"en",
|
|
|
|
|
"es",
|
|
|
|
|
"fr",
|
|
|
|
|
"pl",
|
|
|
|
|
"pt_BR",
|
|
|
|
|
"ro",
|
|
|
|
|
"ru",
|
|
|
|
|
"sk",
|
|
|
|
|
"sv",
|
|
|
|
|
"tr",
|
|
|
|
|
"uk",
|
|
|
|
|
"zh_CN"
|
|
|
|
|
]
|
|
|
|
|
|
2023-03-18 02:10:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = env => {
|
|
|
|
|
const documentScriptBuild = webpack({
|
|
|
|
|
entry: {
|
2023-03-20 19:14:28 +01:00
|
|
|
|
document: path.join(__dirname, srcDir + 'document.ts')
|
2023-03-18 02:10:26 +01:00
|
|
|
|
},
|
|
|
|
|
output: {
|
|
|
|
|
path: path.join(__dirname, '../dist/js'),
|
|
|
|
|
},
|
|
|
|
|
module: {
|
|
|
|
|
rules: [
|
2022-01-14 21:56:38 +01:00
|
|
|
|
{
|
2023-03-18 02:10:26 +01:00
|
|
|
|
test: /\.tsx?$/,
|
|
|
|
|
loader: 'ts-loader',
|
|
|
|
|
exclude: /node_modules/,
|
|
|
|
|
resourceQuery: { not: [/raw/] },
|
|
|
|
|
options: {
|
|
|
|
|
// disable type checker for user in fork plugin
|
|
|
|
|
transpileOnly: true,
|
|
|
|
|
configFile: env.mode === "production" ? "tsconfig-production.json" : "tsconfig.json"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
resolve: {
|
|
|
|
|
extensions: ['.ts', '.tsx', '.js']
|
|
|
|
|
},
|
|
|
|
|
plugins: [
|
|
|
|
|
// Don't fork TS checker for document script to speed up
|
|
|
|
|
// new ForkTsCheckerWebpackPlugin()
|
|
|
|
|
]
|
|
|
|
|
});
|
2022-01-14 22:07:24 +01:00
|
|
|
|
|
2023-03-18 02:10:26 +01:00
|
|
|
|
class DocumentScriptCompiler {
|
|
|
|
|
currentWatching = null;
|
2022-01-14 21:56:38 +01:00
|
|
|
|
|
2023-03-18 02:10:26 +01:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {webpack.Compiler} compiler
|
|
|
|
|
*/
|
|
|
|
|
apply(compiler) {
|
|
|
|
|
compiler.hooks.beforeCompile.tapAsync({ name: 'DocumentScriptCompiler' }, (compiler, callback) => {
|
|
|
|
|
if (env.WEBPACK_WATCH) {
|
|
|
|
|
let first = true;
|
|
|
|
|
if (!this.currentWatching) {
|
|
|
|
|
this.currentWatching = documentScriptBuild.watch({}, () => {
|
|
|
|
|
if (first) {
|
|
|
|
|
first = false;
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
documentScriptBuild.close(() => {
|
|
|
|
|
documentScriptBuild.run(() => {
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
entry: {
|
|
|
|
|
popup: path.join(__dirname, srcDir + 'popup.ts'),
|
|
|
|
|
background: path.join(__dirname, srcDir + 'background.ts'),
|
|
|
|
|
content: path.join(__dirname, srcDir + 'content.ts'),
|
|
|
|
|
options: path.join(__dirname, srcDir + 'options.ts'),
|
|
|
|
|
help: path.join(__dirname, srcDir + 'help.ts'),
|
|
|
|
|
permissions: path.join(__dirname, srcDir + 'permissions.ts'),
|
|
|
|
|
},
|
|
|
|
|
output: {
|
|
|
|
|
path: path.join(__dirname, '../dist/js'),
|
|
|
|
|
},
|
|
|
|
|
module: {
|
|
|
|
|
rules: [
|
|
|
|
|
{
|
|
|
|
|
test: /\.tsx?$/,
|
|
|
|
|
loader: 'ts-loader',
|
|
|
|
|
exclude: /node_modules/,
|
|
|
|
|
resourceQuery: { not: [/raw/] },
|
|
|
|
|
options: {
|
|
|
|
|
// disable type checker for user in fork plugin
|
|
|
|
|
transpileOnly: true,
|
|
|
|
|
configFile: env.mode === "production" ? "tsconfig-production.json" : "tsconfig.json"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-03-24 22:40:29 +01:00
|
|
|
|
test: /js(\/|\\)document\.js$/,
|
2023-03-18 02:10:26 +01:00
|
|
|
|
type: 'asset/source'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
resolve: {
|
2023-06-30 08:46:27 +02:00
|
|
|
|
extensions: ['.ts', '.tsx', '.js'],
|
|
|
|
|
symlinks: false
|
2023-03-18 02:10:26 +01:00
|
|
|
|
},
|
|
|
|
|
plugins: [
|
|
|
|
|
// Prehook to start building document script before normal build
|
|
|
|
|
new DocumentScriptCompiler(),
|
|
|
|
|
// fork TS checker
|
|
|
|
|
new ForkTsCheckerWebpackPlugin(),
|
|
|
|
|
// exclude locale files in moment
|
|
|
|
|
new CopyPlugin({
|
|
|
|
|
patterns: [
|
|
|
|
|
{
|
|
|
|
|
from: '.',
|
|
|
|
|
to: '../',
|
|
|
|
|
globOptions: {
|
2023-05-09 22:06:56 +02:00
|
|
|
|
ignore: ['manifest.json', '**/.git/**', '**/crowdin.yml'],
|
2023-03-18 02:10:26 +01:00
|
|
|
|
},
|
|
|
|
|
context: './public',
|
|
|
|
|
filter: async (path) => {
|
2023-03-24 22:40:29 +01:00
|
|
|
|
if (path.match(/(\/|\\)_locales(\/|\\).+/)) {
|
2023-03-18 02:10:26 +01:00
|
|
|
|
if (env.browser.toLowerCase() === "edge"
|
|
|
|
|
&& !edgeLanguages.includes(path.match(/(?<=\/_locales\/)[^/]+(?=\/[^/]+$)/)[0])) {
|
|
|
|
|
return false;
|
2022-01-14 21:56:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-18 02:10:26 +01:00
|
|
|
|
const data = await fs.promises.readFile(path);
|
|
|
|
|
const parsed = JSON.parse(data.toString());
|
|
|
|
|
|
|
|
|
|
return parsed.fullName && parsed.Description;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
transform(content, path) {
|
2023-03-24 22:40:29 +01:00
|
|
|
|
if (path.match(/(\/|\\)_locales(\/|\\).+/)) {
|
2023-03-18 02:10:26 +01:00
|
|
|
|
const parsed = JSON.parse(content.toString());
|
|
|
|
|
if (env.browser.toLowerCase() === "safari") {
|
2024-01-17 00:37:37 +01:00
|
|
|
|
parsed.fullName.message = parsed.fullName.message.match(/^.+(?= [-–])/)?.[0] || parsed.fullName.message;
|
2023-03-18 02:10:26 +01:00
|
|
|
|
if (parsed.fullName.message.length > 50) {
|
|
|
|
|
parsed.fullName.message = parsed.fullName.message.slice(0, 47) + "...";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parsed.Description.message = parsed.Description.message.match(/^.+(?=\. )/)?.[0] || parsed.Description.message;
|
|
|
|
|
if (parsed.Description.message.length > 80) {
|
|
|
|
|
parsed.Description.message = parsed.Description.message.slice(0, 77) + "...";
|
|
|
|
|
}
|
2022-01-14 21:56:38 +01:00
|
|
|
|
}
|
2023-03-18 02:10:26 +01:00
|
|
|
|
|
|
|
|
|
return Buffer.from(JSON.stringify(parsed));
|
2022-01-14 21:56:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-18 02:10:26 +01:00
|
|
|
|
return content;
|
|
|
|
|
}
|
2022-01-14 21:56:38 +01:00
|
|
|
|
}
|
2023-03-18 02:10:26 +01:00
|
|
|
|
]
|
|
|
|
|
}),
|
|
|
|
|
new BuildManifest({
|
|
|
|
|
browser: env.browser,
|
|
|
|
|
pretty: env.mode === "production",
|
|
|
|
|
stream: env.stream
|
|
|
|
|
}),
|
|
|
|
|
new configDiffPlugin()
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
};
|