Skip to main content

NPM and TypeScript support

tip
  • This chapter applies to EC Android 11.0+

NPM support

Install npm

Enable npm in a project

  • Right-click the project → Add NPM support
  • A package.json is created in the project root when done
  • Run npm install <package-name> in the project directory, e.g. npm install md5 — follow the package's official usage

Notes

  • npm packages target different runtimes (web, Node, etc.). EC's JS runtime is pure JS — choose pure-JS packages only
  • Not every npm package is supported

TypeScript support

Install TypeScript

Enable TypeScript in a project

  • Right-click the project → Add TypeScript support
  • A tsconfig.json is created and .d.ts references are generated for JS under libs

TypeScript in the js folder

  • JS under js is compiled to Java bytecode — no require; do not use export, but you can reference other JS files
  • Write .ts in js, e.g. Card.ts — saving generates Car.d.ts and Card.js for use in other JS files
 // File: js/Car.ts
class Car{
test():void{
logd("i am car.")
}
}
  • Call from other JS:
 // File: js/main.js
function main(){
let car = new Car();
car.test()
}

TypeScript in the ts folder

  • Files under ts must export and be loaded with require
 // File: ts/Car1.ts
class Car1{
test1():void{
logd("i am car1.")
}
}
export ={Car1}
  • Call from JS:
 // File: js/main.js
function main(){
let car = require("ts/Car1")
let car1 = new car.Car1();
car1.test1()
}

JS and TS mixed development

tip
  • For security, put script JS/TS under js
  • For security, put UI JS/TS under subjs
  • For project references, files under js (JS and TS) are directly usable; TS outside js cannot be referenced from other projects
  • The above avoids require. If you use require, put JS/TS outside js and obfuscate JS files
  • Compiled JS from TS participates in IEC build; TS sources do not