-
-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathesbuild.config.mjs
More file actions
149 lines (129 loc) · 4.17 KB
/
esbuild.config.mjs
File metadata and controls
149 lines (129 loc) · 4.17 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
import { sentryEsbuildPlugin } from '@sentry/esbuild-plugin';
import { config } from 'dotenv';
import { execSync } from 'child_process';
import * as esbuild from 'esbuild';
import fs from 'fs';
config();
// check if no-sentry-upload command line argument is set
const noSentryUpload = process.argv.includes('--no-sentry-upload');
// check if we're in wrangler dev mode (wrangler sets WRANGLER_COMMAND=dev)
const isWranglerDev = process.env.WRANGLER_COMMAND === 'dev';
const gitCommit = execSync('git rev-parse --short HEAD').toString().trim();
const gitUrl = execSync('git remote get-url origin').toString().trim();
const gitBranch = execSync('git rev-parse --abbrev-ref HEAD')
.toString()
.trim()
.replace(/[\\\/]/g, '-');
let workerName = 'fixtweet';
// Get worker name from wrangler.toml
try {
workerName = fs
.readFileSync('wrangler.toml')
.toString()
.match(/name ?= ?"(.+?)"/)[1];
} catch (e) {
console.error(`Error reading wrangler.toml to find worker name, using 'fixtweet' instead.`);
}
const releaseName = `${workerName}-${gitBranch}-${gitCommit}-${new Date()
.toISOString()
.substring(0, 19)}`;
let envVariables = [
'STANDARD_DOMAIN_LIST',
'STANDARD_BSKY_DOMAIN_LIST',
'STANDARD_TIKTOK_DOMAIN_LIST',
'DIRECT_MEDIA_DOMAINS',
'TEXT_ONLY_DOMAINS',
'INSTANT_VIEW_DOMAINS',
'GALLERY_DOMAINS',
'FORCE_MOSAIC_DOMAINS',
'MOSAIC_DOMAIN_LIST',
'MOSAIC_BSKY_DOMAIN_LIST',
'POLYGLOT_DOMAIN_LIST',
'POLYGLOT_ACCESS_TOKEN',
'API_HOST_LIST',
'BLUESKY_API_HOST_LIST',
'ATMOSPHERE_API_HOST_LIST',
'SENTRY_DSN',
'GIF_TRANSCODE_DOMAIN_LIST',
'VIDEO_TRANSCODE_DOMAIN_LIST',
'VIDEO_TRANSCODE_BSKY_DOMAIN_LIST',
'OLD_EMBED_DOMAINS',
'TWITTER_ROOT'
];
// Inline process.env.* so Workers bundles stay static; Bun/Node read real process.env at runtime.
let defines = {};
for (let envVar of envVariables) {
defines[`process.env.${envVar}`] = JSON.stringify(process.env[envVar] ?? '');
}
defines['process.env.RELEASE_NAME'] = JSON.stringify(releaseName);
try {
const raw = fs.readFileSync('credentials.enc.json', 'utf-8');
let enc;
try {
enc = JSON.parse(raw);
} catch (parseErr) {
const msg = parseErr instanceof Error ? parseErr.message : String(parseErr);
throw new Error(`credentials.enc.json: invalid JSON (${msg})`);
}
if (
enc == null ||
typeof enc !== 'object' ||
Array.isArray(enc) ||
typeof enc.ciphertext !== 'string' ||
typeof enc.iv !== 'string' ||
enc.ciphertext.length === 0 ||
enc.iv.length === 0
) {
throw new Error(
'credentials.enc.json: expected object with non-empty string ciphertext and iv'
);
}
defines['process.env.ENCRYPTED_CREDENTIALS'] = JSON.stringify(enc.ciphertext);
defines['process.env.CREDENTIALS_IV'] = JSON.stringify(enc.iv);
} catch (err) {
if (err && typeof err === 'object' && err.code === 'ENOENT') {
console.warn(
'No credentials.enc.json found; encrypted credential bundle will be empty (local: npm run credentials:encrypt, CI: fetch from R2 before build).'
);
defines['process.env.ENCRYPTED_CREDENTIALS'] = JSON.stringify('');
defines['process.env.CREDENTIALS_IV'] = JSON.stringify('');
} else {
throw err;
}
}
const plugins = [];
if (process.env.SENTRY_DSN && !noSentryUpload && !isWranglerDev && !workerName.includes('canary')) {
plugins.push(
sentryEsbuildPlugin({
org: process.env.SENTRY_ORG,
project: process.env.SENTRY_PROJECT,
telemetry: false,
release: {
name: releaseName,
create: true,
vcsRemote: gitUrl,
setCommits: {
auto: true,
ignoreMissing: true
}
},
// Auth tokens can be obtained from
// https://sentry.io/orgredirect/organizations/:orgslug/settings/auth-tokens/
authToken: process.env.SENTRY_AUTH_TOKEN
})
);
}
// if branding.json doesn't exist, copy branding.example.json to branding.json, we need this for CI tests
if (!fs.existsSync('branding.json')) {
fs.copyFileSync('branding.example.json', 'branding.json');
}
await esbuild.build({
entryPoints: ['src/worker.ts'],
sourcemap: 'external',
outdir: 'dist',
minify: true,
bundle: true,
format: 'esm',
plugins: plugins,
define: defines
});