From fc864358175749f31ec1ab1fc233f8dbf6e424af Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Mon, 26 Aug 2024 11:55:17 -0400 Subject: [PATCH] Attempt resolving embedded files with a relative path Bun added support for referencing embedded files with require.resolve passing in a relative path --- .../@tailwindcss-cli/src/utils/resolve.ts | 18 +++++++++- packages/@tailwindcss-standalone/build.ts | 8 +++-- packages/@tailwindcss-standalone/package.json | 3 +- packages/@tailwindcss-standalone/src/index.ts | 6 ---- .../@tailwindcss-standalone/src/plugin.ts | 31 ----------------- .../src/resolve.standalone.ts | 34 ------------------- 6 files changed, 25 insertions(+), 75 deletions(-) delete mode 100644 packages/@tailwindcss-standalone/src/plugin.ts delete mode 100644 packages/@tailwindcss-standalone/src/resolve.standalone.ts diff --git a/packages/@tailwindcss-cli/src/utils/resolve.ts b/packages/@tailwindcss-cli/src/utils/resolve.ts index 4702c2a26..35ca1997d 100644 --- a/packages/@tailwindcss-cli/src/utils/resolve.ts +++ b/packages/@tailwindcss-cli/src/utils/resolve.ts @@ -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) +} diff --git a/packages/@tailwindcss-standalone/build.ts b/packages/@tailwindcss-standalone/build.ts index 4bfafd102..c89d03379 100644 --- a/packages/@tailwindcss-standalone/build.ts +++ b/packages/@tailwindcss-standalone/build.ts @@ -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 diff --git a/packages/@tailwindcss-standalone/package.json b/packages/@tailwindcss-standalone/package.json index 7d9279fa9..13177d868 100644 --- a/packages/@tailwindcss-standalone/package.json +++ b/packages/@tailwindcss-standalone/package.json @@ -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", diff --git a/packages/@tailwindcss-standalone/src/index.ts b/packages/@tailwindcss-standalone/src/index.ts index 92f32dc79..1f71a7e43 100644 --- a/packages/@tailwindcss-standalone/src/index.ts +++ b/packages/@tailwindcss-standalone/src/index.ts @@ -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') diff --git a/packages/@tailwindcss-standalone/src/plugin.ts b/packages/@tailwindcss-standalone/src/plugin.ts deleted file mode 100644 index a4eea590e..000000000 --- a/packages/@tailwindcss-standalone/src/plugin.ts +++ /dev/null @@ -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'), - // }) - }) - }, -} diff --git a/packages/@tailwindcss-standalone/src/resolve.standalone.ts b/packages/@tailwindcss-standalone/src/resolve.standalone.ts deleted file mode 100644 index f2251dbca..000000000 --- a/packages/@tailwindcss-standalone/src/resolve.standalone.ts +++ /dev/null @@ -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) - } -}