php-transpiler

raw JSON →
0.4.0 verified Fri May 01 auth: no javascript

A transpiler that converts PHP AST (from php-parser) into JavaScript code. Version 0.4.0 is the latest stable release. The package is mature but sees infrequent updates. It differentiates by targeting the php-parser AST format, enabling automated PHP-to-JS conversion for server-side rendering or migration. Supports conversion of control structures, function calls, and basic output.

error TypeError: Cannot read properties of undefined (reading 'print')
cause Generated code expects '$php.stdout' to exist but no $php context was provided.
fix
Wrap generated code in a context: var $php = { stdout: { print: console.log } };
error Error: php-parser is required. Please install it first.
cause Missing peer dependency php-parser when using php-transpiler.
fix
Run 'npm install php-parser' alongside php-transpiler.
breaking Version 0.3.0 changed the output format to wrap code in module.exports = function($php) {...}.
fix Ensure consuming code expects a function that receives a $php context object.
deprecated The 'transpiler.generate' function signature may change in future versions.
fix Monitor changelog and use named imports.
gotcha The transpiled output assumes a $php runtime object with 'stdout.print' method; not standalone JavaScript.
fix Provide a $php polyfill when executing the generated code.
gotcha Only a subset of PHP constructs are supported; advanced features like classes and namespaces may fail.
fix Review supported features in documentation before use.
npm install php-transpiler
yarn add php-transpiler
pnpm add php-transpiler

Demonstrates transpiling PHP code to JavaScript using php-parser for parsing and php-transpiler for generation.

import { generate } from 'php-transpiler';
import { parse } from 'php-parser';

const phpCode = '<?php echo "Hello World";';
const ast = parse(phpCode);
const jsCode = generate(ast);
console.log(jsCode);
// Output: module.exports = function($php) { $php.stdout.print('hello world'); };