From 4ed7eae61e918990764b47d1262ca7de60a7a7f3 Mon Sep 17 00:00:00 2001 From: Viacheslav Turovskyi Date: Thu, 20 Oct 2022 06:04:24 +0000 Subject: [PATCH] fix: change func export to CJS syntax, add example with 'require' (#81) --- API.md | 37 ++++++++++++++++++++++++++++- README.md | 47 ++++++++++++++++++++++++++++++++----- example/bundle-cjs.js | 20 ++++++++++++++++ example/bundle-esm.js | 20 ++++++++++++++++ example/tsconfig.json | 3 ++- src/index.ts | 54 +++++++++++++++++++++++++++++++++++++++---- tsconfig.json | 2 +- 7 files changed, 169 insertions(+), 14 deletions(-) create mode 100644 example/bundle-cjs.js create mode 100644 example/bundle-esm.js diff --git a/API.md b/API.md index 0604381..56ee263 100644 --- a/API.md +++ b/API.md @@ -65,7 +65,8 @@ console.log(document.string()); // get JSON string | [options.referenceIntoComponents] | boolean |

Pass true to resolve external references to components.

| **Example** -```js +**TypeScript** +```ts import { readFileSync, writeFileSync } from 'fs'; import bundle from '@asyncapi/bundler'; @@ -80,3 +81,37 @@ async function main() { main().catch(e => console.error(e)); ``` + +**JavaScript CJS module system** +```js +'use strict'; + +const { readFileSync, writeFileSync } = require('fs'); +const bundle = require('@asyncapi/bundler'); + +async function main() { + const document = await bundle([readFileSync('./main.yaml', 'utf-8')], { + referenceIntoComponents: true, + }); + writeFileSync('asyncapi.yaml', document.yml()); +} + +main().catch(e => console.error(e)); +``` + +**JavaScript ESM module system** +```js +'use strict'; + +import { readFileSync, writeFileSync } from 'fs'; +import bundle from '@asyncapi/bundler'; + +async function main() { + const document = await bundle([readFileSync('./main.yaml', 'utf-8')], { + referenceIntoComponents: true, + }); + writeFileSync('asyncapi.yaml', document.yml()); +} + +main().catch(e => console.error(e)); +``` diff --git a/README.md b/README.md index 1f1608b..2c8e480 100644 --- a/README.md +++ b/README.md @@ -163,9 +163,11 @@ npm install @asyncapi/bundler AsyncAPI Bundler can be easily used within your JavaScript projects as a Node.js module: -```ts -import { readFileSync, writeFileSync } from 'fs'; -import bundle from '@asyncapi/bundler'; +```js +'use strict'; + +const { readFileSync, writeFileSync } = require('fs'); +const bundle = require('@asyncapi/bundler'); async function main() { const filePaths = ['./camera.yml','./audio.yml']; @@ -264,9 +266,9 @@ components: ``` +
-
- +**TypeScript** ```ts import { readFileSync, writeFileSync } from 'fs'; import bundle from '@asyncapi/bundler'; @@ -281,9 +283,42 @@ async function main() { } main().catch(e => console.error(e)); - ``` +**JavaScript CJS module system** +```js +'use strict'; + +const { readFileSync, writeFileSync } = require('fs'); +const bundle = require('@asyncapi/bundler'); + +async function main() { + const document = await bundle([readFileSync('./main.yaml', 'utf-8')], { + referenceIntoComponents: true, + }); + writeFileSync('asyncapi.yaml', document.yml()); +} + +main().catch(e => console.error(e)); +``` + +**JavaScript ESM module system** +```js +'use strict'; + +import { readFileSync, writeFileSync } from 'fs'; +import bundle from '@asyncapi/bundler'; + +async function main() { + const document = await bundle([readFileSync('./main.yaml', 'utf-8')], { + referenceIntoComponents: true, + }); + writeFileSync('asyncapi.yaml', document.yml()); +} + +main().catch(e => console.error(e)); + +``` diff --git a/example/bundle-cjs.js b/example/bundle-cjs.js new file mode 100644 index 0000000..acc852c --- /dev/null +++ b/example/bundle-cjs.js @@ -0,0 +1,20 @@ +/** + * To use CJS module system, make sure + * `package.json` + * DOES NOT contain line + * `"type": "module",` + */ + +'use strict'; + +const { readFileSync, writeFileSync } = require('fs'); +const bundle = require('@asyncapi/bundler'); + +async function main() { + const document = await bundle([readFileSync('./main.yaml', 'utf-8')], { + referenceIntoComponents: true, + }); + writeFileSync('asyncapi.yaml', document.yml()); +} + +main().catch(e => console.error(e)); diff --git a/example/bundle-esm.js b/example/bundle-esm.js new file mode 100644 index 0000000..e10ef2c --- /dev/null +++ b/example/bundle-esm.js @@ -0,0 +1,20 @@ +/** + * To use ESM module system, first add to + * `package.json` + * line + * `"type": "module",` + */ + +'use strict'; + +import { readFileSync, writeFileSync } from 'fs'; +import bundle from '@asyncapi/bundler'; + +async function main() { + const document = await bundle([readFileSync('./main.yaml', 'utf-8')], { + referenceIntoComponents: true, + }); + writeFileSync('asyncapi.yaml', document.yml()); +} + +main().catch(e => console.error(e)); diff --git a/example/tsconfig.json b/example/tsconfig.json index 80c821c..246f858 100644 --- a/example/tsconfig.json +++ b/example/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "types": [ "node" - ] + ], + "esModuleInterop": true, } } diff --git a/src/index.ts b/src/index.ts index 8f45f3d..3bb7c56 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,15 +5,21 @@ import type { AsyncAPIObject } from './spec-types'; /** * - * @param {string[]} files Array of stringified AsyncAPI documents in YAML format, that are to be bundled (or array of filepaths, resolved and passed via `Array.map()` and `fs.readFileSync`, which is the same, see `README.md`). + * @param {string[]} files Array of stringified AsyncAPI documents in YAML + * format, that are to be bundled (or array of filepaths, resolved and passed + * via `Array.map()` and `fs.readFileSync`, which is the same, see `README.md`). * @param {Object} [options] - * @param {string | object} [options.base] Base object whose properties will be retained. - * @param {boolean} [options.referenceIntoComponents] Pass `true` to resolve external references to components. + * @param {string | object} [options.base] Base object whose properties will be + * retained. + * @param {boolean} [options.referenceIntoComponents] Pass `true` to resolve + * external references to components. * * @return {Document} * * @example * + * **TypeScript** + * ```ts * import { readFileSync, writeFileSync } from 'fs'; * import bundle from '@asyncapi/bundler'; * @@ -27,9 +33,44 @@ import type { AsyncAPIObject } from './spec-types'; * } * * main().catch(e => console.error(e)); + * ``` + * + * **JavaScript CJS module system** + * ```js + * 'use strict'; + * + * const { readFileSync, writeFileSync } = require('fs'); + * const bundle = require('@asyncapi/bundler'); + * + * async function main() { + * const document = await bundle([readFileSync('./main.yaml', 'utf-8')], { + * referenceIntoComponents: true, + * }); + * writeFileSync('asyncapi.yaml', document.yml()); + * } + * + * main().catch(e => console.error(e)); + * ``` + * + * **JavaScript ESM module system** + * ```js + * 'use strict'; + * + * import { readFileSync, writeFileSync } from 'fs'; + * import bundle from '@asyncapi/bundler'; + * + * async function main() { + * const document = await bundle([readFileSync('./main.yaml', 'utf-8')], { + * referenceIntoComponents: true, + * }); + * writeFileSync('asyncapi.yaml', document.yml()); + * } + * + * main().catch(e => console.error(e)); + * ``` * */ -export default async function bundle(files: string[], options: any = {}) { +async function bundle(files: string[], options: any = {}) { if (typeof options.base !== 'undefined') { options.base = toJS(options.base); } @@ -47,4 +88,7 @@ export default async function bundle(files: string[], options: any = {}) { return new Document(resolvedJsons as AsyncAPIObject[], options.base); } -export { Document }; +// 'module.exports' instead of direct export of the function is used, to +// maintain backward compatibility with Node.js projects, that use CJS module +// system. +module.exports = bundle; diff --git a/tsconfig.json b/tsconfig.json index dcbb865..e76b0de 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,7 @@ "lib": [ "esnext", ], - "declaration": true, + "declaration": false, "allowJs": true, "skipLibCheck": true, "esModuleInterop": true,