Add tool to transpile JavaScript sources using Babel (#3009)
JerryScript-DCO-1.0-Signed-off-by: Daniella Barsony bella@inf.u-szeged.hu
This commit is contained in:
committed by
Dániel Bátyai
parent
3c69dfab2c
commit
77237960a7
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"@babel/plugin-transform-function-name",
|
||||||
|
"@babel/plugin-proposal-object-rest-spread",
|
||||||
|
"@babel/plugin-transform-block-scoping",
|
||||||
|
"@babel/plugin-transform-destructuring",
|
||||||
|
"@babel/plugin-transform-block-scoped-functions",
|
||||||
|
"@babel/plugin-transform-unicode-regex",
|
||||||
|
"@babel/plugin-transform-sticky-regex",
|
||||||
|
"@babel/plugin-transform-spread",
|
||||||
|
"@babel/plugin-transform-parameters",
|
||||||
|
"@babel/plugin-transform-object-super",
|
||||||
|
"@babel/plugin-transform-new-target",
|
||||||
|
"@babel/plugin-transform-literals",
|
||||||
|
"@babel/plugin-transform-instanceof"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
# Converting incompatible features with Babel
|
||||||
|
To run ES6 sources with JerryScript that use unsupported language features, you can use Babel to transpile your code, which will output a semantically equivalent source file, where the unsupported features are replaced with ES5.1 code.
|
||||||
|
Babel is a JavaScript compiler that is used to convert ES2015+ code into a backward-compatible version. You can find more information [here](https://babeljs.io/).
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
//Before
|
||||||
|
const fn = () => 1;
|
||||||
|
|
||||||
|
//After conversion
|
||||||
|
|
||||||
|
var fn = function fn() {
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
## Table of Contents
|
||||||
|
* **[Getting ready](#getting-ready)**
|
||||||
|
* Installing node.js and npm
|
||||||
|
* **[Using babel](#using-babel)**
|
||||||
|
* **[Missing features/Polyfill](#missing-features)**
|
||||||
|
|
||||||
|
## Getting ready [](#getting-ready)
|
||||||
|
|
||||||
|
### 1. **Node.js and npm**
|
||||||
|
|
||||||
|
Start by updating the packages with
|
||||||
|
|
||||||
|
`$ sudo apt update`
|
||||||
|
|
||||||
|
Install `nodejs` using the apt package manager
|
||||||
|
|
||||||
|
`$ sudo apt install nodejs`
|
||||||
|
|
||||||
|
Check the version of **node.js** to verify that it installed
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ nodejs --version
|
||||||
|
Output: v8.10.0
|
||||||
|
```
|
||||||
|
|
||||||
|
Next up is installing **npm** with the following command
|
||||||
|
|
||||||
|
`$ sudo apt install npm`
|
||||||
|
|
||||||
|
Verify installation by typing:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ npm --version
|
||||||
|
Output: 6.10.2
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Using babel [](#using-babel)
|
||||||
|
|
||||||
|
Assuming you're in the tools/babel folder,
|
||||||
|
|
||||||
|
`$ sudo npm install`
|
||||||
|
|
||||||
|
After installing the dependencies it is ready to use.
|
||||||
|
|
||||||
|
Place the files/directories you want transpiled to the babel folder and run
|
||||||
|
|
||||||
|
`$ npm run transpile [name of input file/directory] [(OPTIONAL)name of output file/directory]`
|
||||||
|
|
||||||
|
If you want to use the same name, then only give the name once.
|
||||||
|
|
||||||
|
#### Missing features/Polyfill [](#missing-features)
|
||||||
|
Some features aren't implemented yet and found no babel plug-in for them.
|
||||||
|
|
||||||
|
* [Array.from](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)
|
||||||
|
* [Array.prototype.fill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill)
|
||||||
|
* [String.prototype.codePointAt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt)
|
||||||
|
|
||||||
|
In the case you encounter those, use the polyfill found on the linked sites. Also, **be aware** there could be more.
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
"name": "jerryscript",
|
||||||
|
"description": "Ultra-lightweight JavaScript engine for the Internet of Things.",
|
||||||
|
"scripts": {
|
||||||
|
"transpile": "scripty"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/jerryscript-project/jerryscript.git"
|
||||||
|
},
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/cli": "^7.4.4",
|
||||||
|
"@babel/core": "^7.4.5",
|
||||||
|
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
|
||||||
|
"@babel/plugin-transform-block-scoped-functions": "^7.2.0",
|
||||||
|
"@babel/plugin-transform-block-scoping": "^7.4.4",
|
||||||
|
"@babel/plugin-transform-destructuring": "^7.4.4",
|
||||||
|
"@babel/plugin-transform-function-name": "^7.4.4",
|
||||||
|
"@babel/plugin-transform-instanceof": "^7.2.0",
|
||||||
|
"@babel/plugin-transform-literals": "^7.2.0",
|
||||||
|
"@babel/plugin-transform-new-target": "^7.4.4",
|
||||||
|
"@babel/plugin-transform-object-super": "^7.2.0",
|
||||||
|
"@babel/plugin-transform-parameters": "^7.4.4",
|
||||||
|
"@babel/plugin-transform-spread": "^7.2.2",
|
||||||
|
"@babel/plugin-transform-sticky-regex": "^7.2.0",
|
||||||
|
"@babel/plugin-transform-unicode-regex": "^7.4.4",
|
||||||
|
"scripty": "^1.9.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
Executable
+38
@@ -0,0 +1,38 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# Copyright JS Foundation and other contributors, http://js.foundation
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
if [[ "$#" -gt 2 || "$#" -lt 1 ]]; then
|
||||||
|
echo "Usage: $0 input [output]"
|
||||||
|
echo "* input: name of input directory/file"
|
||||||
|
echo "* output: name of output directory/file (same as input if not given)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ! -d $1 && ! -f $1 ]]; then
|
||||||
|
echo "Error: $1 is not a file or directory"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
FLAG='--out-file'
|
||||||
|
if [[ -d $1 ]]; then
|
||||||
|
FLAG='--out-dir'
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$#" -eq 1 ]]; then
|
||||||
|
./node_modules/.bin/babel $1 $FLAG $1 --verbose
|
||||||
|
else
|
||||||
|
./node_modules/.bin/babel $1 $FLAG $2 --verbose
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user