mapbox / polylabel

A fast algorithm for finding the pole of inaccessibility of a polygon (in JavaScript and C++)
Other
1.44k stars 151 forks source link

Add default module export to support ES6 Typescript #10

Closed DenisCarriere closed 8 years ago

DenisCarriere commented 8 years ago

Typescript

This enables a more ES6 import syntax used in Typescript.

current

import * as polylabel from 'polylabel'

with default module

import polylabel from 'polylabel'

Babel

Babel does compile the default module without this addition, however it does some hacks in the background.

sample code

import polylabel from 'polylabel';

const polygon = [[[3116,3071],[3118,3068],[3108,3102],[3751,927]]]
polylabel(polygon)

babel converts into

var _polylabel = require('polylabel');

var _polylabel2 = _interopRequireDefault(_polylabel);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var polygon = [[[3116, 3071], [3118, 3068], [3108, 3102], [3751, 927]]]; 
(0, _polylabel2.default)(polygon);
TWiStErRob commented 8 years ago

So after this the two interop lines will disappear?

mourner commented 8 years ago

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.

DenisCarriere commented 8 years ago

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)

DenisCarriere commented 8 years ago

@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.

mourner commented 8 years ago

Thanks for the explanation! Merged.

DenisCarriere commented 8 years ago

@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.