Custom Processors

Create custom processors to extend jstatico's functionality.

Preprocessor Interface

interface Preprocessor {
  name?: string;
  match: RegExp;
  process: (this: FileResult) => ProcessResult;
}

type ProcessResult = void | {
  extension?: string;
  content?: string;
  meta?: Record<string, unknown>;
};

Example: Custom File Type

Process .upper files by converting content to uppercase:

import type { Preprocessor, ProcessResult } from 'jstatico';

export const processor: Preprocessor = {
  name: 'uppercase',
  match: /\.upper$/,
  process: function(): ProcessResult {
    return {
      extension: '.txt',
      content: this.getContent().toUpperCase()
    };
  }
};

Postprocessor Interface

interface Postprocessor {
  name?: string;
  match: RegExp;
  process: (this: FileResult, tree: TreeMap, context: TreeContext) => ProcessResult;
}

Example: Add Timestamp

Add build timestamp to HTML files:

import type { Postprocessor, ProcessResult } from 'jstatico';

export const processor: Postprocessor = {
  name: 'timestamp',
  match: /\.html$/,
  process: function(): ProcessResult {
    const content = this.getContent();
    const timestamp = new Date().toISOString();
    return {
      content: content.replace('</body>', `<!-- Built: ${timestamp} --></body>`)
    };
  }
};

Writer Interface

interface Writer {
  name?: string;
  match: RegExp;
  write: (file: FileResult, destPath: string) => Promise<void> | void;
}

Registration

See Auto-Discovery or Builder API for how to register processors.