Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A runtime transformer that can convert TypeScript, JavaScript, JSX, TSX, and CSS code to Node.js-compatible code.

NotificationsYou must be signed in to change notification settings

code-found/rts.js

Repository files navigation

npm versionnpm downloadslicense

starsPRs welcome

A powerful Node.js runtime transformer that enables direct execution of TypeScript, JSX, TSX, and CSS files without requiring pre-compilation. Built with SWC for fast compilation and designed for seamless integration with Node.js module system.

📖中文文档:查看中文版 README

Features

  • Runtime TypeScript Compilation: Execute.ts and.tsx files directly with SWC
  • Module Alias Support: Configure path aliases for cleaner imports
  • Extensible Transformer System: Add custom transformers for additional file types
  • Node.js Version Compatibility: Works with both Node.js <24 (polyfill) and >=24 (native hooks)
  • Zero Build Step: No pre-compilation required, everything happens at runtime
  • Fast Performance: Leverages SWC for speedy compilation
  • TypeScript Support: Full TypeScript support with decorators and metadata

Installation

npm install rts.js

Quick Start

Using Register Module (Recommended)

The easiest way to use RTS is with the register module:

# Run TypeScript files directlynode -r rts.js/register app.ts# Or use in package.json scripts
{"scripts": {"start":"node -r rts.js/register src/index.ts","dev":"node -r rts.js/register src/dev.ts"  }}

Basic Usage

import{registerRTS}from'rts.js';// Register RTS hooksconstcleanup=registerRTS();// Now you can import TypeScript files directlyimport{MyComponent}from'./components/MyComponent.tsx';// Cleanup when donecleanup();

With Module Aliases

import{registerRTS}from'rts.js';constcleanup=registerRTS({alias:{'@components':'./src/components','@utils':['./src/utils','./src/helpers'],'@types':'./src/types'}});// Use aliases in your importsimport{Button}from'@components/Button';import{formatDate}from'@utils/date';

Custom Transformers

import{registerRTS}from'rts.js';importtype{TransformerHook}from'rts.js';// Create a custom CSS transformerconstCSSHook:TransformerHook={exts:['.css'],hook:(code:string)=>{// Transform CSS to JS modulereturn`export default${JSON.stringify(code)};`;}};constcleanup=registerRTS({transformers:[CSSHook]});

API Reference

registerRTS(options?)

Registers RTS hooks with Node.js module system.

Parameters:

  • options (optional): Configuration object
    • alias: Module alias mapping
    • transformers: Array of custom transformers

Returns: Cleanup function to deregister hooks

RTSOptions

interfaceRTSOptions{alias?:Record<string,string[]|string>;transformers?:TransformerHook[];}

TransformerHook

interfaceTransformerHook{exts:string[];hook:(code:string,src:string)=>string;}

Configuration

Module Aliases

Aliases allow you to use shorter import paths that resolve to actual file paths:

constcleanup=registerRTS({alias:{'@components':'./src/components','@utils':['./src/utils','./src/helpers'],'@types':'./src/types'}});

Custom Transformers

Create custom transformers for additional file types:

constJSONHook:TransformerHook={exts:['.json'],hook:(code:string)=>{constdata=JSON.parse(code);return`module.exports =${JSON.stringify(data)};`;}};constcleanup=registerRTS({transformers:[JSONHook]});

Node.js Compatibility

RTS supports both older and newer Node.js versions:

  • Node.js >=24: Uses nativeModule.registerHooks API
  • Node.js <24: Uses polyfill implementation with same functionality

Usage Examples

Express.js Application

// app.tsimportexpressfrom'express';import{registerRTS}from'rts';constcleanup=registerRTS({alias:{'@routes':'./src/routes','@middleware':'./src/middleware'}});import{userRoutes}from'@routes/users';import{authMiddleware}from'@middleware/auth';constapp=express();app.use('/api/users',authMiddleware,userRoutes);app.listen(3000,()=>{console.log('Server running on port 3000');});// Cleanup on process exitprocess.on('SIGINT',()=>{cleanup();process.exit(0);});

React Component

// components/Button.tsximportReactfrom'react';interfaceButtonProps{children:React.ReactNode;onClick?:()=>void;variant?:'primary'|'secondary';}exportconstButton:React.FC<ButtonProps>=({   children,   onClick,   variant='primary'})=>{return(<buttonclassName={`btn btn-${variant}`}onClick={onClick}>{children}</button>);};

CSS Module

/* styles/button.css */.btn {padding:8px16px;border: none;border-radius:4px;cursor: pointer;font-size:14px;}.btn-primary {background-color:#007bff;color: white;}.btn-secondary {background-color:#6c757d;color: white;}

Scripts

  • pnpm start: Run the development server
  • pnpm test: Run tests
  • pnpm test:watch: Run tests in watch mode
  • pnpm test:coverage: Run tests with coverage
  • pnpm run lint: Run linting
  • pnpm run lint:fix: Fix linting issues
  • pnpm run check: Run type checking
  • pnpm run check:fix: Fix type checking issues
  • pnpm run format: Format code
  • pnpm run build: Build the project (implement as needed)

Release Process

This project usesChangesets for version management and releases.

Creating a release:

# 1. Create a changesetpnpm changeset# 2. Build and testpnpm run buildpnpmtest# 3. Releasepnpm run release

Available release commands:

  • pnpm changeset: Create a new changeset
  • pnpm run release: Full release process
  • pnpm run release:dry-run: Test release without making changes
  • pnpm run release:build: Build and validate
  • pnpm run release:test: Run tests only
  • pnpm run release:tag: Create git tag for current version

For detailed information, see theRelease Guide.

Architecture

RTS consists of several key components:

Module Resolver (src/resolver/index.ts)

Handles module path resolution, caching, and alias mapping. Integrates with Node.js module system through hooks.

Transformers (src/transformer/)

Transform source code from one format to another. Currently includes TypeScript transformer using SWC.

Register System (src/register/index.ts)

Provides Node.js version compatibility by using native APIs when available and polyfills for older versions.

Configuration (src/config/index.ts)

Handles configuration loading, parsing, and merging.

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the ISC License - see theLICENSE file for details.

Acknowledgments

  • SWC for fast TypeScript compilation
  • Node.js team for the module hooks API
  • The TypeScript community for inspiration and feedback

About

A runtime transformer that can convert TypeScript, JavaScript, JSX, TSX, and CSS code to Node.js-compatible code.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp