Skip to content
Mopsgamer edited this page Aug 23, 2026 · 2 revisions

Creating Custom Targets

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.

Source Code & Reference Links

The Target Interface

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
}

Properties & Fields Explained

  • extendsRoot: Optional package.json field name (e.g., "workspaces"). If defined, ignore file extraction extends upwards above cwd until a package.json containing this field is found.
  • internalRules: Pre-compiled rules applied before (before) or after (after) external user-defined ignore files. High-priority rules go in before, low-priority/fallback rules in after.
  • root: Starting directory for rule evaluation (relative to cwd or 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 (typically ruleTest).
  • init: Optional initialization function to load global configs, read root manifests, or setup rules dynamically.

Custom Docker Target Example

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: ".",
	}
}

Advanced: Dynamic Initialization (init)

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)
			})
		},
	}
}

Related Wiki Pages

Clone this wiki locally