-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Don't inline all env vars in Standalone CLI #19391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+94
−86
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
082a16e
Rework standalone CLI build setup
thecrypticace 66e19b8
Don’t inline all env vars
thecrypticace f18da75
Disable `.env` and `Bunfig.toml` auto loading
thecrypticace c7027ac
Simplify Oxide setup when inlined into binary
thecrypticace c5a3565
Don’t support `NODE_PATH` env in standalone build
thecrypticace 43cfb2d
Enable minification
thecrypticace 9a2048b
Remove `__tw_readFile` hack
thecrypticace File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,85 +1,114 @@ | ||
| import { $ } from 'bun' | ||
| import { createHash } from 'node:crypto' | ||
| import { mkdir, readFile, writeFile } from 'node:fs/promises' | ||
| import * as path from 'node:path' | ||
| import { fileURLToPath } from 'node:url' | ||
|
|
||
| const __dirname = fileURLToPath(new URL('.', import.meta.url)) | ||
|
|
||
| async function buildForPlatform(triple: string, outfile: string) { | ||
| // We wrap this in a retry because occasionally the atomic rename fails for some reason | ||
| for (let i = 0; i < 5; ++i) { | ||
| try { | ||
| let cmd = $`bun build --compile --target=${triple} ./src/index.ts --outfile=${outfile} --env inline` | ||
|
|
||
| // This env var is used by our patched versions of Lightning CSS and Parcel Watcher to | ||
| // statically bundle the proper binaries for musl vs glibc | ||
| cmd = cmd.env({ | ||
| PLATFORM_LIBC: triple.includes('-musl') ? 'musl' : 'glibc', | ||
|
|
||
| // Workaround for Bun binary downloads failing on Windows CI when | ||
| // USERPROFILE is passed through by Turborepo. | ||
| USERPROFILE: '', | ||
| }) | ||
|
|
||
| return await cmd | ||
| } catch (err) { | ||
| if (i < 5) continue | ||
|
|
||
| throw new Error(`Failed to build for platform ${triple}`, { cause: err }) | ||
| } | ||
| } | ||
| // Workaround for Bun binary downloads failing on Windows CI when | ||
| // USERPROFILE is passed through by Turborepo. | ||
| // | ||
| // Unfortunately, setting this at runtime doesn't appear to work so we have to | ||
| // spawn a new process without the env var. | ||
| if (process.env.NESTED_BUILD !== '1' && process.env.USERPROFILE && process.env.USERPROFILE !== '') { | ||
| let result = await Bun.$`bun ${fileURLToPath(import.meta.url)}`.env({ | ||
| USERPROFILE: '', | ||
| NESTED_BUILD: '1', | ||
| }) | ||
|
|
||
| process.exit(result.exitCode) | ||
| } | ||
|
|
||
| async function build(triple: string, file: string) { | ||
| let start = process.hrtime.bigint() | ||
|
|
||
| let outfile = path.resolve(__dirname, `../dist/${file}`) | ||
|
|
||
| await buildForPlatform(triple, outfile) | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 100)) | ||
| // We use baseline builds for all x64 platforms to ensure compatibility with | ||
| // older hardware. | ||
| let builds: { target: Bun.Build.Target; name: string }[] = [ | ||
| { name: 'tailwindcss-linux-arm64', target: 'bun-linux-arm64' }, | ||
| { name: 'tailwindcss-linux-arm64-musl', target: 'bun-linux-arm64-musl' }, | ||
| // @ts-expect-error: Either the types are wrong or the runtime needs to be updated | ||
| // to accept a `-glibc` at the end like the types suggest. | ||
thecrypticace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { name: 'tailwindcss-linux-x64', target: 'bun-linux-x64-baseline' }, | ||
| { name: 'tailwindcss-linux-x64-musl', target: 'bun-linux-x64-baseline-musl' }, | ||
| { name: 'tailwindcss-macos-arm64', target: 'bun-darwin-arm64' }, | ||
| { name: 'tailwindcss-macos-x64', target: 'bun-darwin-x64-baseline' }, | ||
| { name: 'tailwindcss-windows-x64.exe', target: 'bun-windows-x64-baseline' }, | ||
| ] | ||
|
|
||
| let summary: { target: Bun.Build.Target; name: string; sum: string }[] = [] | ||
|
|
||
| // Build platform binaries and checksum them. | ||
| let start = process.hrtime.bigint() | ||
| for (let { target, name } of builds) { | ||
| let outfile = path.resolve(__dirname, `../dist/${name}`) | ||
|
|
||
| let result = await Bun.build({ | ||
| entrypoints: ['./src/index.ts'], | ||
| target: 'node', | ||
| minify: { | ||
| whitespace: false, | ||
| syntax: true, | ||
| identifiers: false, | ||
| keepNames: true, | ||
| }, | ||
|
|
||
| define: { | ||
| // This ensures only necessary binaries are bundled for linux targets | ||
| // It reduces binary size since no runtime selection is necessary | ||
| 'process.env.PLATFORM_LIBC': JSON.stringify(target.includes('-musl') ? 'musl' : 'glibc'), | ||
|
|
||
| // This prevents the WASI build from being bundled with the binary | ||
| 'process.env.NAPI_RS_FORCE_WASI': JSON.stringify(''), | ||
|
|
||
| // This simplifies the Oxide loading code a small amount | ||
| 'process.env.NAPI_RS_NATIVE_LIBRARY_PATH': JSON.stringify(''), | ||
|
|
||
| // No need to support additional NODE_PATHs in the standalone build | ||
| 'process.env.NODE_PATH': JSON.stringify(''), | ||
| }, | ||
|
|
||
| compile: { | ||
| target, | ||
| outfile, | ||
|
|
||
| // Disable .env loading | ||
| autoloadDotenv: false, | ||
|
|
||
| // Disable bunfig.toml loading | ||
| autoloadBunfig: false, | ||
| }, | ||
|
|
||
| plugins: [ | ||
| { | ||
| name: 'tailwindcss-plugin', | ||
| setup(build) { | ||
| build.onLoad({ filter: /tailwindcss-oxide\.wasi\.cjs$/ }, async (args) => { | ||
| return { contents: '' } | ||
| }) | ||
| }, | ||
| }, | ||
| ], | ||
| }) | ||
|
|
||
| let entry = result.outputs.find((output) => output.kind === 'entry-point') | ||
| if (!entry) throw new Error(`Build failed for ${target}`) | ||
thecrypticace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| let content = await readFile(outfile) | ||
| let sum = createHash('sha256').update(content).digest('hex') | ||
|
|
||
| let elapsed = process.hrtime.bigint() - start | ||
|
|
||
| return { | ||
| triple, | ||
| file, | ||
| sum, | ||
| elapsed, | ||
| } | ||
| summary.push({ | ||
| target, | ||
| name, | ||
| sum: createHash('sha256').update(content).digest('hex'), | ||
| }) | ||
| } | ||
|
|
||
| await mkdir(path.resolve(__dirname, '../dist'), { recursive: true }) | ||
|
|
||
| // Build platform binaries and checksum them. We use baseline builds for all x64 platforms to ensure | ||
| // compatibility with older hardware. | ||
| let results = await Promise.all([ | ||
| build('bun-linux-arm64', './tailwindcss-linux-arm64'), | ||
| build('bun-linux-arm64-musl', './tailwindcss-linux-arm64-musl'), | ||
|
|
||
| build('bun-linux-x64-baseline', './tailwindcss-linux-x64'), | ||
| build('bun-linux-x64-musl-baseline', './tailwindcss-linux-x64-musl'), | ||
|
|
||
| build('bun-darwin-arm64', './tailwindcss-macos-arm64'), | ||
| build('bun-darwin-x64-baseline', './tailwindcss-macos-x64'), | ||
|
|
||
| build('bun-windows-x64-baseline', './tailwindcss-windows-x64.exe'), | ||
| ]) | ||
|
|
||
| // Write the checksums to a file | ||
| let sumsFile = path.resolve(__dirname, '../dist/sha256sums.txt') | ||
| let sums = results.map(({ file, sum }) => `${sum} ${file}`) | ||
|
|
||
| console.table( | ||
| results.map(({ triple, sum, elapsed }) => ({ | ||
| triple, | ||
| sum, | ||
| elapsed: `${(Number(elapsed) / 1e6).toFixed(0)}ms`, | ||
| })), | ||
| ) | ||
| let sums = summary.map(({ name, sum }) => `${sum} ./${name}`) | ||
|
|
||
| await writeFile(sumsFile, sums.join('\n') + '\n') | ||
|
|
||
| console.table(summary.map(({ target, sum }) => ({ target, sum }))) | ||
|
|
||
| let elapsed = process.hrtime.bigint() - start | ||
| console.log(`Build completed in ${(Number(elapsed) / 1e6).toFixed(0)}ms`) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cursed, I love it.