API Reference

jspicl

Entry point for transpiling your JavaScript code.

function jspicl(source: string, options?: Options): Output;

Options

type Options = {
prettify?: boolean;
customMappers?: Record<string, AstNodeVisitor>;
};

Output

type Output = {
code: string;
polyfills: Record<string, string>;
};

Types

Types can be imported from @jspicl/core/types:

import type {
JspiclOptions,
JspiclOutput,
AstNodeParser
} from "@jspicl/core/types";

Custom Mappers

You can provide custom mappers to override how specific AST node types are transpiled:

import {jspicl} from "@jspicl/core";

const customMappers = {
Literal: ({raw}) => {
// Custom handling for literals
return raw === "null" ? "nil" : raw;
}
};

const result = jspicl(source, {customMappers});

Each mapper receives the AST node and an options object with:

Supported Polyfills

When your JavaScript uses methods without direct Lua equivalents, jspicl automatically detects and provides polyfill implementations:

The polyfills are returned separately so you can include them once at the top of your PICO-8 cartridge.

ASTNode

Represents a node in the syntax tree. All nodes contain at least a type property.

type ASTNode = {
type: string;
[key: string]: any;
};

AstNodeVisitor

Contract for a visitor function. The function should be named after the AST node type. Binding a scopeBoundary field on the function will create a new scope for all variables defined inside the hierarchy.

type AstNodeVisitor<T extends ASTNode = ASTNode> = {
(node: T, options: AstNodeVisitorOptions): string;
scopeBoundary?: boolean;
};

AstNodeVisitorOptions

Includes references to the transpile function and scope.

type AstNodeVisitorOptions = {
transpile: TranspileFunction;
scope: {
variables: Record<string, any>;
[key: string]: any;
};
};

TranspileFunction

type TranspileFunction = (node: ASTNode, options?: TranspileOptions) => string;

TranspileOptions

type TranspileOptions = {arraySeparator?: string};