Transform and emission pipeline
Concept and boundary
The compiler lowers source constructs according to target and module options and can emit declaration trees separately. A before transformer sees input to built-in transforms; an after transformer observes lowered JavaScript-oriented syntax. Declaration hooks are separate and custom transformations do not cause a full recheck.
Pinned implementation breadcrumbs
In the installed TypeScript 5.8.3 bundle lib/typescript.js the following function definitions were located: transformNodes (line 119190); emitFiles (line 119747). These are navigation anchors for source reading, not public API contracts. The probes below observe public behavior; they do not prove every internal step.
Local tooling setup
For each workshop use a fresh folder. Install the pinned compiler with npm install --save-dev typescript@5.8.3, then create the listed files and run node example.cjs with Node.js 22.16.0. Assertions fail if the expected behavior changes. These are local JavaScript tooling scripts, not browser CodeRunner tasks; they do not independently type-check the tooling source.
Workshop 1: custom-transformers
Revisit reference fixture ts-api-custom-transformers (historical V24, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmitOnError: true, declaration: true };const program = ts.createProgram(["input.ts"], options);assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []);const fs = require("node:fs");const outputs = new Map();const write = (name, text) => outputs.set(name, text);const order = [];const mark = stage => context => root => { order.push(stage); return root; };const result = program.emit(undefined, write, undefined, false, { before: [mark("before")], after: [mark("after")], afterDeclarations: [mark("declarations")] });assert(!result.emitSkipped);assert(outputs.has("input.js") && outputs.has("input.d.ts"));assert.deepEqual(order, ["before", "after", "declarations"]);console.log(order.join(" -> "));Expected stdout (exit 0):
before -> after -> declarationsWorkshop 2: after-transformers
Revisit reference fixture ts-api-after-transformers (historical V24, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmitOnError: true, declaration: true };const program = ts.createProgram(["input.ts"], options);assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []);const fs = require("node:fs");const outputs = new Map();const write = (name, text) => outputs.set(name, text);const replaceSeven = context => root => { function visit(node) { if (ts.isNumericLiteral(node) && node.text === "7") return context.factory.createNumericLiteral(8); return ts.visitEachChild(node, visit, context); } return ts.visitNode(root, visit, ts.isSourceFile);};let annotations = 0;const after = context => root => { function inspect(n) { if (ts.isVariableDeclaration(n) && n.type) annotations++; ts.forEachChild(n, inspect); } inspect(root); return replaceSeven(context)(root);};const result = program.emit(undefined, write, undefined, false, { after: [after] });assert(!result.emitSkipped);assert.equal(annotations, 0);const js = outputs.get("input.js");assert(js.includes("exports.count = 8"));fs.writeFileSync("generated.cjs", js);console.log(annotations, require("./generated.cjs").count);Expected stdout (exit 0):
0 8Practice and recap
Capture one syntax kind before and after lowering with an ES5 target.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Transform and emission pipeline — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Transform and emission pipeline?
What does workshop 1 (custom-transformers) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmitOnError: true, declaration: true };const program = ts.createProgram(["input.ts"], options);assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []);const fs = require("node:fs");const outputs = new Map();const write = (name, text) => outputs.set(name, text);const order = [];const mark = stage => context => root => { order.push(stage); return root; };const result = program.emit(undefined, write, undefined, false, { before: [mark("before")], after: [mark("after")], afterDeclarations: [mark("declarations")] });assert(!result.emitSkipped);assert(outputs.has("input.js") && outputs.has("input.d.ts"));assert.deepEqual(order, ["before", "after", "declarations"]);console.log(order.join(" -> "));What does workshop 2 (after-transformers) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmitOnError: true, declaration: true };const program = ts.createProgram(["input.ts"], options);assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []);const fs = require("node:fs");const outputs = new Map();const write = (name, text) => outputs.set(name, text);const replaceSeven = context => root => { function visit(node) { if (ts.isNumericLiteral(node) && node.text === "7") return context.factory.createNumericLiteral(8); return ts.visitEachChild(node, visit, context); } return ts.visitNode(root, visit, ts.isSourceFile);};let annotations = 0;const after = context => root => { function inspect(n) { if (ts.isVariableDeclaration(n) && n.type) annotations++; ts.forEachChild(n, inspect); } inspect(root); return replaceSeven(context)(root);};const result = program.emit(undefined, write, undefined, false, { after: [after] });assert(!result.emitSkipped);assert.equal(annotations, 0);const js = outputs.get("input.js");assert(js.includes("exports.count = 8"));fs.writeFileSync("generated.cjs", js);console.log(annotations, require("./generated.cjs").count);