Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a5407e3
allow to pass in the `!important` flag
RobinMalfait Dec 3, 2024
29cc49d
implement `postCssAstToCssAst` and `cssAstToPostCssAst`
RobinMalfait Dec 3, 2024
94aeeab
optimize AST before printing
RobinMalfait Dec 3, 2024
7eefb19
implement new `compileAst(…)` and update `compile(…)`
RobinMalfait Dec 3, 2024
531670b
expose `compileAst` from `@tailwindcss/node`
RobinMalfait Dec 3, 2024
6cabeea
WIP: PostCSS implementation
RobinMalfait Dec 3, 2024
42401b4
Tweak code
thecrypticace Dec 3, 2024
0e954b2
Convert our AST to PostCSS’s AST
thecrypticace Dec 3, 2024
79cadb8
Pass through source information
thecrypticace Dec 3, 2024
2adaf42
print CSS from our own AST
RobinMalfait Dec 4, 2024
ef17c45
only convert our AST into a PostCSS AST when not optimizing
RobinMalfait Dec 4, 2024
f006e36
remove unused variable
RobinMalfait Dec 4, 2024
709947c
rename variables to be more explicit
RobinMalfait Dec 4, 2024
a4df476
add timing info around `cssAstToPostCssAst(…)`
RobinMalfait Dec 4, 2024
71c1c0d
restructure `build(…)`
RobinMalfait Dec 4, 2024
a24e4c4
track the `tailwindCssAst` for subsequent cache hits
RobinMalfait Dec 4, 2024
260beed
ensure PostCSS AST nodes have semicolons
RobinMalfait Dec 4, 2024
87065a8
handle comments correctly
RobinMalfait Dec 4, 2024
3de1864
compute current_mtimes in parallel per file/directory
RobinMalfait Dec 4, 2024
98f66cb
remove instrumentation from `read_dir`
RobinMalfait Dec 4, 2024
84bde5e
trick PostCSS into using 2 spaces instead of 4
RobinMalfait Dec 4, 2024
7aeea03
use `??=` shorthand
RobinMalfait Dec 4, 2024
1a04957
ensure `compile` and `compileAst` re-use the same logic
RobinMalfait Dec 4, 2024
5f22986
run format
RobinMalfait Dec 4, 2024
c5c7c9f
clone the cached PostCSS tree before appending it to the root
RobinMalfait Dec 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ensure compile and compileAst re-use the same logic
  • Loading branch information
RobinMalfait committed Dec 4, 2024
commit 1a0495703e0673ba7fe49dc026b50c02d1c1c126
115 changes: 33 additions & 82 deletions packages/@tailwindcss-node/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,93 +18,29 @@ export { Features }

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

export async function compileAst(
ast: AstNode[],
{
base,
onDependency,
shouldRewriteUrls,

customCssResolver,
customJsResolver,
}: {
base: string
onDependency: (path: string) => void
shouldRewriteUrls?: boolean

customCssResolver?: Resolver
customJsResolver?: Resolver
},
) {
let compiler = await _compileAst(ast, {
base,
async loadModule(id, base) {
return loadModule(id, base, onDependency, customJsResolver)
},
async loadStylesheet(id, base) {
let sheet = await loadStylesheet(id, base, onDependency, customCssResolver)

if (shouldRewriteUrls) {
sheet.content = await rewriteUrls({
css: sheet.content,
root: base,
base: sheet.base,
})
}

return sheet
},
})

// Verify if the `source(…)` path exists (until the glob pattern starts)
if (compiler.root && compiler.root !== 'none') {
let globSymbols = /[*{]/
let basePath = []
for (let segment of compiler.root.pattern.split('/')) {
if (globSymbols.test(segment)) {
break
}
export interface CompileOptions {
base: string
onDependency: (path: string) => void
shouldRewriteUrls?: boolean

basePath.push(segment)
}

let exists = await fsPromises
.stat(path.resolve(base, basePath.join('/')))
.then((stat) => stat.isDirectory())
.catch(() => false)

if (!exists) {
throw new Error(`The \`source(${compiler.root.pattern})\` does not exist`)
}
}

return compiler
customCssResolver?: Resolver
customJsResolver?: Resolver
}

export async function compile(
css: string,
{
base,
onDependency,
shouldRewriteUrls,

customCssResolver,
customJsResolver,
}: {
base: string
onDependency: (path: string) => void
shouldRewriteUrls?: boolean

customCssResolver?: Resolver
customJsResolver?: Resolver
},
) {
let compiler = await _compile(css, {
function createCompileOptions({
base,
onDependency,
shouldRewriteUrls,

customCssResolver,
customJsResolver,
}: CompileOptions) {
return {
base,
async loadModule(id, base) {
async loadModule(id: string, base: string) {
return loadModule(id, base, onDependency, customJsResolver)
},
async loadStylesheet(id, base) {
async loadStylesheet(id: string, base: string) {
let sheet = await loadStylesheet(id, base, onDependency, customCssResolver)

if (shouldRewriteUrls) {
Expand All @@ -117,8 +53,13 @@ export async function compile(

return sheet
},
})
}
}

async function ensureSourceDetectionRootExists(
compiler: { root: Awaited<ReturnType<typeof compile>>['root'] },
base: string,
) {
// Verify if the `source(…)` path exists (until the glob pattern starts)
if (compiler.root && compiler.root !== 'none') {
let globSymbols = /[*{]/
Expand All @@ -140,7 +81,17 @@ export async function compile(
throw new Error(`The \`source(${compiler.root.pattern})\` does not exist`)
}
}
}

export async function compileAst(ast: AstNode[], options: CompileOptions) {
let compiler = await _compileAst(ast, createCompileOptions(options))
await ensureSourceDetectionRootExists(compiler, options.base)
return compiler
}

export async function compile(css: string, options: CompileOptions) {
let compiler = await _compile(css, createCompileOptions(options))
await ensureSourceDetectionRootExists(compiler, options.base)
return compiler
}

Expand Down
Loading