rscute: In-memory TypeScript Bundler and Executor

1.2.2 · active · verified Tue Apr 21

rscute is an in-memory TypeScript bundler and executor, leveraging `@swc/core` for high-performance AST transformation and compilation. Currently at stable version 1.2.2, it exhibits an active release cadence with frequent updates. Its core functionality involves intercepting Node.js `require` calls, recursively resolving TypeScript and JavaScript dependencies, and evaluating them as a single, flattened bundle entirely within memory. Key differentiators include its transparent runtime execution capabilities for TypeScript files, a dedicated API for in-memory bundling without execution, and automatic symbol mangling to prevent name collisions in the flattened module scope. It supports various entry points, including a CLI, a Node.js `-r` flag hook, and dedicated programmatic APIs for code execution in a sandboxed VM context or for bundling to a string.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `rscute` programmatically to first bundle a TypeScript file and its dependencies into a single JavaScript string, and then execute that bundled string within an isolated Node.js `vm.Context` to access its exports. It highlights `rscute`'s capabilities for both in-memory compilation and runtime execution.

import { bundle } from 'rscute/bundle';
import { execute } from 'rscute/vm';
import path from 'path';
import fs from 'fs';

// Create a temporary script.ts for the example
const tempScriptContent = `
  export const getMessage = (name: string): string => {
    return \`Hello, \${name} from bundled code!\`;
  };
  export const someValue = 42;
`;
const tempScriptPath = path.resolve(__dirname, 'temp-script.ts');
fs.writeFileSync(tempScriptPath, tempScriptContent);

try {
  // 1. Bundle the TypeScript file into a single JavaScript string
  console.log('Bundling temp-script.ts...');
  const bundledCode = bundle(tempScriptPath);
  console.log('\n--- Bundled Code (truncated) ---');
  console.log(bundledCode.substring(0, 200) + '...');

  // 2. Execute the bundled code in a VM context
  console.log('\nExecuting bundled code in VM...');
  const moduleExports = execute(bundledCode);

  // 3. Use the exports from the executed code
  if (moduleExports && typeof moduleExports.getMessage === 'function') {
    console.log(`Result from executed bundle: ${moduleExports.getMessage('rscute user')}`);
    console.log(`Exported value: ${moduleExports.someValue}`);
  } else {
    console.error('Failed to get expected exports from bundled code.');
  }
} catch (error) {
  console.error('An error occurred:', error);
} finally {
  // Clean up the temporary file
  fs.unlinkSync(tempScriptPath);
}

view raw JSON →