Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add module.exports CJS syntax, add example with require #87

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ console.log(document.string()); // get JSON string
| [options.referenceIntoComponents] | <code>boolean</code> | <p>Pass <code>true</code> to resolve external references to components.</p> |

**Example**
```js
**TypeScript**
```ts
import { readFileSync, writeFileSync } from 'fs';
import bundle from '@asyncapi/bundler';

Expand All @@ -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));
```
47 changes: 41 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -264,9 +266,9 @@ components:

```
</details>
<br />

</br>

**TypeScript**
```ts
import { readFileSync, writeFileSync } from 'fs';
import bundle from '@asyncapi/bundler';
Expand All @@ -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));

```

<a name="bundle"></a>

Expand Down
18 changes: 18 additions & 0 deletions example/bundle-cjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* In case `.cjs` extension is used, Node.js will recognize CJS module system
* automatically.
*/

'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));
20 changes: 20 additions & 0 deletions example/bundle-cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
aeworxet marked this conversation as resolved.
Show resolved Hide resolved
* To use `.js` extension with 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));
20 changes: 20 additions & 0 deletions example/bundle-esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
aeworxet marked this conversation as resolved.
Show resolved Hide resolved
* To use `.js` extension with 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));
18 changes: 18 additions & 0 deletions example/bundle-esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* In case `.mjs` extension is used, Node.js will recognize ESM module system
* automatically.
*/

'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));
9 changes: 7 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
"description": "",
"main": "index.js",
"scripts": {
"dev": "node bundle.js"
"start": "npm run start:cjs && npm run start:esm && npm run start:ts",
"start:cjs": "node bundle-cjs.cjs",
"start:esm": "node bundle-esm.mjs",
"start:ts": "ts-node bundle.ts"
},
"keywords": [],
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@asyncapi/bundler": "../",
"@types/node": "^18.7.23"
"@types/node": "^18.7.23",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
}
}
3 changes: 2 additions & 1 deletion example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"types": [
"node"
]
],
"esModuleInterop": true,
}
}
51 changes: 47 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
*
Expand All @@ -27,6 +33,41 @@ 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 = {}) {
Expand All @@ -47,4 +88,6 @@ export default async function bundle(files: string[], options: any = {}) {
return new Document(resolvedJsons as AsyncAPIObject[], options.base);
}

export { Document };
// 'module.exports' is added to maintain backward compatibility with Node.js
// projects, that use CJS module system.
module.exports = bundle;