-
Notifications
You must be signed in to change notification settings - Fork 151
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
Conversation
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 }; } ```
So after this the two |
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 ( example: 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);
|
@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 |
Mentioned in more details in previous PR mapbox/polylabel#10
Thanks for the explanation! Merged. |
@mourner
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