Closed DenisCarriere closed 8 years ago
So after this the two interop
lines will disappear?
Nice! Is this now a common pattern? I didn't see it before in ES5 JS libraries. Also wondering how Babel code looks after the change.
I can't speak too much more about how Babel compiles to ES5.
However, in Typescript, you can define your target at what level you want to compile to (ES3
, ES5
, ES2015/ES6
)
example: polylabel-tests.ts
import polylabel from 'polylabel'
const polygon = [[[3116,3071],[3118,3068],[3108,3102],[3751,927]]]
polylabel(polygon)
Typescript into ES3
$ tsc polylabel-tests.ts --target ES3
"use strict";
var polylabel_1 = require('polylabel');
var polygon = [[[3116, 3071], [3118, 3068], [3108, 3102], [3751, 927]]];
polylabel_1["default"](polygon);
Typescript into ES5
$ tsc polylabel-tests.ts --target ES5
"use strict";
var polylabel_1 = require('polylabel');
var polygon = [[[3116, 3071], [3118, 3068], [3108, 3102], [3751, 927]]];
polylabel_1.default(polygon);
Typescript into ES6
$ tsc polylabel-tests.ts --target ES6
import polylabel from 'polylabel';
const polygon = [[[3116, 3071], [3118, 3068], [3108, 3102], [3751, 927]]];
polylabel(polygon);
Note: No change from the original source example (except for my missing semicolons)
@mourner It's hard to find examples of projects that are purely a single function. Babel makes it work using "magic", so people assume that including export default module
isn't required.
Thanks for the explanation! Merged.
@mourner Another example of ES6 compiled to ES5 using your library as source.
index.ts
export default polylabel
export function polylabel(polygon, precision, debug) {
return "foo bar"
}
$ tsc --target ES5 index.ts
ES5
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = polylabel;
function polylabel(polygon, precision, debug) {
return "foo bar";
}
exports.polylabel = polylabel;
The compiled ES5 is also doing two exports.
Typescript
This enables a more ES6 import syntax used in Typescript.
current
with default module
Babel
Babel does compile the default module without this addition, however it does some hacks in the background.
sample code
babel converts into