or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-parcel--transformer-typescript-tsc

A Parcel transformer plugin for TypeScript using the TypeScript compiler API (tsc)

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@parcel/transformer-typescript-tsc@2.15.x

To install, run

npx @tessl/cli install tessl/npm-parcel--transformer-typescript-tsc@2.15.0

index.mddocs/

Parcel TypeScript TSC Transformer

A 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.

Package Information

  • Package Name: @parcel/transformer-typescript-tsc
  • Package Type: npm
  • Language: JavaScript with Flow types (handles TypeScript files)
  • Installation: This is a Parcel plugin, install via: npm install @parcel/transformer-typescript-tsc
  • Node Version: >= 16.0.0
  • Parcel Version: ^2.15.4

Core Imports

This 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";

Basic Usage

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:

  • Transpile .ts and .tsx files to JavaScript
  • Generate source maps when enabled
  • Respect TypeScript configuration from tsconfig.json
  • Handle JSX compilation (React by default)
  • Preserve ES modules for Parcel's scope hoisting

Architecture

The transformer is built as a Parcel plugin using Parcel's transformer architecture:

  • Plugin Interface: Creates new Transformer instance with object literal containing loadConfig and transform functions
  • Configuration Loading: Uses @parcel/ts-utils.loadTSConfig for TypeScript configuration
  • Compilation Engine: TypeScript compiler API (typescript.transpileModule)
  • Source Map Integration: @parcel/source-map for source map handling and VLQ map processing
  • Asset Pipeline: Integrates with Parcel's asset transformation pipeline
  • Flow Types: Source code uses Flow type annotations (// @flow strict-local)

Capabilities

TypeScript Compilation

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;
}

Configuration Loading

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>;

Code Transformation

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[]>;

TypeScript Compiler Configuration

The transformer supports all standard TypeScript compiler options through tsconfig.json, with specific defaults:

Default Compiler Options

/**
 * 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
}

Supported File Types

  • .ts: TypeScript files
  • .tsx: TypeScript files with JSX

Output

  • File type: JavaScript (.js)
  • Module format: ES modules (preserved for scope hoisting)
  • Source maps: Generated when enabled in Parcel environment
  • JSX: Compiled to React.createElement calls by default

Error Handling

The transformer integrates with Parcel's error handling system:

  • TypeScript compilation errors are reported through Parcel's diagnostic system
  • Configuration errors from invalid tsconfig.json are properly surfaced
  • Source map generation errors are handled gracefully

Dependencies

Required Dependencies

// 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';

Peer Dependencies

  • typescript: >=3.0.0 - Must be installed by the user

Advanced Usage

Custom TypeScript Configuration

Override default compiler options via tsconfig.json:

{
  "compilerOptions": {
    "jsx": "react-jsx",
    "target": "es2022", 
    "strict": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Source Map Configuration

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

Type Definitions

// 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'
}