A Parcel transformer plugin for TypeScript using the TypeScript compiler API (tsc)
npx @tessl/cli install tessl/npm-parcel--transformer-typescript-tsc@2.15.0A Parcel transformer plugin that uses the TypeScript compiler API (tsc) to transpile TypeScript code to JavaScript. It integrates with Parcel's plugin architecture to handle TypeScript files (.ts, .tsx) during the build process, offering TypeScript compilation with source map support, configurable compiler options through tsconfig.json, and seamless integration with Parcel's asset pipeline.
npm install @parcel/transformer-typescript-tscThis package is used as a Parcel plugin and is not typically imported directly in user code. It's configured through Parcel's .parcelrc configuration file:
{
"extends": "@parcel/config-default",
"transformers": {
"*.{ts,tsx}": ["@parcel/transformer-typescript-tsc"]
}
}For plugin development or advanced usage:
import TSCTransformer from "@parcel/transformer-typescript-tsc";Configure the transformer in your .parcelrc file to handle TypeScript files:
{
"extends": "@parcel/config-default",
"transformers": {
"*.{ts,tsx}": ["@parcel/transformer-typescript-tsc"]
}
}TypeScript configuration via tsconfig.json:
{
"compilerOptions": {
"target": "es2020",
"module": "esnext",
"jsx": "react",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}The transformer will automatically:
.ts and .tsx files to JavaScripttsconfig.jsonThe transformer is built as a Parcel plugin using Parcel's transformer architecture:
@parcel/ts-utils.loadTSConfig for TypeScript configurationtypescript.transpileModule)@parcel/source-map for source map handling and VLQ map processing// @flow strict-local)Transpiles TypeScript source code to JavaScript using the official TypeScript compiler API.
/**
* Default export: Pre-configured Transformer instance created with new Transformer()
* Contains loadConfig and transform function implementations
*/
export default Transformer;
/**
* Transformer instance created with object literal containing:
*/
interface TransformerInstance {
loadConfig({config, options}): Promise<TSConfig>;
transform({asset, config, options}): Promise<Asset[]>;
}
interface LoadConfigParams {
config: ConfigFile;
options: ParcelOptions;
}
interface TransformParams {
asset: Asset;
config: TSConfig;
options: ParcelOptions;
}Loads and processes TypeScript configuration from tsconfig.json files.
/**
* Loads TypeScript configuration using @parcel/ts-utils.loadTSConfig
* @param config - Parcel config object
* @param options - Parcel options object
* @returns Processed TypeScript compiler options
*/
loadConfig({config, options}): Promise<TSConfig>;Transforms TypeScript code to JavaScript with source map support.
/**
* Transforms TypeScript asset to JavaScript using typescript.transpileModule
* The implementation:
* 1. Gets code and source map from asset
* 2. Calls typescript.transpileModule with merged compiler options
* 3. Processes source maps and extends with original map if present
* 4. Sets asset type to 'js' and returns the modified asset
* @param asset - Input TypeScript asset
* @param config - TypeScript compiler configuration from loadConfig
* @param options - Parcel build options
* @returns Array containing single transformed JavaScript asset
*/
transform({asset, config, options}): Promise<Asset[]>;The transformer supports all standard TypeScript compiler options through tsconfig.json, with specific defaults:
/**
* Compiler options merged in transform() method:
* User config is spread first, then these options override/ensure correct behavior
*/
interface AppliedCompilerOptions {
jsx: typescript.JsxEmit.React; // React JSX compilation (default, user can override via tsconfig)
noEmit: false; // Always emit output (forced)
module: typescript.ModuleKind.ESNext; // Preserve ES modules for scope hoisting (forced)
sourceMap: boolean; // Based on asset.env.sourceMap (forced)
mapRoot: string; // Set to options.projectRoot (forced)
}
/**
* TranspileOptions passed to typescript.transpileModule
*/
interface TranspileOptions {
compilerOptions: AppliedCompilerOptions;
fileName: string; // Set to asset.filePath
}.ts: TypeScript files.tsx: TypeScript files with JSX.js)The transformer integrates with Parcel's error handling system:
tsconfig.json are properly surfaced// Flow type import (source uses Flow types)
import type {TranspileOptions} from 'typescript';
// Core Parcel plugin system
import {Transformer} from '@parcel/plugin';
// TypeScript configuration utilities
import {loadTSConfig} from '@parcel/ts-utils';
// TypeScript compiler API
import typescript from 'typescript';
// Source map handling
import SourceMap from '@parcel/source-map';>=3.0.0 - Must be installed by the userOverride default compiler options via tsconfig.json:
{
"compilerOptions": {
"jsx": "react-jsx",
"target": "es2022",
"strict": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}Source maps are automatically generated based on Parcel's environment settings:
// Source maps enabled in development
process.env.NODE_ENV === 'development'
// Controlled via Parcel CLI
parcel build --no-source-maps// Complete type definitions used by the transformer
// From TypeScript compiler API (imported as Flow type)
interface TranspileOptions {
compilerOptions?: CompilerOptions;
fileName?: string;
reportDiagnostics?: boolean;
moduleName?: string;
renamedDependencies?: MapLike<string>;
}
interface TranspileOutput {
outputText: string;
diagnostics?: Diagnostic[];
sourceMapText?: string;
}
interface CompilerOptions {
jsx?: JsxEmit;
noEmit?: boolean;
module?: ModuleKind;
sourceMap?: boolean;
mapRoot?: string;
target?: ScriptTarget;
strict?: boolean;
// ... plus all other TypeScript compiler options
}
// Asset interface from Parcel plugin system
interface Asset {
getCode(): Promise<string>;
getMap(): Promise<SourceMap | null>;
setCode(code: string): void;
setMap(map: SourceMap): void;
type: string;
filePath: string;
env: Environment;
}
interface Environment {
sourceMap: boolean;
}
interface ParcelOptions {
projectRoot: string;
}
interface ConfigFile {
// Parcel config file interface
}
interface TSConfig {
// TypeScript configuration object returned by loadTSConfig
}
// Constants from TypeScript API
enum JsxEmit {
React = 'react'
}
enum ModuleKind {
ESNext = 'esnext'
}