-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
build.js
121 lines (94 loc) · 3.31 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import fs from "fs";
import path from "path";
import sass from "sass";
import thena from "thena/node";
const __dirname = thena.global.__dirname(import.meta);
const IN_DIR = `${__dirname}/src`;
const OUT_DIR = `${__dirname}/dist`;
const options = {
input: `${IN_DIR}/index.scss`,
output: `${OUT_DIR}/primer.css`,
};
/**
* @type {sass.Options}
*/
const sassOptions = {
sourceMap: true,
importers: [
{
canonicalize(url) {
const npmPackageNameRegex = /^@?\w+\//i;
if (npmPackageNameRegex.test(url)) {
thena.log(`Importing ${url}...`, "cyan", "bold");
return new URL(url, "file:///");
}
return null;
},
load(canonicalUrl) {
const parsed = String(canonicalUrl).replace("file:///", "");
if (parsed.startsWith("@")) {
const [packageName, ...rest] = parsed.split("/");
const packagePath = path.join(__dirname, "node_modules", packageName);
if (!fs.existsSync(packagePath)) {
return null;
}
const filePath = path.join(packagePath, ...rest);
if (!fs.existsSync(filePath)) {
return null;
}
return {
contents: fs.readFileSync(filePath, "utf-8"),
syntax: "scss",
};
} else {
const filePath = path.join(__dirname, "node_modules", parsed);
if (!fs.existsSync(filePath)) {
return null;
}
return {
contents: fs.readFileSync(filePath, "utf-8"),
syntax: "scss",
};
}
},
},
],
};
const bundle = async () => {
thena.log("Bundling...", "magenta", "bold");
try {
const result = sass.compile(options.input, sassOptions);
if (!fs.existsSync(OUT_DIR)) {
fs.mkdirSync(OUT_DIR);
}
await fs.promises.writeFile(options.output, result.css);
thena.log("Wrote to:", "cyan", "bold");
console.log(options.output);
if (process.argv.includes("--watch")) {
await fs.promises.writeFile(
`${options.output}.dev`,
`/* Generated local development file */\n\n@import url("https://mwittrien.github.io/BetterDiscordAddons/Themes/EmojiReplace/base/Apple.css");\n\n${result.css}`
);
thena.log("Wrote to:", "cyan", "bold");
console.log(`${options.output}.dev`);
}
} catch (error) {
thena.log("Error:", "red", "bold");
if (error instanceof sass.Exception) {
console.error(error.message);
} else {
console.error(error);
}
}
thena.log("Done!", "green", "bold");
};
bundle();
if (process.argv.includes("--watch")) {
fs.watch(IN_DIR, { recursive: true }, (event, filename) => {
if (event === "change" && filename.endsWith(".scss")) {
console.log();
console.log(`File ${filename} changed, rebuilding...`);
bundle();
}
});
}