Skip to content

Commit e262e0f

Browse files
Bundle core @tailwindcss/forms and @tailwindcss/typography with the standalone build
1 parent 9ca7386 commit e262e0f

File tree

6 files changed

+153
-24
lines changed

6 files changed

+153
-24
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os from 'node:os'
2+
import path from 'node:path'
3+
import { candidate, css, html, json, test } from '../utils'
4+
5+
const STANDALONE_BINARY = (() => {
6+
switch (os.platform()) {
7+
case 'win32':
8+
return 'tailwindcss-windows-x64.exe'
9+
case 'darwin':
10+
return os.arch() === 'x64' ? 'tailwindcss-macos-x64' : 'tailwindcss-macos-arm64'
11+
case 'linux':
12+
return os.arch() === 'x64' ? 'tailwindcss-linux-x64' : 'tailwindcss-linux-arm64'
13+
default:
14+
throw new Error(`Unsupported platform: ${os.platform()} ${os.arch()}`)
15+
}
16+
})()
17+
18+
test(
19+
'includes first-party plugins',
20+
{
21+
fs: {
22+
'package.json': json`
23+
{
24+
"dependencies": {
25+
"tailwindcss": "workspace:^",
26+
"@tailwindcss/cli": "workspace:^"
27+
}
28+
}
29+
`,
30+
'index.html': html`
31+
<div className="prose">
32+
<h1>Headline</h1>
33+
<p>
34+
Until now, trying to style an article, document, or blog post with Tailwind has been a
35+
tedious task that required a keen eye for typography and a lot of complex custom CSS.
36+
</p>
37+
</div>
38+
<input type="text" class="form-input" />
39+
`,
40+
'src/index.css': css`
41+
@import 'tailwindcss/theme' theme(reference);
42+
@import 'tailwindcss/utilities';
43+
44+
@plugin '@tailwindcss/forms';
45+
@plugin '@tailwindcss/typography';
46+
`,
47+
},
48+
},
49+
async ({ fs, exec }) => {
50+
await exec(
51+
`${path.resolve(__dirname, `../../packages/@tailwindcss-standalone/dist/${STANDALONE_BINARY}`)} --input src/index.css --output dist/out.css`,
52+
)
53+
54+
await fs.expectFileToContain('dist/out.css', [candidate`form-input`, candidate`prose`])
55+
},
56+
)

packages/@tailwindcss-node/src/compile.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import EnhancedResolve from 'enhanced-resolve'
22
import { createJiti, type Jiti } from 'jiti'
33
import fs from 'node:fs'
44
import fsPromises from 'node:fs/promises'
5+
import type Module from 'node:module'
56
import path, { dirname } from 'node:path'
67
import { pathToFileURL } from 'node:url'
78
import {
@@ -87,6 +88,13 @@ export async function loadModule(id: string, base: string, onDependency: (path:
8788
throw new Error(`Could not resolve '${id}' from '${base}'`)
8889
}
8990

91+
if (typeof resolvedPath !== 'string') {
92+
return {
93+
base,
94+
module: resolvedPath,
95+
}
96+
}
97+
9098
let module = await importModule(pathToFileURL(resolvedPath).href)
9199
return {
92100
base: dirname(resolvedPath),
@@ -98,6 +106,14 @@ export async function loadModule(id: string, base: string, onDependency: (path:
98106
if (!resolvedPath) {
99107
throw new Error(`Could not resolve '${id}' from '${base}'`)
100108
}
109+
110+
if (typeof resolvedPath !== 'string') {
111+
return {
112+
base,
113+
module: resolvedPath,
114+
}
115+
}
116+
101117
let [module, moduleDependencies] = await Promise.all([
102118
importModule(pathToFileURL(resolvedPath).href + '?id=' + Date.now()),
103119
getModuleDependencies(resolvedPath),
@@ -173,7 +189,13 @@ const jsResolver = EnhancedResolve.ResolverFactory.createResolver({
173189
conditionNames: import.meta.url ? ['node', 'import'] : ['node', 'require'],
174190
})
175191

176-
function resolveJsId(id: string, base: string): Promise<string | false | undefined> {
192+
function resolveJsId(id: string, base: string): Promise<string | false | undefined | Module> {
193+
if (typeof globalThis.__tw_resolve === 'function') {
194+
let resolved = globalThis.__tw_resolve(id, base)
195+
if (resolved) {
196+
return Promise.resolve(resolved)
197+
}
198+
}
177199
return runResolver(jsResolver, id, base)
178200
}
179201

packages/@tailwindcss-standalone/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
],
2727
"dependencies": {
2828
"@tailwindcss/cli": "workspace:^",
29+
"@tailwindcss/forms": "^0.5.9",
30+
"@tailwindcss/typography": "^0.5.15",
2931
"detect-libc": "1.0.3",
3032
"enhanced-resolve": "^5.17.1",
3133
"tailwindcss": "workspace:^"

packages/@tailwindcss-standalone/src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ import preflightCss from 'tailwindcss/preflight.css' with { type: 'file' }
77
import themeCss from 'tailwindcss/theme.css' with { type: 'file' }
88
import utilitiesCss from 'tailwindcss/utilities.css' with { type: 'file' }
99

10+
import forms from '@tailwindcss/forms'
11+
import typography from '@tailwindcss/typography'
12+
1013
const localResolve = createRequire(import.meta.url).resolve
1114

1215
globalThis.__tw_resolve = (id, baseDir) => {
1316
let isEmbeddedFileBase = baseDir === '/$bunfs/root' || baseDir?.includes(':/~BUN/root')
1417
const likelyEmbeddedFile =
15-
id === 'tailwindcss' || id.startsWith('tailwindcss/') || isEmbeddedFileBase
18+
id === 'tailwindcss' ||
19+
id.startsWith('tailwindcss/') ||
20+
id.startsWith('@tailwindcss/') ||
21+
isEmbeddedFileBase
1622

1723
if (!likelyEmbeddedFile) {
1824
return false
@@ -38,6 +44,10 @@ globalThis.__tw_resolve = (id, baseDir) => {
3844
case 'utilities':
3945
case 'utilities.css':
4046
return localResolve(utilitiesCss)
47+
case '@tailwindcss/forms':
48+
return forms
49+
case '@tailwindcss/typography':
50+
return typography
4151
default:
4252
return false
4353
}

packages/@tailwindcss-standalone/src/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ declare module '*.css' {
44
}
55

66
declare var __tw_version: string | undefined
7-
declare var __tw_resolve: undefined | ((id: string, base?: string) => string | false)
7+
declare var __tw_resolve: undefined | ((id: string, base?: string) => string | false | object)
88
declare var __tw_readFile:
99
| undefined
1010
| ((path: string, encoding: BufferEncoding) => Promise<string | undefined>)

0 commit comments

Comments
 (0)