[]previous and next page

Compile a module

Compile a module in the Compiler guide.

For agentsUpdated

compile(source, options) is a pure function. The result has the new code, a source map, the diagnostics that plugins reported, and transformed, which is false when there was nothing to rewrite.

compile.tsts
import { compile } from "@shadow-js/compiler";

const source = "export const Hello = () => <h1>Hello</h1>;";
const result = compile(source, { filename: "hello.tsx" });

console.log(result.transformed);
console.log(result.code.includes('from "@shadow-js/web"'));
console.log(result.diagnostics.length);
console.log(compile("export const n = 1;\n", { filename: "n.ts" }).transformed);
Expected output
true
true
0
false

A syntax error, or JSX the compiler refuses (<p><div /></p> is invalid HTML nesting), throws a CompileError whose diagnostic has a code, a line and a column. The generate option picks the output: "dom" clones templates and binds reactively, "ssr" writes strings with hydration markers.

The line, the column and the offsets of a CompileError, and the source map with its sourcesContent, always describe the module as you passed it in, also when the compiler first rewrote a useStore call or a derived constant in it.