aboutsummaryrefslogtreecommitdiffhomepage
path: root/webpack/webpack.common.js
blob: 4f751af2dc33cd027afee9834745ab2b2e3e22f4 (plain)
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
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import webpack from "webpack"
import path from "path"
import { fileURLToPath } from "url"
import CopyPlugin from "copy-webpack-plugin"
import BuildManifest from "./webpack.manifest.cjs";
const srcDir = "../src/";
import fs from "fs";
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const edgeLanguages = [
    "de",
    "en",
    "es",
    "fr",
    "pl",
    "pt_BR",
    "ro",
    "ru",
    "sk",
    "sv",
    "tr",
    "uk",
    "zh_CN"
]

export default env => ({
    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'),
    },
    optimization: {
        splitChunks: {
            name: 'vendor',
            chunks: "initial"
        }
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: /node_modules/,
                options: {
                    // disable type checker for user in fork plugin
                    transpileOnly: true
                }
            }
        ]
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.js']
    },
    plugins: [
        // fork TS checker
        new ForkTsCheckerWebpackPlugin(),
        // exclude locale files in moment
        new CopyPlugin({
            patterns: [
                {
                    from: '.',
                    to: '../',
                    globOptions: {
                    ignore: ['manifest.json'],
                    },
                    context: './public',
                    filter: async (path) => {
                        if (path.match(/\/_locales\/.+/)) {
                            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.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) + "...";
                                }
                            }
            
                            return Buffer.from(JSON.stringify(parsed));
                        }

                        return content;
                    }
                }
            ]
        }),
        new BuildManifest({
            browser: env.browser,
            pretty: env.mode === "production",
            stream: env.stream
        })
    ]
});