-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
202 lines (194 loc) · 5.72 KB
/
Copy pathwebpack.config.js
File metadata and controls
202 lines (194 loc) · 5.72 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const webpack = require("webpack");
const path = require("path");
const fs = require("fs");
const TerserPlugin = require("terser-webpack-plugin");
const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
const pkg = require("./package.json");
const pluginConfig = require("./config.json");
// Color codes
const ccGreen = "\u001b[1;32m";
const ccReset = "\u001b[0m";
// Plugin name and entry file
const pluginName = "Quantum";
const entryFile = "./src/quantum.jsx";
const meta = (() => {
const lines = ["/**"];
for (const key in pluginConfig) {
lines.push(` * @${key} ${pluginConfig[key]}`);
if (key === "description") {
if (pkg.version) lines.push(` * @version ${pkg.version}`);
if (pkg.author && pkg.author.name)
lines.push(` * @author ${pkg.author.name}`);
}
}
lines.push(" */");
return lines.join("\n");
})();
module.exports = (env) => ({
mode: "development",
target: "node",
devtool: false,
watch: env && env.watch,
watchOptions: {
ignored: /node_modules/,
},
entry: env.production
? {
[pluginName]: entryFile,
[pluginName + ".min"]: entryFile,
}
: { [pluginName]: entryFile },
output: {
filename: "[name].plugin.js",
path: path.join(__dirname, "build"),
libraryTarget: "commonjs2",
libraryExport: "default",
compareBeforeEmit: false,
},
resolve: {
extensions: [".js", ".jsx", ".css"],
alias: {
"@assets": path.resolve(__dirname, "assets/"),
"@components": path.resolve(__dirname, "src/components/"),
"@modules": path.resolve(__dirname, "src/modules/"),
"@utils": path.resolve(__dirname, "src/utils/"),
"@quantum": path.resolve(__dirname, "src/quantum"),
"@meta": path.resolve(__dirname, "src/utils/meta"),
"@i18n": path.resolve(__dirname, "src/i18n"),
},
},
optimization: {
minimize: env.production,
minimizer: [
new TerserPlugin({
include: /\.min\.plugin\.js$/,
extractComments: false,
terserOptions: {
output: {
comments: (node, comment) => {
const text = comment.value;
const type = comment.type;
if (type == "comment2") {
// multiline comment
return ["@name", "@description", "@version", "@author"].every(
(term) => new RegExp(term).test(text)
);
}
},
},
},
}),
new ImageMinimizerPlugin({
minimizer: {
implementation: ImageMinimizerPlugin.imageminMinify,
options: {
// Lossless optimization with custom option
// Feel free to experiment with options for better result for you
plugins: [
// Svgo configuration here https://github.com/svg/svgo#configuration
[
"svgo",
{
plugins: [
{
name: "preset-default",
params: {
overrides: {
removeViewBox: false,
removeXMLNS: true,
addAttributesToSVGElement: {
params: {
attributes: [
{ xmlns: "http://www.w3.org/2000/svg" },
],
},
},
},
},
},
],
},
],
],
},
},
}),
],
},
module: {
rules: [
{ test: /\.css$/, exclude: /node_modules/, use: "css-loader" },
{
test: /\.(s(a|c)ss)$/,
exclude: /node_modules/,
use: ["css-loader", "sass-loader"],
},
{ test: /\.jsx$/, exclude: /node_modules/, use: "babel-loader" },
{
test: /\.svg$/,
exclude: /node_modules/,
use: "raw-loader",
},
{
test: /\.(jpe?g|png|gif)$/i,
exclude: /node_modules/,
use: [
{
loader: "url-loader",
options: {
limit: Infinity,
},
},
],
},
],
},
plugins: [
new webpack.BannerPlugin({ raw: true, banner: meta }),
{
apply: (compiler) => {
compiler.hooks.assetEmitted.tap("copyPlugin2Dir", (filename, info) => {
// Only copy files that end with min.plugin.js
if (
env.production
? !filename.endsWith("min.plugin.js")
: filename.endsWith("min.plugin.js")
) {
return;
}
const userConfig = (() => {
if (process.platform === "win32") return process.env.APPDATA;
if (process.platform === "darwin")
return path.join(
process.env.HOME,
"Library",
"Application Support"
);
if (process.env.XDG_CONFIG_HOME) return process.env.XDG_CONFIG_HOME;
return path.join(process.env.HOME, ".config");
})();
const bdPluginFolder = path.join(
userConfig,
"BetterDiscord",
"plugins"
);
const filePath = path.join(bdPluginFolder, pluginName + ".plugin.js");
// Copy the plugin file to the plugin folder
fs.copyFileSync(info.targetPath, filePath);
console.log(
"\ncopied " +
ccGreen +
filename +
ccReset +
' to "' +
filePath +
'" ' +
ccGreen +
"successfully" +
ccReset
);
});
},
},
],
});