Skip to content

Commit

Permalink
add support for ESM default export in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Mar 4, 2021
1 parent acf35ca commit a96962d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
6 changes: 3 additions & 3 deletions docs/authoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ In case you have more than one template and want to reuse filters, you can put t

## TypeScript support

The AsyncAPI generator has support for the TypeScript for [hooks](#hooks) and Nunjucks's [filters](#filters). Assumptions:
The AsyncAPI generator has TypeScript support for [hooks](#hooks) and Nunjucks's [filters](#filters). Assumptions:

- Installing the `typescript` package and creating the` tsconfig.json` file isn't necessary.
- Source code of the hook/filter must have `ts` extension.
- Each package related with the typings for TypeScript like `@types/node` must be installed in the template under `dependencies` array. This is because the Generator transpiles the TypeScript code on-the-fly while rendering the template.
- Source code of the hook/filter must have `.ts` extension.
- Each package related with the typings for TypeScript like `@types/node` must be installed in the template under `dependencies` array. This is because the Generator transpiles the TypeScript code on-the-fly while rendering the template, so cannot uses packages under `devDependencies`.
- Each package should have installed `@types/node` package to support typings for Node.
5 changes: 5 additions & 0 deletions lib/hooksRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ async function registerConfigHooks(hooks, templateDir, templateConfig) {
* @param {Array} config List of hooks configured in template configuration
*/
function addHook(hooks, mod, config) {
// for ESM `export default {}`
if (typeof mod.default === 'object') {
mod = mod.default;
}

Object.keys(mod).forEach(hookType => {
const moduleHooksArray = [].concat(mod[hookType]);

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a96962d

Please sign in to comment.