forked from ilyhalight/voice-over-translation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
151 lines (131 loc) · 3.79 KB
/
webpack.config.js
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
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
import webpack from "webpack";
import { monkey } from "webpack-monkey";
import { styleLoaderInsertStyleElement } from "webpack-monkey/lib/client/css.js";
import ESLintPlugin from "eslint-webpack-plugin";
import TerserPlugin from "terser-webpack-plugin";
const repo =
"https://raw.githubusercontent.com/ilyhalight/voice-over-translation";
const dev = process.env.NODE_ENV === "development";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let isBeta = getHeaders().version.includes("beta");
console.log("development mode: ", dev);
function getHeaders(lang) {
const headersPath = path.resolve(
__dirname,
"src",
lang ? `locales/${lang}` : "",
"headers.json",
);
return JSON.parse(fs.readFileSync(headersPath).toString());
}
export default (env) => {
const build_mode = env.build_mode;
const build_type = env.build_type;
console.log("build mode: ", build_mode);
console.log("build type: ", build_type);
function get_filename() {
let name = "vot";
if (build_mode === "cloudflare") {
name += "-cloudflare";
}
if (build_type === "minify") {
name += "-min";
}
return name + ".user.js";
}
function get_name_by_build_mode(name) {
let finalName =
build_mode === "cloudflare"
? name.replace("[VOT]", "[VOT Cloudflare]")
: name;
return finalName;
}
return monkey({
mode: dev ? "development" : "production",
resolve: {
extensions: [".js"],
},
performance: {
hints: "error",
maxEntrypointSize: 2000 * 10 ** 3,
maxAssetSize: 2000 * 10 ** 3,
},
entry: path.resolve(__dirname, "src", "index.js"),
output: {
path: path.resolve(__dirname, "dist"),
...(!dev ? { filename: get_filename() } : {}),
},
monkey: {
debug: dev,
meta: {
resolve: "headers.json",
transform({ meta }) {
const extFileName = get_filename().slice(0, -8);
const finalURL = `${repo}/${
isBeta ? "dev" : "master"
}/dist/${extFileName}.user.js`;
meta.namespace = extFileName;
meta.updateURL = meta.downloadURL = finalURL;
if (build_mode === "cloudflare") {
meta.name = meta.name.replace("[VOT]", "[VOT Cloudflare]");
meta["inject-into"] = "page";
}
const files = fs.readdirSync(
path.resolve(__dirname, "src", "locales"),
);
meta.name = {
default: meta.name,
};
meta.description = {
default: meta.description,
};
for (const file of files) {
const localeHeaders = getHeaders(file);
const lang = file.substring(0, 2);
meta.name[lang] = get_name_by_build_mode(localeHeaders.name);
meta.description[lang] = localeHeaders.description;
}
return meta;
},
},
},
plugins: [
new ESLintPlugin(),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
}),
new webpack.DefinePlugin({
BUILD_MODE: JSON.stringify(build_mode),
DEBUG_MODE: dev,
IS_BETA_VERSION: isBeta,
...(() => {
if (!dev) {
return {
__MK_GLOBAL__: {
styleLoaderInsertStyleElement,
},
};
}
})(),
}),
],
module: {
rules: [
{
test: /\.(css|scss|sass)$/i,
use: ["style-loader", "css-loader", "sass-loader"],
},
],
},
optimization: {
emitOnErrors: true,
moduleIds: "named",
minimize: build_type === "minify" ? true : false,
minimizer: [new TerserPlugin()],
},
});
};