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))
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)
}
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' },
{ 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 }[] = []
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: {
'process.env.PLATFORM_LIBC': JSON.stringify(target.includes('-musl') ? 'musl' : 'glibc'),
'process.env.NAPI_RS_FORCE_WASI': JSON.stringify(''),
'process.env.NAPI_RS_NATIVE_LIBRARY_PATH': JSON.stringify(''),
'process.env.NODE_PATH': JSON.stringify(''),
},
compile: {
target,
outfile,
autoloadDotenv: false,
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}`)
let content = await readFile(outfile)
summary.push({
target,
name,
sum: createHash('sha256').update(content).digest('hex'),
})
}
await mkdir(path.resolve(__dirname, '../dist'), { recursive: true })
let sumsFile = path.resolve(__dirname, '../dist/sha256sums.txt')
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`)