-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathesbuild.config.mjs
97 lines (91 loc) · 2.91 KB
/
esbuild.config.mjs
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
import esbuild from "esbuild";
import process from "process";
import dotenv from "dotenv";
dotenv.config();
import fs from "fs";
import path from "path";
// Compile EJS templates into JSON
const views = {};
fs.readdir("./", (err, files) => {
if (err) {
console.error('Error reading the directory', err);
return;
}
files.forEach(file => {
if(!['.ejs', '.md'].includes(path.extname(file))) return;
const file_path = path.join("./", file);
const content = fs.readFileSync(file_path, 'utf8').replace(/\r\n/g, '\n');
views[path.basename(file, path.extname(file))] = content;
});
// console.log('views', views);
// add dist folder if not exists
if (!fs.existsSync('dist')) fs.mkdirSync('dist');
fs.writeFileSync('dist/views.json', JSON.stringify(views, null, 2));
console.log('EJS templates compiled into templates.json');
});
// Compile markdown files in templates folder into JSON
const templates = {};
const templates_folder = './templates';
fs.readdir(templates_folder, (err, files) => {
if (err) {
console.error('Error reading the templates directory', err);
return;
}
files.forEach(file => {
if (path.extname(file) !== '.md') return;
const file_path = path.join(templates_folder, file);
const content = fs.readFileSync(file_path, 'utf8').replace(/\r\n/g, '\n');
templates[path.basename(file, '.md')] = content;
});
// add dist folder if not exists
if (!fs.existsSync('dist')) fs.mkdirSync('dist');
fs.writeFileSync('dist/templates.json', JSON.stringify(templates, null, 2));
console.log('Markdown templates compiled into templates.json');
});
const banner =
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit https://github.com/brianpetro/obsidian-smart-templates
*/
`;
// get id from manifest.json
const manifest = JSON.parse(fs.readFileSync("./manifest.json", "utf8"));
const plugin_id = manifest.id;
const copy_to_plugins = {
name: 'copy_to_plugins',
setup(build) {
build.onEnd(() => {
const plugin_path = path.join(process.env.OBSIDIAN_PLUGINS_PATH, plugin_id);
if (!fs.existsSync(plugin_path)) fs.mkdirSync(plugin_path);
fs.copyFileSync("./dist/main.js", path.join(plugin_path, "main.js"));
fs.copyFileSync("./manifest.json", path.join(plugin_path, "manifest.json"));
fs.copyFileSync("./styles.css", path.join(plugin_path, "styles.css"));
fs.writeFileSync(path.join(plugin_path, ".hotreload"), ""); // add empty .hotreload file
console.log("Plugin built and copied to obsidian plugins folder");
});
}
};
const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["main.js"],
platform: "node",
bundle: true,
write: true,
external: [
"obsidian",
"electron",
],
format: "cjs",
target: "es2022",
logLevel: "info",
sourcemap: "inline",
treeShaking: true,
outfile: "dist/main.js",
plugins: [
copy_to_plugins
]
});
await context.rebuild();
process.exit(0);