Attempt resolving embedded files with a relative path

Bun added support for referencing embedded files with require.resolve passing in a relative path
This commit is contained in:
Jordan Pittman 2024-08-26 11:55:17 -04:00
parent e1a87974b6
commit fc86435817
6 changed files with 25 additions and 75 deletions

View file

@ -1,3 +1,19 @@
import { createRequire } from 'node:module'
export const resolve = createRequire(import.meta.url).resolve
const localResolve = createRequire(import.meta.url).resolve
export function resolve(id: string) {
if (id.startsWith('tailwindcss/')) {
try {
return localResolve(id)
} catch (err) {
try {
return localResolve('./' + id.slice(12))
} catch {}
throw err
}
}
return localResolve(id)
}

View file

@ -1,4 +1,4 @@
import { $ } from 'bun'
import { $, Glob } from 'bun'
import { createHash } from 'node:crypto'
import { mkdir, readFile, writeFile } from 'node:fs/promises'
import * as path from 'node:path'
@ -6,11 +6,15 @@ import { fileURLToPath } from 'node:url'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
let embedded = new Glob('../tailwindcss/**/*.{css,json}')
async function buildForPlatform(triple: string, outfile: string) {
let files = Array.from(embedded.scanSync(__dirname))
// We wrap this in a retry because occasionally the atomic rename fails for some reason
for (let i = 0; i < 5; ++i) {
try {
return await $`bun build --compile --target=${triple} ./src/index.ts --outfile=${outfile}`
return await $`bun build --compile --target=${triple} ./src/index.ts --outfile=${outfile} ${{ raw: files.join(' ') }}`
} catch (err) {
if (i < 5) continue

View file

@ -33,6 +33,7 @@
},
"__notes": "These binary packages must be included so Bun can build the CLI for all supported platforms. We also rely on Lightning CSS and Parcel being patched so Bun can statically analyze the executables.",
"devDependencies": {
"bun": "^1.1.26",
"@parcel/watcher-darwin-arm64": "^2.4.2-alpha.0",
"@parcel/watcher-darwin-x64": "^2.4.2-alpha.0",
"@parcel/watcher-linux-arm64-glibc": "^2.4.2-alpha.0",
@ -40,7 +41,7 @@
"@parcel/watcher-linux-x64-glibc": "^2.4.2-alpha.0",
"@parcel/watcher-linux-x64-musl": "^2.4.2-alpha.0",
"@parcel/watcher-win32-x64": "^2.4.2-alpha.0",
"@types/bun": "^1.1.6",
"@types/bun": "^1.1.8",
"lightningcss-darwin-arm64": "^1.25.1",
"lightningcss-darwin-x64": "^1.25.1",
"lightningcss-linux-arm64-gnu": "^1.25.1",

View file

@ -1,7 +1 @@
import { plugin } from 'bun'
import { resolution } from './plugin.ts'
plugin(resolution)
await new Promise((resolve) => setTimeout(resolve))
await import('../../@tailwindcss-cli/src/index.ts')

View file

@ -1,31 +0,0 @@
import type { BunPlugin } from 'bun'
export const resolution: BunPlugin = {
name: 'Custom loader',
target: 'bun',
setup(build) {
// onLoad
// build.onLoad({ filter: /./ }, async (file) => {
// console.log({ file })
// return {
// loader: 'object',
// exports: {},
// }
// })
// Replace resolve.ts with the local resolve.standalone.ts
build.onResolve({ filter: /./ }, (file) => {
console.log('on resolve', { file })
return undefined
// return {
// path: require.resolve(file.path),
// }
// return Object.assign(file, {
// path: require.resolve('./resolve.standalone.ts'),
// })
})
},
}

View file

@ -1,34 +0,0 @@
// @ts-ignore
import twIndex from 'tailwindcss/index.css' with { type: 'file' }
// @ts-ignore
import twPreflight from 'tailwindcss/preflight.css' with { type: 'file' }
// @ts-ignore
import twTheme from 'tailwindcss/theme.css' with { type: 'file' }
// @ts-ignore
import twUtilities from 'tailwindcss/utilities.css' with { type: 'file' }
// @ts-ignore
// import twPkg from 'tailwindcss/package.json' with { type: 'file' }
import { createRequire } from 'node:module'
const localResolve = createRequire(import.meta.url).resolve
export function resolve(id: string) {
if (id === 'tailwindcss/index.css') {
return twIndex
} else if (id === 'tailwindcss/preflight.css') {
return twPreflight
} else if (id === 'tailwindcss/theme.css') {
return twTheme
} else if (id === 'tailwindcss/utilities.css') {
return twUtilities
} else if (id === 'tailwindcss/package.json') {
return twPkg
} else {
return localResolve(id)
}
}