Caution
This is a work in progress. The syntax and the semantic of the proposed extension to JavaScript are not yet fully defined. The packages are published in the GitHub registry, but they are not yet ready for production. The current version is not working.
This is a repo illustrating how to use Adrian Mora's set of packages published in the GitHub registry inside the ull-esit-pl organization. These packages extend the JavaScript language with a new kind of functions. The packages are:
- The JS parser modified: @ull-esit-pl/parser-default-vector
- The AST transformation plugin: @ull-esit-pl/babel-plugin-default-vector
These packages extend JS with a new syntax for arrays. And in the future the syntax will be extended for Objects. Here is an example of such syntax:
let a = [1, 2, 3, else x => x * x];
console.log(a[2]); // 3
console.log(a[5]); // 25 (since 5 * 5 = 25)
Here are the node and npm versions I have used to test the packages:
➜ babel-npm-test node --version
v20.5.0
➜ babel-npm-test npm --version
9.8.0
These packages use the GitHub registry instead of the npm registry. Therefore, remember
to set the registry entry in your .npmrc
file:
➜ babel-default-vector-npm-test git:(main) ✗ cat ~/.npmrc | grep '@ull-esit-pl:'
@ull-esit-pl:registry=https://npm.pkg.github.com
or set an entry registry
in your package.json
file:
➜ babel-default-vector-npm-test git:(main) ✗ jq '.registry' package.json
"https://npm.pkg.github.com"
Then you can proceed to install the packages:
npm i -D @babel/cli@7.10 @ull-esit-pl/babel-plugin-default-vector-plugin @ull-esit-pl/babel-plugin-default-vector-support @ull-esit-pl/parser-default-vector
Your package.json devDependencies
section will look similar to this:
➜ babel-default-vector-npm-test git:(main) ✗ jq '.devDependencies' package.json
{
"@babel/cli": "^7.10.1",
"@ull-esit-pl/babel-plugin-default-vector": "latest",
"@ull-esit-pl/parser-default-vector": "latest"
}
To compile the example above add a babel.config.js
to your workspace folder:
➜ babel-npm-test git:(main) cat babel.config.js
module.exports = {
"plugins": [
"@ull-esit-pl/babel-plugin-default-vector-plugin"
],
}
and then compile it using the installed packages:
➜ babel-npm-test npx babel example.js
This will output the compiled code to the console:
const {
assign,
functionObject
} = require("@ull-esit-pl/babel-plugin-default-vector-support");
const foo = functionObject(function (bar) {
return bar * 2;
});
assign(foo, [10], 5);
console.log(foo(10));
console.log(foo(5));
If you want to save it to a file, use the -o
option.
You can pipe the output to node
:
➜ babel-npm-test npx babel example.js | node -
5
10
or alternatively, use the -o
option to save the output to a file and then run it:
➜ babel-default-vector-npm-test git:(main) npx babel example.js -o example.cjs
➜ babel-default-vector-npm-test git:(main) ✗ node example.cjs
5
10
- The actual code implementation: https://github.com/ULL-ESIT-PL/babel-tanhauhau/tree/Adrian-tfg
- Our tutorial on babel: https://github.com/ULL-ESIT-PL/babel-learning/tree/main
- Some internal information: https://github.com/ULL-ESIT-PL/beca-colaboracion/tree/main
- First steps on publishing a Babel package: https://github.com/ULL-ESIT-PL/babel-learning/blob/main/doc/building-publishing.md#how-to-publish-from-a-fork-of-babel
- Another description of our attempts to define the syntax and semantics of the array else clause: https://github.com/ULL-ESIT-PL/babel-learning/tree/main/src/array-else
- README.md is the one babel has. It has to change to be specific about the extension for the three packages,
- Add
-D
to all install instructions and remove the version number:npm install @ull-esit-pl/babel-plugin-default-vector-plugin -D
- Do we need two separated packages for the plugin and the support? Can we have a single package?