├── assets └── .gitkeep ├── .gitignore ├── src ├── executor.js └── Transpiler.php ├── .editorconfig ├── phpunit.php ├── .travis.yml ├── phpunit.xml ├── tests └── BabelTranspilerTest.php ├── composer.json └── README.md /assets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | node_modules 3 | package.json 4 | composer.lock 5 | assets/executor.bundle.js 6 | -------------------------------------------------------------------------------- /src/executor.js: -------------------------------------------------------------------------------- 1 | var babel = require('../node_modules/babel-core'); 2 | 3 | var options = JSON.parse(JSON.stringify(PHP.babelOptions)); 4 | 5 | print(babel.transform(PHP.sourceCode, options).code); 6 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | charset = utf-8 6 | trim_trailing_whitespace = true 7 | insert_final_newline = true 8 | 9 | [**.php] 10 | indent_style = space 11 | indent_size = 4 12 | -------------------------------------------------------------------------------- /phpunit.php: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /tests/BabelTranspilerTest.php: -------------------------------------------------------------------------------- 1 | assertFileExists(realpath(dirname(__FILE__) . '/../assets/executor.bundle.js')); 9 | } 10 | 11 | public function testFileNotExist() { 12 | $this->assertEquals(Transpiler::transformFile('/not/existent.php'), ''); 13 | } 14 | 15 | public function testOptions() { 16 | $compiled = Transpiler::transform('class MyClass {}', [ 'blacklist' => [ 'useStrict' ]]); 17 | 18 | $this->assertNotEquals($compiled, ''); 19 | $this->assertNotContains('"use strict";', $compiled); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "talyssonoc/php-babel-transpiler", 3 | "description": "Transform JavaScript with Babel from PHP", 4 | "require": { 5 | "ext-v8js": ">=0.1.3", 6 | "koala-framework/composer-extra-assets": "~1.1" 7 | }, 8 | "extra": { 9 | "require-npm": { 10 | "babel-core": "^5.8.20", 11 | "browserify": "^11.0.1" 12 | } 13 | }, 14 | "require-dev": { 15 | "phpunit/phpunit": "~4.0.0@stable" 16 | }, 17 | "scripts": { 18 | "post-install-cmd": "\"node_modules/.bin/browserify\" \"src/executor.js\" > assets/executor.bundle.js" 19 | }, 20 | "license": "MIT", 21 | "authors": [ 22 | { 23 | "name": "talyssonoc", 24 | "email": "talyssonoc@gmail.com" 25 | } 26 | ], 27 | "autoload": { 28 | "psr-4": {"Babel\\": "src/"} 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Transpiler.php: -------------------------------------------------------------------------------- 1 | false ]); 17 | 18 | // The compiled bundle will use this attributes as 19 | // `PHP.sourceCode` and `PHP.babelOptions`. 20 | // Check `src/executor.js`. 21 | self::$v8->sourceCode = $sourceCode; 22 | self::$v8->babelOptions = $options; 23 | 24 | try { 25 | ob_start(); 26 | self::$v8->executeString(self::$babel); 27 | return ob_get_contents(); 28 | } 29 | catch(\V8JsException $e) { 30 | ob_end_clean(); 31 | echo $e->getMessage(); 32 | return ''; 33 | } 34 | } 35 | 36 | /** 37 | * Transform the content of a file 38 | * @param string $filePath Absolute path of the file 39 | * @param array $options Associative array of options that will be passed to Babel 40 | * @return string Transformed content of the file 41 | */ 42 | public static function transformFile($filePath, $options = []) { 43 | try { 44 | $fileContent = file_get_contents($filePath); 45 | } 46 | catch(\Exception $e) { 47 | echo $e->getMessage(); 48 | return ''; 49 | } 50 | 51 | return self::transform($fileContent, $options); 52 | } 53 | } 54 | 55 | Transpiler::$v8 = new \V8Js(); 56 | Transpiler::$babel = file_get_contents(realpath(dirname(__FILE__) . '/../assets/executor.bundle.js')); 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP Babel Transpiler 2 | 3 | [![Build Status](https://travis-ci.org/talyssonoc/php-babel-transpiler.svg)](https://travis-ci.org/talyssonoc/php-babel-transpiler) 4 | 5 | Transform JavaScript with [Babel](https://babeljs.io/) from PHP. 6 | 7 | ## Installation 8 | 9 | ### V8Js dependency 10 | 11 | It's important to know that `php-babel-transpiler` has a dependency of the [v8js](https://pecl.php.net/package/v8js) PHP extension. 12 | 13 | You can see how to install it here: [how to install V8Js](https://github.com/talyssonoc/react-laravel/blob/master/install_v8js.md). 14 | 15 | ### Composer 16 | 17 | ```sh 18 | php composer.phar require talyssonoc/php-babel-transpiler 19 | ``` 20 | 21 | ## Usage 22 | 23 | ```php 24 | $transpiledCode = Babel\Transpiler::transform('class MyClass { }'); 25 | $otherTranspiledCode = Babel\Transpiler::transformFile('/my/Class.js', [ 'blacklist' => [ 'useStrict' ] ]); 26 | ``` 27 | 28 | ## API 29 | 30 | - `Babel\Transpiler::transform($sourceCode, $babelOptions)`: Transpile the given source code, passing the given options to Babel, and return the transformed code. 31 | - `Babel\Transpiler::transformFile($filePath, $babelOptions)`: Transpile the file with the given **absolute** path, passing the given options to Babel, and return the transformed code. 32 | 33 | ## License 34 | 35 | The MIT License (MIT) 36 | 37 | Permission is hereby granted, free of charge, to any person obtaining a copy 38 | of this software and associated documentation files (the "Software"), to deal 39 | in the Software without restriction, including without limitation the rights 40 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 41 | copies of the Software, and to permit persons to whom the Software is 42 | furnished to do so, subject to the following conditions: 43 | 44 | The above copyright notice and this permission notice shall be included in 45 | all copies or substantial portions of the Software. 46 | 47 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 48 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 49 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 50 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 51 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 52 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 53 | THE SOFTWARE 54 | --------------------------------------------------------------------------------