Registry / serialization / php-transpiler

php-transpiler

JSON →
library0.4.0jsnpmunverified

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.

npm install php-transpiler
INSTALL
IMPORT
SIG · PHP-TRANSPILER
P
php-transpiler
serializationjavascriptv0.4.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

generate
import { generate } from 'php-transpiler';
const generate = require('php-transpiler').generate;
ESM export; named export, not default.
default (named export)
import { transpiler } from 'php-transpiler';
Alternatively, the library exposes a 'transpiler' named export.
php-parser
import { parse } from 'php-parser';
const parse = require('php-parser').parseCode;
Use named export 'parse' from php-parser; parseCode is a method, not exported directly.

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'); };
Debug
Known issues
breakingVersion 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.
affects: >=0.3.0
deprecatedThe 'transpiler.generate' function signature may change in future versions.
fix
Monitor changelog and use named imports.
affects: <=0.4.0
gotchaThe transpiled output assumes a $php runtime object with 'stdout.print' method; not standalone JavaScript.
fix
Provide a $php polyfill when executing the generated code.
affects: all
gotchaOnly a subset of PHP constructs are supported; advanced features like classes and namespaces may fail.
fix
Review supported features in documentation before use.
affects: <=0.4.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'print')
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: php-parser is required. Please install it first.
Missing peer dependency php-parser when using php-transpiler.
fix
Run 'npm install php-parser' alongside php-transpiler.
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies
php-parserrequiredRequired to parse PHP code into AST before transpilation
Agent activity
2 hits · last 30 days
node
2
Resources
php-transpiler — npm install php-transpiler · libregistry