-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathwebpack.chrome.js
141 lines (127 loc) · 3.86 KB
/
webpack.chrome.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
// webpack.chrome.js
const path = require("path");
const fs = require("fs");
const rimraf = require("rimraf");
const CopyPlugin = require("copy-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const { merge } = require("webpack-merge");
const common = require("./webpack.common.js");
const archiver = require("archiver");
const isBuildMode = process.env.BUILD_MODE === "build";
// خواندن manifest مربوط به کروم
const manifestFilePath = "./src/manifest.chrome.json";
const manifestRaw = fs.readFileSync(manifestFilePath, "utf8");
const manifest = JSON.parse(manifestRaw);
const defaultLocale = "en";
const messagesPath = path.resolve(
__dirname,
"_locales",
defaultLocale,
"messages.json"
);
let messages = {};
try {
messages = JSON.parse(fs.readFileSync(messagesPath, "utf8"));
} catch (error) {
console.error("Error reading messages file:", error);
}
// جایگزینی placeholderهای i18n در name
if (typeof manifest.name === "string") {
manifest.name = manifest.name.replace(/__MSG_(\w+)__/g, (match, key) => {
return messages[key] ? messages[key].message : match;
});
}
const extensionName = manifest.name.replace(/ /g, "-");
const extensionVersion = manifest.version;
const outputFolderPath = path.resolve(
__dirname,
"Build-Extension",
extensionName
);
const zipFilePath = path.resolve(
__dirname,
"Build-Extension",
`${extensionName}-v${extensionVersion}.zip`
);
rimraf.sync(outputFolderPath);
if (fs.existsSync(zipFilePath)) fs.unlinkSync(zipFilePath);
// کلاس ساخت فایل zip بعد از build
class ZipAfterBuildPlugin {
apply(compiler) {
compiler.hooks.afterEmit.tapAsync(
"ZipAfterBuildPlugin",
(compilation, callback) => {
const output = fs.createWriteStream(zipFilePath);
const archive = archiver("zip", { zlib: { level: 9 } });
output.on("close", () => {
console.log(
`✅ ZIP created at ${zipFilePath} (${archive.pointer()} bytes)`
);
callback();
});
archive.on("error", (err) => callback(err));
archive.pipe(output);
archive.directory(outputFolderPath, false);
archive.finalize();
}
);
}
}
const chromeDistConfig = {
mode: "production",
output: {
path: outputFolderPath,
filename: "[name].bundle.js",
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
compress: { drop_console: true },
},
}),
],
},
plugins: [
new CopyPlugin({
patterns: [
{
from: "src/manifest.chrome.json",
to: "manifest.json",
transform(content) {
let manifest = JSON.parse(content);
if (typeof manifest.name === "string") {
manifest.name = manifest.name.replace(
/__MSG_(\w+)__/g,
(match, key) => {
return messages[key] ? messages[key].message : match;
}
);
}
return Buffer.from(JSON.stringify(manifest, null, 2));
},
},
{ from: "html/*.html", to: "html/[name][ext]" },
{ from: "styles/*.css", to: "styles/[name][ext]" },
{
from: "icons/*.png",
to: ({ absoluteFilename }) => {
const iconName = path.basename(absoluteFilename);
return `icons/${iconName}`;
},
},
{ from: "html/offscreen.html", to: "offscreen.html" },
{ from: "src/offscreen.js", to: "offscreen.js" },
{ from: "node_modules/webextension-polyfill/dist/browser-polyfill.js" },
{ from: "_locales", to: "_locales" },
{
from: "src/pageBridge.js",
to: "pageBridge.js",
},
],
}),
...(isBuildMode ? [new ZipAfterBuildPlugin()] : []),
],
};
module.exports = merge(common, chromeDistConfig);