SponsorBlock/webpack/webpack.common.js

111 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-01-29 04:16:48 +01:00
const webpack = require("webpack");
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
2020-02-04 07:32:58 +01:00
const BuildManifest = require('./webpack.manifest');
2020-01-29 04:16:48 +01:00
const srcDir = '../src/';
const fs = require("fs");
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"
]
2020-02-04 07:32:58 +01:00
module.exports = env => ({
2020-01-29 04:16:48 +01:00
entry: {
popup: path.join(__dirname, srcDir + 'popup.ts'),
background: path.join(__dirname, srcDir + 'background.ts'),
2020-02-04 04:34:43 +01:00
content: path.join(__dirname, srcDir + 'content.ts'),
options: path.join(__dirname, srcDir + 'options.ts'),
2021-08-20 06:04:42 +02:00
help: path.join(__dirname, srcDir + 'help.ts'),
permissions: path.join(__dirname, srcDir + 'permissions.ts')
2020-01-29 04:16:48 +01:00
},
output: {
path: path.join(__dirname, '../dist/js'),
filename: '[name].js'
},
optimization: {
splitChunks: {
name: 'vendor',
chunks: "initial"
}
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
plugins: [
// exclude locale files in moment
2020-08-28 03:27:43 +02:00
new CopyPlugin({
patterns: [
{
from: '.',
to: '../',
globOptions: {
ignore: ['manifest.json'],
},
context: './public',
filter: async (path) => {
if (path.match(/\/_locales\/.+/)) {
2022-01-14 22:07:24 +01:00
if (env.browser.toLowerCase() === "edge"
&& !edgeLanguages.includes(path.match(/(?<=\/_locales\/)[^/]+(?=\/[^/]+$)/)[0])) {
return false;
}
const data = await fs.promises.readFile(path);
const parsed = JSON.parse(data.toString());
return parsed.fullName && parsed.Description;
} else {
return true;
}
},
transform(content, path) {
if (path.match(/\/_locales\/.+/)) {
const parsed = JSON.parse(content.toString());
if (env.browser.toLowerCase() === "safari") {
parsed.fullName.message = parsed.fullName.message.match(/^.+(?= -)/)?.[0] || parsed.fullName.message;
if (parsed.fullName.message.length > 50) {
parsed.fullName.message = parsed.fullName.message.substr(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.substr(0, 77) + "...";
}
}
return Buffer.from(JSON.stringify(parsed));
}
return content;
}
}
]
2020-08-28 03:27:43 +02:00
}),
2020-02-04 07:32:58 +01:00
new BuildManifest({
browser: env.browser,
2020-02-20 17:39:06 +01:00
pretty: env.mode === "production",
stream: env.stream
2020-02-04 07:32:58 +01:00
})
2020-01-29 04:16:48 +01:00
]
2020-02-04 07:32:58 +01:00
});