-
Notifications
You must be signed in to change notification settings - Fork 46
/
webpack.config.js
125 lines (107 loc) · 3.43 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
const path = require("path");
const webpack = require("webpack");
const CopyPlugin = require("copy-webpack-plugin");
module.exports = function generatePluginConfig(pluginPath) {
const manifest = readManifest(pluginPath);
const bdHeader = buildBdHeader(manifest);
const bdPluginsPath = getBdPluginsPath();
return (env, argv) => {
const isProduction = argv.mode === "production";
return {
target: "node",
entry: pluginPath,
output: {
clean: isProduction,
filename: `${manifest.name}.plugin.js`,
path: isProduction ? path.join(pluginPath, "dist") : bdPluginsPath,
library: {
type: "commonjs2",
export: "default"
}
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: "@sucrase/webpack-loader",
options: {
production: isProduction,
disableESTransforms: true,
transforms: ["jsx"]
}
}
},
{
test: /\.css$/,
use: 'raw-loader'
},
{
test: /\.scss$/,
use: ['raw-loader', 'sass-loader']
}
]
},
resolve: {
extensions: ['.js', '.jsx']
},
optimization: {
minimize: false
},
plugins: [
new webpack.BannerPlugin({ banner: bdHeader.toString(), raw: true }),
isProduction && new CopyPlugin({
patterns: [
path.join(pluginPath, "README.md")
]
})
].filter(Boolean),
externals: {
react: ["global BdApi", "React"]
}
}
};
};
function readManifest(pluginPath) {
return require(path.join(pluginPath, "package.json"));
}
function buildBdHeader(manifest) {
const header = new BdHeader();
header.set("name", manifest.name);
header.set("author", manifest.author);
header.set("authorLink", manifest.authorLink);
header.set("description", manifest.description);
header.set("version", manifest.version);
header.set("source", manifest.source);
return header;
}
function getBdPluginsPath() {
const dataPath =
process.env.APPDATA ||
process.env.XDG_CONFIG_HOME ||
(process.platform === "darwin"
? `${process.env.HOME}/Library/Application Support`
: `${process.env.HOME}/.config`);
return path.join(dataPath, "BetterDiscord", "plugins");
}
class BdHeader {
constructor() {
this.properties = new Map();
}
set(name, value) {
if (!value) return;
this.properties.set(name, value);
}
remove(name) {
this.properties.delete(name);
}
toString() {
let result = "/**\n";
for (const [name, value] of this.properties) {
result += ` * @${name} ${value}\n`;
}
result += " */";
return result;
}
}