PHP 封装器,用于执行带有 react-jsx 转换插件的 babel,或者回退到 PHP 替代方案。

1.3.1 2019-01-16 14:40 UTC

This package is auto-updated.

Last update: 2024-09-10 14:52:41 UTC


README

Latest Stable Version Build Status StyleCI Test Coverage Code Climate

PHP 封装器,用于执行带有 react-jsx 转换插件的 babel,或者回退到 PHP 替代方案。

用法

首先,如果您还没有安装,需要 composer。然后使用 composer require nodejs-php-fallback/react 获取包,然后如果您还没有,在您的 PHP 文件中要求 composer 自动加载。

<?php

use NodejsPhpFallback\React;

// Require the composer autload in your PHP file if it's not already.
// You do not need to if you use a framework with composer like Symfony, Laravel, etc.
require 'vendor/autoload.php';

$react = new React('path/to/my-react-file.jsx');

// Output to a file:
$react->write('path/to/my-js-file.js');

// Get JS contents:
$jsContents = $react->getResult();

// Output to the browser:
header('Content-type: text/javascript');
echo $react;

// You can also get react code from a string:
$react = new React('
ReactDOM.render(
    <h1>Hello world!</h1>,
    document.getElementById("main")
);
');
// Then write JS with:
$react->write('path/to/my-js-file.js');
// or get it with:
$jsContents = $react->getResult();

// Get source map contents:
$sourceMap = $react->getSourceMap();

// Get source map path:
$sourceMap = $react->getSourceMapFile();

// Pass false to the React constructor if you do not need source map:
$react = new React('path/to/my-react-file.jsx', false);