diff --git a/README.md b/README.md index c75cd226..37fe9c26 100644 --- a/README.md +++ b/README.md @@ -15,20 +15,33 @@ interface SomeInterface { combined with the JavaScript implementation class file ```js -exports.implementation = class SomeInterfaceImpl { +module.exports = class SomeInterfaceImpl { add(x, y) { return x + y; } }; ``` +> Note: It's also possible to use ES2015 module default export syntax: +> +> ```js +> export default class SomeInterfaceImpl { +> add(x, y) { +> return x + y; +> } +> }; +> ``` + will generate a JavaScript wrapper class file roughly like this: ```js const conversions = require("webidl-conversions"); -const impl = require("./utils.js").implSymbol; +const utils = require("./utils.js"); +const impl = utils.implSymbol; -const Impl = require("./SomeInterface-impl.js").implementation; +// utils.importStar is roughly equivalent to Babel's _interopRequireWildcard() +// and TypeScript's __importStar() functions. +const Impl = utils.importStar(require("./SomeInterface-impl.js")).default; class SomeInterface { constructor() { diff --git a/lib/output/utils.js b/lib/output/utils.js index dc1300a7..c1a4cba3 100644 --- a/lib/output/utils.js +++ b/lib/output/utils.js @@ -84,7 +84,9 @@ const importStarCache = new WeakMap(); * * @template T * @param {T} obj - * @return {T extends { default: any } ? T : { default: T } & { [K in keyof T]: T[K]; }} + * @return {T extends { default: any } + * ? T : T extends string | number | bigint | boolean | symbol | null | undefined + * ? { default: T } : { default: T } & { [K in keyof T]: T[K] }} */ function importStar(obj) { if (obj && obj.__esModule) {