-
Notifications
You must be signed in to change notification settings - Fork 0
Target Custom
Mopsgamer edited this page Aug 23, 2026
·
2 revisions
You can create custom targets by implementing the Target interface. This allows view-ignored to support any custom file ignore format, CLI packer, or project structure.
-
Target Interface Definition:
src/targets/target.ts -
Built-in Target Implementations:
src/targets/
import type { Extractor, IgnoresCb, InitCb, Rule, InternalRules } from "view-ignored/patterns"
export interface Target {
extendsRoot?: string
internalRules: Rule[] | InternalRules
root: string
extractors: Extractor[]
ignores: IgnoresCb
init?: InitCb
}-
extendsRoot: Optionalpackage.jsonfield name (e.g.,"workspaces"). If defined, ignore file extraction extends upwards abovecwduntil apackage.jsoncontaining this field is found. -
internalRules: Pre-compiled rules applied before (before) or after (after) external user-defined ignore files. High-priority rules go inbefore, low-priority/fallback rules inafter. -
root: Starting directory for rule evaluation (relative tocwdor absolute). Defaults to"."or"/". -
extractors: Array of extractors (Extractor[]) responsible for reading ignore files (e.g..gitignore,.dockerignore,package.json). -
ignores: Callback function for testing paths against compiled rules (typicallyruleTest). -
init: Optional initialization function to load global configs, read root manifests, or setup rules dynamically.
This example demonstrates a Docker-like target that extracts .dockerignore rules and caches compiled glob patterns outside hot execution paths:
import type { Target } from "view-ignored/targets"
import {
type Extractor,
extractGitignore,
ruleTest,
ruleCompile,
type InternalRules,
type GlobRule,
} from "view-ignored/patterns"
let cachedDockerRule: GlobRule | null = null
export function makeDocker(): Target {
const extractors: Extractor[] = [
{
extract: extractGitignore,
path: ".dockerignore",
},
]
// Pre-compile internal rules outside hot execution paths
cachedDockerRule ||= ruleCompile({
compiled: null,
excludes: true,
list: [".git/", "node_modules/", ".DS_Store"],
})
const internal: InternalRules = {
before: [cachedDockerRule],
after: [],
}
return {
extractors,
ignores: ruleTest,
internalRules: internal,
root: ".",
}
}If your target needs to read configuration files before scanning (like parsing .gitconfig or package.json), implement the init function:
import type { Target } from "view-ignored/targets"
import { ruleTest } from "view-ignored/patterns"
export function makeCustomWithInit(): Target {
return {
extractors: [],
ignores: ruleTest,
internalRules: { before: [], after: [] },
root: ".",
init({ fs, cwd, signal, target }, cb) {
fs.readFile(`${cwd}/custom.config.json`, (err, data) => {
if (err || !data) return cb(null)
// Parse config and attach rules to target.internalRules
cb(null)
})
},
}
}