-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathesbuild.config.mjs
More file actions
64 lines (58 loc) · 1.93 KB
/
Copy pathesbuild.config.mjs
File metadata and controls
64 lines (58 loc) · 1.93 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
import console from "console";
import esbuild from "esbuild";
import fs from "fs";
import { builtinModules } from "node:module";
import path from "path";
import prettier from "prettier";
import process from "process";
const prod = process.argv[2] === "production";
// The published plugin expects CSS at the repository root, not under build/.
// We also normalize the generated stylesheet through Prettier so CI does not
// fail on formatting differences introduced by the bundler output.
const moveToRootPlugin = {
name: "move-to-root",
setup(build) {
build.onEnd(async (_) => {
const cssFile = path.join("build", "main.css");
const targetFile = "styles.css";
if (fs.existsSync(cssFile)) {
let contents = fs.readFileSync(cssFile, "utf8");
// Remove source map comment
contents = contents.replace(/\/\*#\s*sourceMappingURL=.*?\*\/\s*$/s, "");
contents = await prettier.format(contents, { filepath: targetFile });
fs.writeFileSync(targetFile, contents);
fs.rmSync(cssFile);
console.log(`✓ CSS bundled to ${targetFile}`);
}
});
},
};
const context = await esbuild.context({
entryPoints: ["src/main.ts"],
bundle: true,
external: ["obsidian", "electron", ...builtinModules],
format: "cjs",
target: "es2018",
logLevel: "info",
sourcemap: "inline",
sourcesContent: !prod,
treeShaking: true,
outfile: "build/main.js",
loader: {
".css": "css",
},
plugins: [moveToRootPlugin],
});
if (prod) {
try {
// Production mode must await rebuild/dispose so async post-processing of
// styles.css has finished before the process exits.
await context.rebuild();
} catch {
process.exit(1);
} finally {
await context.dispose();
}
} else {
context.watch().catch(() => process.exit(1));
}