Skip to content

Commit dc2e1c5

Browse files
authored
fix: replace Yoga build flag for browsers (#774)
## Summary - replace SATORI_STANDALONE at build time in both default and standalone builds - prevent the default browser bundle from reading the Node-only process global - verify all ESM and CJS artifacts eliminate the runtime build flag ## Why The Yoga loader branches on process.env.SATORI_STANDALONE, but tsup only replaced that expression for the standalone build. The unresolved expression leaked into the default browser bundle and threw ReferenceError: process is not defined. Defining the flag as 0 or 1 for every build keeps the runtime branch correct without adding browser guards that would change standalone init behavior. This is a focused alternative to #749 and leaves the broader browser test framework upgrade separate. Closes #738. Closes #749. ## Validation - pnpm build - pnpm test - pnpm ci-check
1 parent 504b4c9 commit dc2e1c5

4 files changed

Lines changed: 32 additions & 7 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@
6060
"dev": "pnpm run dev:default",
6161
"dev:default": "NODE_ENV=development tsup src/index.ts --watch --ignore-watch playground",
6262
"dev:playground": "turbo dev --filter=satori-playground...",
63-
"build": "pnpm run build:default && pnpm run build:standalone",
63+
"build": "pnpm run build:default && pnpm run build:standalone && pnpm run test:browser-build",
6464
"build:default": "NODE_ENV=production tsup",
6565
"build:standalone": "NODE_ENV=production SATORI_STANDALONE=1 tsup",
6666
"test": "NODE_ENV=test vitest run",
67+
"test:browser-build": "node test/browser-build.mjs",
6768
"test:ui": "NODE_ENV=test vitest --ui --coverage.enabled",
6869
"test-type": "tsc -p tsconfig.json --noEmit && tsc -p playground/tsconfig.json --noEmit",
6970
"dev:test": "NODE_ENV=test vitest --update",

src/satori.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ export default async function satori(
7676
// Use a null-prototype object so that text matching Object.prototype property
7777
// names (e.g. "constructor", "toString") doesn't inherit truthy values from
7878
// the prototype chain when we look it up via `graphemeImages[text]`.
79-
const graphemeImages = Object.assign(Object.create(null), options.graphemeImages)
79+
const graphemeImages = Object.assign(
80+
Object.create(null),
81+
options.graphemeImages
82+
)
8083
// Some Chinese characters have different glyphs in Chinese and
8184
// Japanese, but their Unicode is the same. If the user needs to display
8285
// the Chinese and Japanese characters simultaneously correctly, the user

test/browser-build.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { readFileSync } from 'node:fs'
2+
3+
for (const entry of [
4+
'index.js',
5+
'index.cjs',
6+
'standalone.js',
7+
'standalone.cjs',
8+
]) {
9+
const source = readFileSync(
10+
new URL(`../dist/${entry}`, import.meta.url),
11+
'utf8'
12+
)
13+
14+
if (source.includes('process.env.SATORI_STANDALONE')) {
15+
throw new Error(
16+
`${entry} reads process.env.SATORI_STANDALONE at runtime; ` +
17+
'the browser build must replace this flag at build time.'
18+
)
19+
}
20+
}

tsup.config.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ export default defineConfig({
2323
options.tsconfig = 'tsconfig.json'
2424
options.legalComments = 'external'
2525
},
26-
env: isStandaloneBuild
27-
? {
28-
SATORI_STANDALONE: '1',
29-
}
30-
: {},
26+
// Always replace this flag at build time. Leaving it unresolved in the
27+
// default build makes the browser bundle access the Node-only `process`
28+
// global while selecting the Yoga loader.
29+
env: {
30+
SATORI_STANDALONE: isStandaloneBuild ? '1' : '0',
31+
},
3132
esbuildPlugins: [
3233
{
3334
name: 'optimize tailwind',

0 commit comments

Comments
 (0)