leaningtech / cheerp-meta

Cheerp - a C/C++ compiler for Web applications - compiles to WebAssembly and JavaScript
https://labs.leaningtech.com/cheerp
Other
1.03k stars 51 forks source link

[Feature request] -cheerp-make-module=typescript or ES6 #102

Closed sdykae closed 3 years ago

sdykae commented 3 years ago

ES6 modules? import { add } from "./file.js"; import { add } from "./file.ts"; (with tipes :(?)

carlopi commented 3 years ago

Hi @sdykae, ES6 support was on our wish-list and it was recently merged into Cheerp.

An explanation of the feature is here: https://leaningtech.github.io/cheerp-meta/pages/ES6-Modules.

ES6 modules generated by Cheerp will have as default export an initializing function: example.

Basic syntax:

import instantiate from './add.js'

var module = await instantiate();
var add = module.add;
console.log(add(23, 23));

The syntax you suggest is available by composing the original module with a wrapper module: complete example on GitHub's Gist

Composed syntax:

import instantiate from './add.js'

var module = await instantiate();
var add = module.add;

export {add};

then

import {add} from "./wrap_add.js"

console.log(add(23, 19));