-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.config.mjs
124 lines (110 loc) · 3.81 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import * as esbuild from 'esbuild';
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
console.log(' 🔎 Scanning...');
// Trouver automatiquement tous les plugins
const plugins = fs.readdirSync(__dirname)
.filter(dir => dir.startsWith('obsidian-') || dir.startsWith('@obsidian-'))
.filter(dir => {
const mainTs = path.resolve(__dirname, dir, 'src/main.ts');
const mainJs = path.resolve(__dirname, dir, 'src/main.js');
try {
if (fs.existsSync(mainTs) && fs.statSync(mainTs).isFile()) {
console.log(` 📂 Found -> ${dir}`);
return true;
}
if (fs.existsSync(mainJs) && fs.statSync(mainJs).isFile()) {
console.log(` 📂 Found -> ${dir}`);
return true;
}
} catch (error) {
console.warn(` ⚠️ Skipping invalid plugin: ${dir}`);
}
return false;
});
console.log(' 📦 Building...');
// Configuration de base pour esbuild
const baseConfig = {
bundle: true,
platform: 'node',
format: 'cjs',
target: 'es2018',
sourcemap: 'inline',
logLevel: 'silent',
external: [
'obsidian',
'@codemirror/view',
'@codemirror/state',
'@codemirror/language'
],
loader: {
'.ts': 'ts'
},
treeShaking: true,
minify: false
};
let isInitialBuild = true;
// Créer un contexte pour chaque plugin
const contexts = await Promise.all(plugins.map(async plugin => {
const entryPoint = path.resolve(__dirname, plugin, 'src/main.ts');
const outfile = path.resolve(__dirname, plugin, 'main.js');
const ctx = await esbuild.context({
...baseConfig,
entryPoints: [entryPoint],
outfile,
plugins: [{
name: 'log-rebuild',
setup(build) {
let buildStart = Date.now();
build.onStart(() => {
buildStart = Date.now();
if (!isInitialBuild) {
const changedFile = build.initialOptions.stdin?.contents || '';
if (changedFile) {
console.log(`\n 📝 Changed file: ${changedFile}`);
}
console.log(` 🏗️ Building -> 🌱 ${plugin}...`);
}
});
build.onEnd(result => {
const date = new Date().toLocaleTimeString();
const buildTime = Date.now() - buildStart;
if (result.errors.length > 0) {
console.error(`[${date}] ❌ Failed -> ${plugin}:`);
result.errors.forEach(error => {
console.error(` 🚫 ${error.text}`);
});
} else {
if (isInitialBuild) {
console.log(`[${date}] 🎯 Success -> 🌺 ${plugin}`);
} else {
console.log(`[${date}] ⚡ Success -> ⏳ ${buildTime}ms 🌷 ${plugin}`);
console.log(` ✨ Watching...\n`);
}
}
});
}
}]
});
return { plugin, ctx };
}));
// Mode watch
if (process.argv.includes('--watch')) {
// Faire le build initial de tous les plugins
console.log('\n 🚀 Starting build...');
await Promise.all(contexts.map(({ ctx }) => ctx.rebuild()));
console.log(' ✅ Success!\n');
isInitialBuild = false;
console.log(' 👀 Watching...\n');
await Promise.all(contexts.map(({ ctx }) => ctx.watch()));
}
// Build unique
else {
await Promise.all(contexts.map(async ({ ctx }) => {
await ctx.rebuild();
await ctx.dispose();
}));
console.log(' ✅ Success!');
}