Skip to content

Commit 0303828

Browse files
committed
fix: accept readonly arrays for build candidates and Scanner sources
`build()` only iterates its candidates and `Scanner` only reads its sources, but both public types required mutable arrays. Code that holds either as a `readonly` array had to copy it first. - `packages/tailwindcss/src/index.ts`: type `build()` candidates as `readonly string[]` for both `compile()` and `compileAst()`. - `crates/node/src/lib.rs`: emit `ReadonlyArray<SourceEntry>` for `ScannerOptions.sources` via `#[napi(ts_type = ...)]`. - Add type-level regression tests for both public types. Fixes #20706
1 parent b08bf83 commit 0303828

4 files changed

Lines changed: 31 additions & 3 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Fixed
2+
3+
- Allow `readonly` arrays for `build()` candidates and `Scanner` sources, so callers no longer need to copy them into mutable arrays

‎crates/node/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ impl From<SourceEntry> for tailwindcss_oxide::PublicSourceEntry {
8989
#[napi(object)]
9090
pub struct ScannerOptions {
9191
/// Glob sources
92+
#[napi(ts_type = "ReadonlyArray<SourceEntry>")]
9293
pub sources: Option<Vec<SourceEntry>>,
9394
}
9495

‎packages/tailwindcss/src/index.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ScannerOptions, SourceEntry } from '@tailwindcss/oxide'
12
import fs from 'node:fs'
23
import path from 'node:path'
34
import { describe, expect, it, test } from 'vitest'
@@ -7582,3 +7583,26 @@ describe('feature detection', () => {
75827583
expect(compiler.build([])).toEqual('')
75837584
})
75847585
})
7586+
7587+
describe('public types', () => {
7588+
test('`build()` accepts a readonly array of candidates', async () => {
7589+
let compiler = await compile(css`
7590+
@tailwind utilities;
7591+
`)
7592+
7593+
let candidates: readonly string[] = ['flex', 'items-center']
7594+
let output = compiler.build(candidates)
7595+
7596+
expect(output).toContain('.flex')
7597+
expect(output).toContain('.items-center')
7598+
})
7599+
7600+
test('`ScannerOptions.sources` accepts a readonly array', () => {
7601+
let sources: readonly SourceEntry[] = [
7602+
{ base: process.cwd(), pattern: '**/*.html', negated: false },
7603+
]
7604+
let options: ScannerOptions = { sources }
7605+
7606+
expect(options.sources).toBe(sources)
7607+
})
7608+
})

‎packages/tailwindcss/src/index.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ export async function compileAst(
719719
sources: { base: string; pattern: string; negated: boolean }[]
720720
root: Root
721721
features: Features
722-
build(candidates: string[]): AstNode[]
722+
build(candidates: readonly string[]): AstNode[]
723723
}> {
724724
let { designSystem, ast, sources, root, utilitiesNode, features, inlineCandidates } =
725725
await parseCss(input, opts)
@@ -770,7 +770,7 @@ export async function compileAst(
770770
sources,
771771
root,
772772
features,
773-
build(newRawCandidates: string[]) {
773+
build(newRawCandidates: readonly string[]) {
774774
if (features === Features.None) {
775775
return input
776776
}
@@ -865,7 +865,7 @@ export async function compile(
865865
sources: { base: string; pattern: string; negated: boolean }[]
866866
root: Root
867867
features: Features
868-
build(candidates: string[]): string
868+
build(candidates: readonly string[]): string
869869
buildSourceMap(): DecodedSourceMap
870870
}> {
871871
let ast = CSS.parse(css, { from: opts.from })

0 commit comments

Comments
 (0)