Repository navigation
Expand file tree
/
Copy pathbuild.mjs
More file actions
117 lines (108 loc) · 3.4 KB
/
Copy pathbuild.mjs
File metadata and controls
117 lines (108 loc) · 3.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
import { chmod, readFile, rename, stat, unlink, writeFile } from 'fs/promises';
import esbuild from 'esbuild';
import { sentryEsbuildPlugin } from '@sentry/esbuild-plugin';
// jsonc-parser ships a UMD entry (its `main`) whose body does runtime
// `require("./impl/format")` etc. esbuild can't follow those dynamic sibling
// requires when bundling, so the built `dist/craft` fails at startup with
// "Cannot find module './impl/format'". Redirect the package to its ESM entry
// (`module`), which uses static imports esbuild can bundle. Pulled in
// transitively via @vercel/client → @vercel/microfrontends.
const jsoncParserEsmPlugin = {
name: 'jsonc-parser-esm',
setup(build) {
build.onResolve({ filter: /^jsonc-parser$/ }, async args => {
// Avoid recursing into our own resolve call below.
if (args.pluginData?.resolved) {
return;
}
const result = await build.resolve('jsonc-parser', {
importer: args.importer,
kind: args.kind,
resolveDir: args.resolveDir,
pluginData: { resolved: true },
});
if (result.errors.length > 0) {
return result;
}
return {
path: result.path.replace(/([\\/])lib[\\/]umd[\\/]/, '$1lib/esm/'),
};
});
},
};
const plugins = [jsoncParserEsmPlugin];
// Only add Sentry plugin if auth token is available (production builds on master)
if (process.env.SENTRY_AUTH_TOKEN) {
plugins.push(
sentryEsbuildPlugin({
org: 'sentry',
project: 'craft',
authToken: process.env.SENTRY_AUTH_TOKEN,
sourcemaps: {
assets: ['dist/craft.js', 'dist/craft.js.map'],
filesToDeleteAfterUpload: ['dist/**/*.map'],
},
}),
);
} else {
console.log(
'[build] SENTRY_AUTH_TOKEN not found, skipping source map upload',
);
}
// Build to .js file first so Sentry plugin can properly handle source maps
await esbuild.build({
entryPoints: ['src/index.ts'],
sourcemap: true,
bundle: true,
platform: 'node',
target: 'node22',
inject: ['./src/utils/import-meta-url.js'],
define: {
'import.meta.url': 'import_meta_url',
'process.env.NODE_ENV': JSON.stringify('production'),
...(process.env.CRAFT_BUILD_SHA && {
'process.env.CRAFT_BUILD_SHA': JSON.stringify(
process.env.CRAFT_BUILD_SHA,
),
}),
},
outfile: 'dist/craft.js',
plugins,
});
// Helper to check if file exists
async function exists(path) {
try {
await stat(path);
return true;
} catch {
return false;
}
}
// Post-build processing with race condition handling for parallel test builds
// Another parallel build may have already processed the file, so all operations
// must gracefully handle ENOENT errors
try {
// Add shebang if not present
const content = await readFile('dist/craft.js', 'utf-8');
const hasShebang = content.startsWith('#!');
if (!hasShebang) {
await writeFile('dist/craft.js', '#!/usr/bin/env node\n' + content);
}
// Rename to final executable name
await rename('dist/craft.js', 'dist/craft');
} catch (err) {
// ENOENT means another parallel build already processed the file
if (err.code !== 'ENOENT') {
throw err;
}
}
// Ensure permissions are set (idempotent)
if (await exists('dist/craft')) {
await chmod('dist/craft', 0o755);
}
// Clean up source map if it wasn't deleted by Sentry plugin
try {
await unlink('dist/craft.js.map');
} catch {
// Source map already deleted by Sentry plugin or doesn't exist
}