Skip to content

Commit

Permalink
docs(interface): Document new implementation export syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Oct 22, 2019
1 parent b02f320 commit d239363
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion lib/output/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d239363

Please sign in to comment.