forked from callumalpass/tasknotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild.config.mjs
More file actions
102 lines (95 loc) · 2.55 KB
/
esbuild.config.mjs
File metadata and controls
102 lines (95 loc) · 2.55 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
import esbuild from "esbuild";
import process from "process";
import builtins from "builtin-modules";
import { readFileSync, existsSync, copyFileSync, mkdirSync } from "fs";
import { resolve, join } from "path";
import { homedir } from "os";
const banner =
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;
const prod = (process.argv[2] === "production");
// Read copy destinations from .copy-files.local (same logic as copy-files.mjs)
function getCopyDestinations() {
const localFile = resolve(process.cwd(), '.copy-files.local');
if (!existsSync(localFile)) return [];
const expandTilde = (p) => p.startsWith('~/') ? join(homedir(), p.slice(2)) : p;
return readFileSync(localFile, 'utf8')
.split('\n')
.map(p => p.trim())
.filter(p => p && !p.startsWith('#'))
.map(expandTilde);
}
// esbuild plugin: copy build outputs to vault after each rebuild
const copyToVaultPlugin = {
name: 'copy-to-vault',
setup(build) {
const destinations = getCopyDestinations();
if (destinations.length === 0) return;
const filesToCopy = ['main.js', 'styles.css', 'manifest.json'];
build.onEnd((result) => {
if (result.errors.length > 0) return;
for (const dest of destinations) {
mkdirSync(dest, { recursive: true });
for (const file of filesToCopy) {
const src = resolve(process.cwd(), file);
if (existsSync(src)) {
copyFileSync(src, join(dest, file));
}
}
console.log(`\x1b[32m✓ Copied to ${dest}\x1b[0m`);
}
});
}
};
// Plugin to import markdown files as strings
const markdownPlugin = {
name: 'markdown',
setup(build) {
build.onLoad({ filter: /\.md$/ }, async (args) => {
const text = readFileSync(args.path, 'utf8');
return {
contents: `export default ${JSON.stringify(text)}`,
loader: 'js',
};
});
}
};
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["main.ts"],
bundle: true,
external: [
"obsidian",
"electron",
"@codemirror/autocomplete",
"@codemirror/collab",
"@codemirror/commands",
"@codemirror/language",
"@codemirror/lint",
"@codemirror/search",
"@codemirror/state",
"@codemirror/view",
"@lezer/common",
"@lezer/highlight",
"@lezer/lr",
...builtins],
format: "cjs",
target: "es2018",
logLevel: "info",
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: "main.js",
minify: prod,
plugins: [markdownPlugin, ...(!prod ? [copyToVaultPlugin] : [])],
});
if (prod) {
await context.rebuild();
process.exit(0);
} else {
await context.watch();
}