-
Notifications
You must be signed in to change notification settings - Fork 538
Expand file tree
/
Copy pathesbuild.config.mjs
More file actions
146 lines (129 loc) · 4.1 KB
/
esbuild.config.mjs
File metadata and controls
146 lines (129 loc) · 4.1 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import esbuild from 'esbuild';
import path from 'path';
import process from 'process';
import builtins from 'builtin-modules';
import {
copyFileSync,
existsSync,
mkdirSync,
promises as fsPromises,
readFileSync,
rmSync,
} from 'fs';
import rendererSafeUnrefHelpers from './scripts/rendererSafeUnref.js';
const {
findUnsafeTimerUnrefSites,
patchRendererUnsafeUnrefSites,
} = rendererSafeUnrefHelpers;
// Load .env.local if it exists
if (existsSync('.env.local')) {
const envContent = readFileSync('.env.local', 'utf-8');
for (const line of envContent.split('\n')) {
const match = line.match(/^([^=]+)=["']?(.+?)["']?$/);
if (match && !process.env[match[1]]) {
process.env[match[1]] = match[2];
}
}
}
const prod = process.argv[2] === 'production';
const patchCodexSdkImportMeta = {
name: 'patch-codex-sdk-import-meta',
setup(build) {
build.onLoad(
{ filter: /[\\/]node_modules[\\/]@openai[\\/]codex-sdk[\\/]dist[\\/]index\.js$/ },
async (args) => {
const contents = await fsPromises.readFile(args.path, 'utf8');
return {
contents: contents.replace('createRequire(import.meta.url)', 'createRequire(__filename)'),
loader: 'js',
};
},
);
},
};
const patchRendererUnsafeUnref = {
name: 'patch-renderer-unsafe-unref',
setup(build) {
build.onEnd(async (result) => {
if (result.errors.length > 0 || !existsSync('main.js')) return;
const bundlePath = path.join(process.cwd(), 'main.js');
const originalContents = await fsPromises.readFile(bundlePath, 'utf8');
const patchedBundle = patchRendererUnsafeUnrefSites(originalContents);
if (patchedBundle.contents !== originalContents) {
await fsPromises.writeFile(bundlePath, patchedBundle.contents, 'utf8');
}
const unsafeMatches = findUnsafeTimerUnrefSites(patchedBundle.contents);
if (unsafeMatches.length > 0) {
const details = unsafeMatches
.slice(0, 5)
.map((match) => `line ${match.line}: ${match.snippet}`)
.join('\n');
throw new Error(
`Renderer-unsafe timer .unref() calls remain in main.js:\n${details}`,
);
}
});
},
};
// Obsidian plugin folder path (set via OBSIDIAN_VAULT env var or .env.local)
const OBSIDIAN_VAULT = process.env.OBSIDIAN_VAULT;
const OBSIDIAN_PLUGIN_PATH = OBSIDIAN_VAULT && existsSync(OBSIDIAN_VAULT)
? path.join(OBSIDIAN_VAULT, '.obsidian', 'plugins', 'claudian')
: null;
// Plugin to copy built files to Obsidian plugin folder
const copyToObsidian = {
name: 'copy-to-obsidian',
setup(build) {
build.onEnd((result) => {
if (result.errors.length > 0) return;
rmSync(path.join(process.cwd(), '.codex-vendor'), { recursive: true, force: true });
if (!OBSIDIAN_PLUGIN_PATH) return;
if (!existsSync(OBSIDIAN_PLUGIN_PATH)) {
mkdirSync(OBSIDIAN_PLUGIN_PATH, { recursive: true });
}
const files = ['main.js', 'manifest.json', 'styles.css'];
for (const file of files) {
if (existsSync(file)) {
copyFileSync(file, path.join(OBSIDIAN_PLUGIN_PATH, file));
console.log(`Copied ${file} to Obsidian plugin folder`);
}
}
const pluginVendorRoot = path.join(OBSIDIAN_PLUGIN_PATH, '.codex-vendor');
rmSync(pluginVendorRoot, { recursive: true, force: true });
});
}
};
const context = await esbuild.context({
entryPoints: ['src/main.ts'],
bundle: true,
plugins: [patchCodexSdkImportMeta, patchRendererUnsafeUnref, copyToObsidian],
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,
...builtins.map(m => `node:${m}`),
],
format: 'cjs',
target: 'es2018',
logLevel: 'info',
sourcemap: prod ? false : 'inline',
treeShaking: true,
outfile: 'main.js',
});
if (prod) {
await context.rebuild();
process.exit(0);
} else {
await context.watch();
}