-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
105 lines (86 loc) · 2.77 KB
/
build.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
const esbuild = require("esbuild");
const path = require("path");
const fs = require("fs");
const os = require("os");
const processCss = require("./utils/processCss");
// Get current working directory
const cwd = process.cwd();
// Define paths relative to current directory
const srcPath = path.join(cwd, "Essentials.nkplugin.jsx");
const outPath = path.join(cwd, "build", "Essentials.nkplugin.js");
// Ensure build directory exists
const buildDir = path.join(cwd, "build");
if (!fs.existsSync(buildDir)) {
fs.mkdirSync(buildDir, { recursive: true });
}
// Check for flags
const shouldCopy = process.argv.includes("-c") && process.platform === "darwin";
const shouldBundleCss = process.argv.includes("-b");
const pluginsPath = shouldCopy
? path.join(
os.homedir(),
"Library",
"Application Support",
"nekocord",
"plugins",
"Essentials.nkplugin.js"
)
: null;
async function build() {
console.log("Building plugin...");
const buildConfig = {
entryPoints: [srcPath],
bundle: true,
outfile: outPath,
format: "cjs",
platform: "node",
target: "es2020",
external: ["react"],
};
// Only add CSS processing plugin if -b flag is present
if (shouldBundleCss) {
console.log("Processing CSS imports...");
buildConfig.plugins = [{
name: 'css-import-processor',
setup(build) {
build.onLoad({ filter: /\.(js|jsx)$/ }, async (args) => {
let contents = await fs.promises.readFile(args.path, 'utf8');
const cssRegex = /css:\s*`([^`]+)`/g;
let match;
let lastIndex = 0;
let result = '';
while ((match = cssRegex.exec(contents)) !== null) {
result += contents.slice(lastIndex, match.index);
const css = match[1];
if (css.includes('@import')) {
const processedCss = await processCss(css);
const escapedCss = processedCss
.replace(/\\/g, '\\\\')
.replace(/`/g, '\\`')
.replace(/\$/g, '\\$');
result += `css:\`${escapedCss}\``;
} else {
result += match[0];
}
lastIndex = match.index + match[0].length;
}
result += contents.slice(lastIndex);
return {
contents: result,
loader: path.extname(args.path).slice(1)
};
});
}
}];
}
await esbuild.build(buildConfig);
if (shouldCopy && pluginsPath) {
fs.copyFileSync(outPath, pluginsPath);
console.log("Copied to nekocord plugins directory");
}
console.log("Build complete!");
}
build().catch((error) => {
console.error("Build failed:", error);
process.exit(1);
});