This repository was archived by the owner on May 1, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathbuild.mjs
More file actions
413 lines (386 loc) · 13.4 KB
/
build.mjs
File metadata and controls
413 lines (386 loc) · 13.4 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { copyFileSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { env, exit } from 'node:process'
import { fileURLToPath } from 'node:url'
// Shared plugin definitions - Bun's plugin API is compatible with esbuild's
const stubSemver = {
name: 'stub-semver',
setup(build) {
build.onResolve({ filter: /^semver$/ }, args => ({
path: args.path,
namespace: 'stub-semver',
}))
build.onLoad({ filter: /.*/, namespace: 'stub-semver' }, () => ({
contents: `
// Stub for semver package - @capacitor/cli requires it but checkPlatformVersions is never called
export const diff = () => null;
export const parse = () => null;
export const valid = () => null;
export const clean = () => null;
export const inc = () => null;
export const major = () => null;
export const minor = () => null;
export const patch = () => null;
export const compare = () => 0;
export const rcompare = () => 0;
export const gt = () => false;
export const lt = () => false;
export const eq = () => false;
export const neq = () => true;
export const gte = () => false;
export const lte = () => false;
export const satisfies = () => false;
export const maxSatisfying = () => null;
export const minSatisfying = () => null;
export const validRange = () => null;
export const outside = () => false;
export const gtr = () => false;
export const ltr = () => false;
export const intersects = () => false;
export const coerce = () => null;
export const Range = class Range {};
export const SemVer = class SemVer {};
export const Comparator = class Comparator {};
`,
}))
},
}
const ignorePunycode = {
name: 'ignore-punycode',
setup(build) {
build.onResolve({ filter: /^punycode$/ }, args => ({
path: args.path,
namespace: 'ignore',
}))
build.onLoad({ filter: /.*/, namespace: 'ignore' }, () => ({
contents: 'export default {}',
}))
},
}
const noopXml2js = {
name: 'noop-xml2js',
setup(build) {
build.onResolve({ filter: /^xml2js$/ }, args => ({
path: args.path,
namespace: 'noop',
}))
build.onLoad({ filter: /.*/, namespace: 'noop' }, () => ({
contents: 'export default {}',
}))
},
}
const noopIonicUtilsSubprocess = {
name: 'noop-ionic-utils-subprocess',
setup(build) {
build.onResolve({ filter: /@ionic\/utils-subprocess/ }, args => ({
path: args.path,
namespace: 'noop',
}))
build.onLoad({ filter: /.*/, namespace: 'noop' }, () => ({
contents: 'export default {}',
}))
},
}
const smartNoopIonicCliFrameworkOutput = {
name: 'smart-noop-ionic-cli-framework-output',
setup(build) {
build.onResolve({ filter: /@ionic\/cli-framework-output/ }, args => ({
path: args.path,
namespace: 'smart-noop-ionic-cli-framework-output',
}))
build.onLoad({ filter: /.*/, namespace: 'smart-noop-ionic-cli-framework-output' }, () => ({
contents: `
export const TTY_WIDTH = 80;
export const indent = (str) => str;
export const sliceAnsi = (str) => str;
export const stringWidth = (str) => str.length;
export const stripAnsi = (str) => str;
export const wordWrap = (str) => str;
export const createDefaultLogger = () => ({
info: console.log,
warn: console.warn,
error: console.error,
debug: console.debug,
});
export const NO_COLORS = {};
export class StreamOutputStrategy {
constructor() {
this.colors = NO_COLORS;
this.stream = process.stdout;
}
}
export class TTYOutputStrategy extends StreamOutputStrategy {
constructor(options) {
super();
this.options = options;
}
}
export class Logger {
constructor() {}
info() {}
warn() {}
error() {}
debug() {}
}
export const LOGGER_LEVELS = {
DEBUG: 'DEBUG',
INFO: 'INFO',
WARN: 'WARN',
ERROR: 'ERROR'
};
`,
}))
},
}
const noopSupabaseRealtimeJs = {
name: 'noop-supabase-realtime-js',
setup(build) {
build.onResolve({ filter: /@supabase\/realtime-js/ }, args => ({
path: args.path,
namespace: 'noop-supabase-realtime-js',
}))
build.onLoad({ filter: /.*/, namespace: 'noop-supabase-realtime-js' }, () => ({
contents: `
export class RealtimeClient {
constructor() {}
connect() {}
disconnect() {}
}
`,
}))
},
}
const stubPrompts = {
name: 'stub-prompts',
setup(build) {
build.onResolve({ filter: /^prompts$/ }, args => ({
path: args.path,
namespace: 'stub-prompts',
}))
build.onLoad({ filter: /.*/, namespace: 'stub-prompts' }, () => ({
contents: `
// Stub for prompts package - @capacitor/cli requires it but we don't use it
export default function prompts() {
throw new Error('Prompts are not supported in this CLI build');
}
`,
}))
},
}
const noopSupabaseAuthJs = {
name: 'noop-supabase-auth-js',
setup(build) {
build.onResolve({ filter: /@supabase\/auth-js/ }, args => ({
path: args.path,
namespace: 'noop-supabase-auth-js',
}))
build.onLoad({ filter: /.*/, namespace: 'noop-supabase-auth-js' }, () => ({
contents: `
// Stub for @supabase/auth-js - we don't use authentication, just API calls
const noopAsync = () => Promise.resolve({ data: { session: null, user: null }, error: null });
const noopHandler = {
get: (target, prop) => {
if (prop === 'constructor') return target.constructor;
if (prop === 'then' || prop === 'catch' || prop === 'finally') return undefined;
if (typeof prop === 'symbol') return undefined;
// Return method that returns properly structured promises
if (prop === 'getSession') return () => Promise.resolve({ data: { session: null, user: null }, error: null });
if (prop === 'onAuthStateChange') return () => ({ data: { subscription: { unsubscribe: () => {} } }, error: null });
return noopAsync;
}
};
export class GoTrueClient {
constructor(options) {
this.options = options;
return new Proxy(this, noopHandler);
}
}
export class GoTrueAdminApi {
constructor(options) {
this.options = options;
return new Proxy(this, noopHandler);
}
}
export class AuthClient extends GoTrueClient {}
export class AuthAdminApi extends GoTrueAdminApi {}
// Export error classes
export class AuthError extends Error {}
export class AuthApiError extends AuthError {}
export class AuthRetryableError extends AuthError {}
export class AuthSessionMissingError extends AuthError {}
export class AuthInvalidTokenResponseError extends AuthError {}
export class AuthInvalidCredentialsError extends AuthError {}
export class AuthImplicitGrantRedirectError extends AuthError {}
export class AuthPKCEGrantCodeExchangeError extends AuthError {}
export class AuthWeakPasswordError extends AuthError {}
// Export helper functions
export const navigatorLock = noopAsync;
export const processLock = noopAsync;
export class NavigatorLockAcquireTimeoutError extends Error {}
export const lockInternals = {};
// Export type helpers
export const isAuthError = () => false;
export const isAuthApiError = () => false;
export const isAuthRetryableError = () => false;
export const isAuthSessionMissingError = () => false;
export const isAuthWeakPasswordError = () => false;
`,
}))
},
}
const noopSupabaseNodeFetch = {
name: 'noop-supabase-node-fetch',
setup(build) {
build.onResolve({ filter: /@supabase\/node-fetch/ }, args => ({
path: args.path,
namespace: 'noop',
}))
build.onLoad({ filter: /.*/, namespace: 'noop' }, () => ({
contents: 'export default {}',
}))
},
}
// Stub react-devtools-core — Ink optionally imports it for dev mode
const stubReactDevtools = {
name: 'stub-react-devtools',
setup(build) {
build.onResolve({ filter: /^react-devtools-core$/ }, args => ({
path: args.path,
namespace: 'stub-react-devtools',
}))
build.onLoad({ filter: /.*/, namespace: 'stub-react-devtools' }, () => ({
contents: 'export default {}; export const connectToDevTools = () => {};',
}))
},
}
// Fix for @capacitor/cli path assumptions in bundled builds
// - __dirname gets baked in as the build machine path
// - loadCLIConfig reads package.json from cliRootDir
// We replace __dirname with import.meta.url and make package.json read resilient
// See: https://github.com/oven-sh/bun/issues/4216
const fixCapacitorCliDirname = {
name: 'fix-capacitor-cli-dirname',
setup(build) {
// Allow matching when @capacitor/cli is hoisted, linked, or vendored.
build.onLoad({ filter: /@capacitor[\\/]cli[\\/]dist[\\/]config\.js$/ }, async (args) => {
const contents = readFileSync(args.path, 'utf-8')
// Replace any __dirname usage (CJS) with runtime-safe import.meta.url resolution.
// Keep this broad so it survives upstream refactors.
let patched = contents.replace(
/\b__dirname\b/g,
"require('path').dirname(require('url').fileURLToPath(import.meta.url))"
)
// Make CLI package.json read resilient in bundled runtime.
// Capture module alias names to avoid breaking if upstream renames them.
patched = patched.replace(
/package:\s*await\s*\(0,\s*([\w$]+)\.readJSON\)\(\(0,\s*([\w$]+)\.resolve\)\(rootDir,\s*'package\.json'\)\)\s*,/g,
(_match, fsAlias, pathAlias) =>
`package: await (0, ${fsAlias}.readJSON)((0, ${pathAlias}.resolve)(rootDir, 'package.json')).catch(() => ({ name: '@capacitor/cli', version: '0.0.0' })),`
)
return { contents: patched, loader: 'js' }
})
},
}
// Build CLI
const buildCLI = Bun.build({
entrypoints: ['src/index.ts'],
target: 'node',
outdir: 'dist',
naming: 'index.js',
sourcemap: env.NODE_ENV === 'development' ? 'linked' : 'none',
minify: true,
// Keep env access runtime-only unless explicitly defined below.
env: 'disable',
define: {
'process.env.SUPA_DB': '"production"',
},
plugins: [
fixCapacitorCliDirname,
stubSemver,
ignorePunycode,
noopXml2js,
noopIonicUtilsSubprocess,
smartNoopIonicCliFrameworkOutput,
noopSupabaseRealtimeJs,
stubPrompts,
noopSupabaseAuthJs,
stubReactDevtools,
],
})
// Build SDK (separate bundle without CLI dependencies)
const buildSDK = Bun.build({
entrypoints: ['src/sdk.ts'],
target: 'node',
outdir: 'dist/src',
naming: 'sdk.js',
sourcemap: env.NODE_ENV === 'development' ? 'linked' : 'none',
minify: true,
format: 'esm',
// Keep env access runtime-only unless explicitly defined below.
env: 'disable',
define: {
'process.env.SUPA_DB': '"production"',
},
plugins: [
fixCapacitorCliDirname,
ignorePunycode,
noopSupabaseNodeFetch,
],
})
Promise.all([buildCLI, buildSDK]).then(async (results) => {
const [cliResult, sdkResult] = results
// Check for build errors
if (!cliResult.success) {
console.error('CLI build failed:')
for (const log of cliResult.logs) {
console.error(log)
}
exit(1)
}
if (!sdkResult.success) {
console.error('SDK build failed:')
for (const log of sdkResult.logs) {
console.error(log)
}
exit(1)
}
// Add shebang to CLI bundle
const cliOutput = await Bun.file('dist/index.js').text()
await Bun.write('dist/index.js', `#!/usr/bin/env node\n${cliOutput}`)
// Bun has occasionally emitted `module.exports` in ESM bundles.
// Ensure the SDK bundle doesn't crash in ESM by providing a shim when needed.
const sdkPath = 'dist/src/sdk.js'
try {
let sdkOutput = readFileSync(sdkPath, 'utf-8')
const hasModuleBinding = /\b(?:var|let|const)\s+module(?![$\w])/.test(sdkOutput)
if (/\bmodule\.exports\b/.test(sdkOutput) && !hasModuleBinding) {
const importBlock = sdkOutput.match(/^(?:\s*import[^;]+;)+/)
const insertAt = importBlock ? importBlock[0].length : 0
sdkOutput = `${sdkOutput.slice(0, insertAt)}var module={exports:{}};${sdkOutput.slice(insertAt)}`
writeFileSync(sdkPath, sdkOutput)
}
}
catch (err) {
console.warn('⚠️ Could not inspect SDK bundle for module shim:', err)
}
// Write metafile for bundle analysis (similar to esbuild's metafile)
// Use relative paths to match esbuild's format
const metafile = {
inputs: {},
outputs: {},
}
for (const output of cliResult.outputs) {
const relativePath = output.path.replace(process.cwd() + '/', '')
metafile.outputs[relativePath] = { bytes: output.size }
}
for (const output of sdkResult.outputs) {
const relativePath = output.path.replace(process.cwd() + '/', '')
metafile.outputs[relativePath] = { bytes: output.size }
}
writeFileSync('meta.json', JSON.stringify(metafile))
copyFileSync('package.json', 'dist/package.json')
console.warn('✅ Built CLI and SDK successfully')
}).catch((err) => {
console.error('Build failed:', err)
exit(1)
})