Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default module export to support ES6 Typescript #10

Merged
merged 1 commit into from
Sep 16, 2016
Merged

Add default module export to support ES6 Typescript #10

merged 1 commit into from
Sep 16, 2016

Conversation

DenisCarriere
Copy link
Contributor

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);

This enables a more ES6 import syntax.

**current**

```javascript
import * as polylabel from 'polylabel'
```

**With default module**

```javascript
import polylabel from 'polylabel'
```

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

```javascript
var _polylabel = require('polylabel');

var _polylabel2 = _interopRequireDefault(_polylabel);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
```
@TWiStErRob
Copy link

So after this the two interop lines will disappear?

@mourner
Copy link
Member

mourner commented Sep 16, 2016

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
Copy link
Contributor Author

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
Copy link
Contributor Author

@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 mourner merged commit f4d9a4f into mapbox:master Sep 16, 2016
mourner pushed a commit to mapbox/concaveman that referenced this pull request Sep 16, 2016
Mentioned in more details in previous PR mapbox/polylabel#10
@mourner
Copy link
Member

mourner commented Sep 16, 2016

Thanks for the explanation! Merged.

@DenisCarriere
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants