NPM Module
Introduction
Luwak's built-in JavaScript engine supports full ES5 features and a small set of ES6 features. It is recommended to use Babeljs to convert ES6 code to ES5.
Before use, you can install it normally through npm install
and test whether the library functions you need are normal. If there are no problems after testing, you don't need to convert to ES5.
Note: Luwak has a built-in JavaScript engine developed by Golang and does not support browser and Node.js container-specific APIs. Therefore only some NPM packages are supported. NPM packages that call Node.js-specific API cannot be used, for example: log4js-node
Third-party library
JavaScript has an extremely rich set of high-quality third-party libraries, such as:
Library | Github Star count | Purpose |
---|---|---|
axios | 102K | HTTP client |
lodash | 57.7K | Basic function library, such as array/date operations |
moment | 47.6K | date operations |
dayjs | 44K | moment replacement |
cheerio | 27K | HTML parser |
validator.js | 21.9K | String validator |
crypto-js | 14.9K | Encryption and hash algorithm library |
mathjs | 13.6K | Mathematics |
Numeral-js | 9.5K | Number formatting |
The richness of third-party libraries is one of the reasons why we chose JavaScript as the Luwak embedded scripting language.
Call NPM Module in Luwak
Consistent with normal node.js calls, for example:
var validator = require('validator.js');
var isEmail = validator.isEmail('foo@bar.com'); /* => true */
Install NPM Module
To use NPM Module in Luwak, you only need to "download source code-compile-copy". The following uses validator.js as an example to introduce the installation method in detail:
Download source code
Generally downloaded from github.com or npmjs.com:
Compile
First, you need to modify the compilation output target to ES5, because Luwak's JavaScript engine only supports ES5.1 planning and does not fully support ES6:
jsconfig.json
{
"compilerOptions": {
"module": "system",
"target": "ES5"
}
}
Install dependencies and compile into node.js module:
yarn
yarn build:node
Copy
Copy the compiled product index.js file and lib directory to the _node_modules/validator.js/ directory of the Luwak installation directory
verify
Create a new RPC of com.example.test in Luwak and enter the following in the corresponding JS Code:
var validator = require('validator.js');
var isEmail = validator.isEmail('foo@bar.com'); /* => true */
var isNumeric = validator.isNumeric("1234567890"); /* => true */
var isNumeric = validator.isNumeric("N123456789"); /* => false */
Luwak’s preset NPM Module
- lodash -validator.js -crypto-js
- mathjs
These modules are in _node_modules in the luwak installation directory, and developers can freely upgrade or delete them.