Skip to content

Commit 5db528d

Browse files
committed
implement compile in function of compileAst
1 parent 7aeea03 commit 5db528d

File tree

1 file changed

+13
-70
lines changed

1 file changed

+13
-70
lines changed

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

Lines changed: 13 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@ import path, { dirname } from 'node:path'
66
import { pathToFileURL } from 'node:url'
77
import {
88
__unstable__loadDesignSystem as ___unstable__loadDesignSystem,
9-
compile as _compile,
109
compileAst as _compileAst,
1110
Features,
1211
} from 'tailwindcss'
1312
import type { AstNode } from '../../tailwindcss/src/ast'
13+
import * as CSS from '../../tailwindcss/src/css-parser'
1414
import { getModuleDependencies } from './get-module-dependencies'
1515
import { rewriteUrls } from './urls'
1616

1717
export { Features }
1818

1919
export type Resolver = (id: string, base: string) => Promise<string | false | undefined>
2020

21+
export interface CompileOptions {
22+
base: string
23+
onDependency: (path: string) => void
24+
shouldRewriteUrls?: boolean
25+
26+
customCssResolver?: Resolver
27+
customJsResolver?: Resolver
28+
}
29+
2130
export async function compileAst(
2231
ast: AstNode[],
2332
{
@@ -27,14 +36,7 @@ export async function compileAst(
2736

2837
customCssResolver,
2938
customJsResolver,
30-
}: {
31-
base: string
32-
onDependency: (path: string) => void
33-
shouldRewriteUrls?: boolean
34-
35-
customCssResolver?: Resolver
36-
customJsResolver?: Resolver
37-
},
39+
}: CompileOptions,
3840
) {
3941
let compiler = await _compileAst(ast, {
4042
base,
@@ -81,67 +83,8 @@ export async function compileAst(
8183
return compiler
8284
}
8385

84-
export async function compile(
85-
css: string,
86-
{
87-
base,
88-
onDependency,
89-
shouldRewriteUrls,
90-
91-
customCssResolver,
92-
customJsResolver,
93-
}: {
94-
base: string
95-
onDependency: (path: string) => void
96-
shouldRewriteUrls?: boolean
97-
98-
customCssResolver?: Resolver
99-
customJsResolver?: Resolver
100-
},
101-
) {
102-
let compiler = await _compile(css, {
103-
base,
104-
async loadModule(id, base) {
105-
return loadModule(id, base, onDependency, customJsResolver)
106-
},
107-
async loadStylesheet(id, base) {
108-
let sheet = await loadStylesheet(id, base, onDependency, customCssResolver)
109-
110-
if (shouldRewriteUrls) {
111-
sheet.content = await rewriteUrls({
112-
css: sheet.content,
113-
root: base,
114-
base: sheet.base,
115-
})
116-
}
117-
118-
return sheet
119-
},
120-
})
121-
122-
// Verify if the `source(…)` path exists (until the glob pattern starts)
123-
if (compiler.root && compiler.root !== 'none') {
124-
let globSymbols = /[*{]/
125-
let basePath = []
126-
for (let segment of compiler.root.pattern.split('/')) {
127-
if (globSymbols.test(segment)) {
128-
break
129-
}
130-
131-
basePath.push(segment)
132-
}
133-
134-
let exists = await fsPromises
135-
.stat(path.resolve(base, basePath.join('/')))
136-
.then((stat) => stat.isDirectory())
137-
.catch(() => false)
138-
139-
if (!exists) {
140-
throw new Error(`The \`source(${compiler.root.pattern})\` does not exist`)
141-
}
142-
}
143-
144-
return compiler
86+
export async function compile(css: string, options: CompileOptions) {
87+
return compileAst(CSS.parse(css), options)
14588
}
14689

14790
export async function __unstable__loadDesignSystem(css: string, { base }: { base: string }) {

0 commit comments

Comments
 (0)