Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/skip-ignored-discovery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'zyzz': patch
---

Discovery and the portable adapters' source scan now skip paths that `.gitignore` files ignore and the bundler's output directories, so stale bundles beside the sources are never parsed.
2 changes: 1 addition & 1 deletion site/src/content/docs/api/unplugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const plugin = zyzz.rollup({ reset: true })
- **Type:** `string{:ts}`
- **Default:** `process.cwd(){:ts}`

The source directory to compile. Every module under it contributes CSS, including unimported global styles. Hidden directories, `node_modules`, `dist`, `build`, `coverage`, tests, fixtures, and declaration, test, and benchmark modules are skipped.
The source directory to compile. Every module under it contributes CSS, including unimported global styles. Hidden directories, `node_modules`, `dist`, `build`, `coverage`, the bundler's output directory, paths that `.gitignore` files ignore, tests, fixtures, and declaration, test, and benchmark modules are skipped.

```ts title="build.ts"
import { zyzz } from 'zyzz/unplugin'
Expand Down
3 changes: 2 additions & 1 deletion site/src/content/docs/api/vite.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,12 @@ Discovery covers the first of these that applies:
2. The directories that hold a `zyzz.config.*` module, found by name below the Vite root. Place the configuration at the root of the application's source, such as `src/zyzz.config.ts`, so tool configuration and scripts beside `src/` stay out.
3. The Vite root.

The `zyzz.config.*` search and a root scan skip nested packages: directories below the Vite root that hold their own `package.json`, such as a workspace's examples. [`exclude`](#optionsexclude) removes modules in every case. Adding or moving a `zyzz.config.*` module or a nested `package.json` takes full effect when the server restarts.
The `zyzz.config.*` search and a root scan skip nested packages: directories below the Vite root that hold their own `package.json`, such as a workspace's examples. Every scan also skips each environment's `build.outDir` and the paths that `.gitignore` files ignore, read from the repository root down, or from the scanned directory outside a repository, so stale bundles beside the sources are never parsed. [`exclude`](#optionsexclude) removes modules in every case. Adding or moving a `zyzz.config.*` module or a nested `package.json` takes full effect when the server restarts.

```txt
app/
├── examples/web/ # skipped: nested package
├── dist-preview/ # skipped: listed in .gitignore
├── playwright.config.ts # skipped: outside src/
├── src/
│ ├── global.ts # discovered
Expand Down
256 changes: 256 additions & 0 deletions src/node/internal/Ignores.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
/** Matches paths against `.gitignore` rules, so project discovery skips what Git ignores. @module */
import * as Fs from 'node:fs'
import * as Path from 'node:path'

/** One `.gitignore` pattern, anchored at the directory holding its file. */
export type Rule = {
/** Directory holding the `.gitignore` file, with platform separators. */
readonly base: string
/** Matches directories only, as a trailing slash requires. */
readonly directory: boolean
/** Re-includes a path that an earlier rule ignored. */
readonly negated: boolean
/** Compiled pattern over the `/`-separated path relative to `base`. */
readonly pattern: RegExp
}

/**
* Creates a reader of the `.gitignore` files that apply below source directories. Rules apply from
* the enclosing repository root down, or from the source directory outside a repository. Files are
* read synchronously once per directory, as watcher events need an immediate answer.
* @param options - The source directories discovery walks.
*/
export function create(options: create.Options) {
const directories = new Map<string, boolean>()
const repositories = new Map<string, boolean>()
const rules = new Map<string, readonly Rule[]>()

function repository(directory: string): boolean {
let value = repositories.get(directory)

if (value === undefined) {
const parent = Path.dirname(directory)

value =
Fs.existsSync(Path.join(directory, '.git')) ||
(parent !== directory && repository(parent))
repositories.set(directory, value)
}

return value
}

function read(directory: string): readonly Rule[] {
let value = rules.get(directory)

if (value === undefined) {
const parent = Path.dirname(directory)
const inherited =
parent === directory ||
Fs.existsSync(Path.join(directory, '.git')) ||
(options.roots.includes(directory) && !repository(parent))
? []
: read(parent)
const source = (() => {
try {
return Fs.readFileSync(Path.join(directory, '.gitignore'), 'utf8')
} catch (error) {
const code = (error as NodeJS.ErrnoException).code

if (code === 'ENOENT' || code === 'ENOTDIR' || code === 'EISDIR')
return ''
throw error
}
})()

value = [...inherited, ...parse({ base: directory, source })]
rules.set(directory, value)
}

return value
}

return {
/**
* Whether the rules of a path's directory ignore it. Callers check each ancestor below a
* source directory, since Git never re-includes a path below an ignored directory.
*/
ignored(path: ignored.Options): boolean {
if (!path.directory) return ignored(read(Path.dirname(path.path)), path)

let value = directories.get(path.path)

if (value === undefined) {
value = ignored(read(Path.dirname(path.path)), path)
directories.set(path.path, value)
}

return value
},
}
}

/** Ignore reader contracts. */
export declare namespace create {
/** Directories whose own `.gitignore` starts the rules outside a repository. */
type Options = {
/** Absolute source directories. */
readonly roots: readonly string[]
}
}

/**
* Whether the last rule matching a path ignores it, as Git decides within one directory. A path
* below an ignored directory is the caller's concern: Git never re-includes it.
* @param rules - Rules of every `.gitignore` file that applies, outermost first.
* @param options - Absolute path and whether it names a directory.
*/
export function ignored(
rules: readonly Rule[],
options: ignored.Options,
): boolean {
let value = false

for (const rule of rules) {
if (rule.directory && !options.directory) continue

const relative = relativePath(rule.base, options.path)

if (relative !== undefined && rule.pattern.test(relative))
value = !rule.negated
}

return value
}

/** Path matching contracts. */
export declare namespace ignored {
/** The path to test. */
type Options = {
/** Whether the path is a directory. */
readonly directory: boolean
/** Absolute path with platform separators. */
readonly path: string
}
}

/**
* Parses a `.gitignore` file into rules in file order. Blank lines, comments, and patterns Git
* treats as invalid produce no rule.
* @param options - File text and the directory holding it.
*/
export function parse(options: parse.Options): readonly Rule[] {
const rules: Rule[] = []

for (const line of options.source.split(/\r?\n/)) {
// Trailing spaces are insignificant unless escaped.
let value = line.replace(/(?<!\\)\s+$/, '')

if (!value || value.startsWith('#')) continue

const negated = value.startsWith('!')

if (negated) value = value.slice(1)
if (value.startsWith('\\#') || value.startsWith('\\!'))
value = value.slice(1)

const directory = value.endsWith('/')

if (directory) value = value.slice(0, -1)
if (!value) continue

// A slash before the end anchors the pattern at the file's directory.
const anchored = value.includes('/')

if (value.startsWith('/')) value = value.slice(1)

const source = glob(value)

if (source === undefined) continue

rules.push({
base: options.base,
directory,
negated,
pattern: new RegExp(anchored ? `^${source}$` : `^(?:.*/)?${source}$`),
})
}

return rules
}

/** Rule parsing contracts. */
export declare namespace parse {
/** One `.gitignore` file. */
type Options = {
/** Absolute directory holding the file. */
readonly base: string
/** File text. */
readonly source: string
}
}

/** Translates gitignore glob syntax to a regular expression source, or undefined when malformed. */
function glob(value: string) {
let output = ''

for (let index = 0; index < value.length; index++) {
const character = value[index]!

if (character === '*' && value[index + 1] === '*') {
const start = index === 0 || value[index - 1] === '/'
const end = index + 2 === value.length || value[index + 2] === '/'

// `**` spans directories only as a whole segment; otherwise it is two single-segment stars.
if (start && end) {
if (index + 2 === value.length) output += '.*'
else output += '(?:.*/)?'
index += index + 2 === value.length ? 1 : 2
continue
}
}

if (character === '*') output += '[^/]*'
else if (character === '?') output += '[^/]'
else if (character === '\\') {
const next = value[index + 1]

if (next === undefined) return undefined

output += escape(next)
index++
} else if (character === '[') {
const close = value.indexOf(']', index + 2)

if (close === -1) return undefined

let body = value.slice(index + 1, close)

if (body.startsWith('!')) body = `^${body.slice(1)}`

output += `[${body.replace(/[\\\]]/g, '\\$&')}]`
index = close
} else output += escape(character)
}

return output
}

function escape(character: string) {
return /[.*+?^${}()|[\]\\/]/.test(character) ? `\\${character}` : character
}

/** The `/`-separated path of `path` below `base`, or undefined outside it. */
function relativePath(base: string, path: string) {
const relative = Path.relative(base, path)

if (
!relative ||
relative === '..' ||
relative.startsWith(`..${Path.sep}`) ||
Path.isAbsolute(relative)
)
return undefined

return relative.split(Path.sep).join('/')
}
11 changes: 11 additions & 0 deletions src/unplugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as AtRules from '../compiler/internal/AtRules.js'
import * as Catalogs from '../compiler/internal/Catalogs.js'
import * as ClassName from '../web/internal/ClassName.js'
import * as Contracts from '../node/internal/Contracts.js'
import * as Ignores from '../node/internal/Ignores.js'
import * as Results from '../compiler/internal/Results.js'
import * as Vite from '../vite/index.js'
import * as ThemeRules from '../web/internal/Themes.js'
Expand Down Expand Up @@ -43,6 +44,8 @@ const portable = createUnplugin<Options | undefined, false>(
let assets = new Map<string, Uint8Array>()
let css = ''
let cssMap = ''
// The bundler's output directory, which the scan skips like ignored files.
let outdir: string | undefined
let script = ''

function identity(file: string) {
Expand Down Expand Up @@ -113,6 +116,8 @@ const portable = createUnplugin<Options | undefined, false>(
> = Object.create(null)
const contracts: Record<string, string> = Object.create(null)

const ignores = Ignores.create({ roots: [root] })

async function scan(directory: string): Promise<void> {
directories.add(directory)
const entries = await Fs.readdir(directory, { withFileTypes: true })
Expand All @@ -137,6 +142,11 @@ const portable = createUnplugin<Options | undefined, false>(
continue
const file = Path.join(directory, entry.name)

if (
(entry.isDirectory() && file === outdir) ||
ignores.ignored({ directory: entry.isDirectory(), path: file })
)
continue
if (entry.isDirectory()) await scan(file)
else if (
entry.isFile() &&
Expand Down Expand Up @@ -551,6 +561,7 @@ const portable = createUnplugin<Options | undefined, false>(
options.absWorkingDir ?? process.cwd(),
options.outdir,
)
outdir = options.outdir
},
setup(build) {
// Unplugin forwards watchFiles but not watchDirs from its transform hook.
Expand Down
48 changes: 48 additions & 0 deletions src/vite/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,54 @@ export function unrelated(props: { className?: string | undefined } = {}) {
}
})

test('skips ignored files and build output in discovery', async () => {
const { config, root } = await create()
const global = (color: string) =>
`import { global } from 'zyzz/web'; global({ body: { color: '${color}' } });`

// Stale bundles from earlier builds sit beside the sources.
await Fs.mkdir(Path.join(root, 'bundles'))
await Fs.mkdir(Path.join(root, 'out'))
await Fs.writeFile(
Path.join(root, '.gitignore'),
'bundles/\n*.bundle.js\n!kept.bundle.js\n',
)
await Fs.writeFile(Path.join(root, 'bundles/stale.js'), global('#123456'))
await Fs.writeFile(Path.join(root, 'out/index.js'), global('#234567'))
await Fs.writeFile(Path.join(root, 'stale.bundle.js'), global('#345678'))
await Fs.writeFile(Path.join(root, 'kept.bundle.js'), global('#456789'))

try {
const result = await Vite.build({
...config,
build: { outDir: 'out', write: false },
resolve: {
alias: {
...(config.resolve!.alias as Record<string, string>),
'zyzz/web': Path.resolve('src/web/index.ts'),
},
},
})
const output = Array.isArray(result) ? result[0]! : result
if (!('output' in output)) throw new Error('Unexpected watch build')
const css = output.output
.flatMap((file) =>
file.type === 'asset' && file.fileName.endsWith('.css')
? [String(file.source)]
: [],
)
.join('\n')

expect(css.includes('#06c')).toMatchInlineSnapshot(`true`)
expect(css.includes('#123456')).toMatchInlineSnapshot(`false`)
expect(css.includes('#234567')).toMatchInlineSnapshot(`false`)
expect(css.includes('#345678')).toMatchInlineSnapshot(`false`)
expect(css.includes('#456789')).toMatchInlineSnapshot(`true`)
} finally {
await Fs.rm(root, { recursive: true, force: true })
}
})

test('skips nested packages in discovery unless included', async () => {
const { config, root } = await create()
const example = Path.join(root, 'examples/web')
Expand Down
Loading