From d23936329913bf8f9fd8b098c150accfa1c1ad5c Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Tue, 22 Oct 2019 11:10:00 +0200 Subject: [PATCH] =?UTF-8?q?docs(interface):=20Document=20new=C2=A0implemen?= =?UTF-8?q?tation=20export=C2=A0syntax?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 19 ++++++++++++++++--- lib/output/utils.js | 4 +++- 2 files changed, 19 insertions(+), 4 deletions(-) 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) {