Pythonic JavaScript that doesn't suck - a Python-to-JavaScript transpiler with clean syntax and performance
npx @tessl/cli install tessl/npm-rapydscript-ng@0.7.0RapydScript-NG is a Python-to-JavaScript transpiler that allows developers to write JavaScript applications using Python syntax and semantics. It provides a comprehensive solution for those who prefer Python's clean, readable syntax over JavaScript's constructs, while maintaining full interoperability with existing JavaScript libraries and frameworks. The compiler supports advanced Python features including classes with inheritance, generators, iterators, comprehensions, exception handling, and a module system that works like Python's import mechanism.
npm install rapydscript-ng (global: npm install -g rapydscript-ng)// Main programmatic usage (package.json main entry)
const { create_compiler } = require("rapydscript-ng");
const RapydScript = create_compiler();// Explicit path to compiler module
const { create_compiler } = require("rapydscript-ng/tools/compiler");
const RapydScript = create_compiler();# Compile a RapydScript file to JavaScript
rapydscript input.pyj --output output.js
# Interactive REPL
rapydscript repl
# Lint RapydScript code
rapydscript lint input.pyj
# Execute RapydScript directly
rapydscript --execute input.pyjconst { create_compiler } = require("rapydscript-ng");
const RapydScript = create_compiler();
// Parse RapydScript source code
const ast = RapydScript.parse(sourceCode, {
filename: 'example.pyj',
basedir: '/path/to/project',
libdir: '/path/to/stdlib'
});
// Generate JavaScript output
const output = new RapydScript.OutputStream({ beautify: true });
ast.print(output);
const javascript = output.get();RapydScript-NG is built around several key components:
Core compilation functionality for transforming RapydScript source code into optimized JavaScript. Supports all Python language features with advanced optimization options.
function create_compiler(): CompilerInstance;
interface CompilerInstance {
parse(code: string, options: ParseOptions): AST_Toplevel;
OutputStream: new (options: OutputOptions) => OutputStream;
DefaultsError: typeof Error;
SyntaxError: typeof Error;
ImportError: typeof Error;
string_template: (template: string, vars: object) => string;
tokenizer: (code: string, options: object) => TokenStream;
compile_time_decorators: object;
ALL_KEYWORDS: string[];
IDENTIFIER_PAT: RegExp;
NATIVE_CLASSES: string[];
}
interface ParseOptions {
filename?: string;
toplevel?: AST_Toplevel;
basedir?: string;
libdir?: string;
import_dirs?: string[];
discard_asserts?: boolean;
module_cache_dir?: string;
}Comprehensive CLI with multiple operational modes for different development workflows. Includes compilation, REPL, linting, testing, and internationalization tools.
# Main modes
rapydscript compile [options] <files...>
rapydscript repl [options]
rapydscript lint [options] <files...>
rapydscript test [tests...]
rapydscript self [options]
rapydscript gettext <files...>
rapydscript msgfmtPython standard library implementations providing familiar APIs for JavaScript environments. Includes math operations, regular expressions, random numbers, encodings, cryptography, and more.
// Import from RapydScript source
import { sin, cos, pi } from "math";
import { randint, choice } from "random";
import { match, search } from "re";
import { base64encode, utf8_decode } from "encodings";Complete AST system with 50+ node classes representing all Python language constructs. Supports programmatic manipulation and analysis of RapydScript code.
// Core AST classes
class AST_Node {
transform(transformer: TreeTransformer): AST_Node;
walk(visitor: TreeWalker): void;
}
class AST_Statement extends AST_Node {}
class AST_Expression extends AST_Node {}
class AST_Function extends AST_Statement {}
class AST_Class extends AST_Statement {}Specialized output generation system that converts AST nodes into optimized JavaScript code. Supports multiple JavaScript versions and output formats.
interface OutputOptions {
beautify?: boolean;
private_scope?: boolean;
omit_baselib?: boolean;
js_version?: number;
keep_docstrings?: boolean;
discard_asserts?: boolean;
}
class OutputStream {
constructor(options: OutputOptions);
print(text: string): void;
get(): string;
}Streaming compilation interface optimized for web browsers and embedded environments. Supports incremental compilation with state preservation across multiple compile operations.
function create_embedded_compiler(
compiler: CompilerInstance,
baselib: string,
runjs?: Function,
name?: string
): EmbeddedCompiler;
interface EmbeddedCompiler {
compile(code: string, options: EmbeddedCompileOptions): string;
toplevel: AST_Toplevel | null;
}
interface EmbeddedCompileOptions {
filename?: string;
keep_baselib?: boolean;
keep_docstrings?: boolean;
js_version?: number;
private_scope?: boolean;
write_name?: boolean;
discard_asserts?: boolean;
}interface CompilerInstance {
parse: (code: string, options: ParseOptions) => AST_Toplevel;
OutputStream: new (options: OutputOptions) => OutputStream;
DefaultsError: typeof Error;
SyntaxError: typeof Error;
ImportError: typeof Error;
string_template: (template: string, vars: object) => string;
tokenizer: (code: string, options: object) => TokenStream;
ALL_KEYWORDS: string[];
IDENTIFIER_PAT: RegExp;
NATIVE_CLASSES: string[];
}
interface ParseOptions {
filename?: string;
toplevel?: AST_Toplevel;
basedir?: string;
libdir?: string;
import_dirs?: string[];
discard_asserts?: boolean;
module_cache_dir?: string;
}
interface OutputOptions {
beautify?: boolean;
private_scope?: boolean;
omit_baselib?: boolean;
js_version?: number;
keep_docstrings?: boolean;
discard_asserts?: boolean;
module_cache_dir?: string;
baselib_plain?: string;
comments?: boolean | Function;
}
class AST_Toplevel extends AST_Block {
print(output: OutputStream): void;
}
class OutputStream {
constructor(options: OutputOptions);
print(text: string): void;
with_indent(body: () => void): void;
get(): string;
toString(): string;
}