Skip to content

Commit 3a16720

Browse files
authored
fix: add module.exports CJS syntax, add example with require (#87)
1 parent 1b19e9c commit 3a16720

File tree

9 files changed

+209
-14
lines changed

9 files changed

+209
-14
lines changed

API.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ console.log(document.string()); // get JSON string
6565
| [options.referenceIntoComponents] | <code>boolean</code> | <p>Pass <code>true</code> to resolve external references to components.</p> |
6666

6767
**Example**
68-
```js
68+
**TypeScript**
69+
```ts
6970
import { readFileSync, writeFileSync } from 'fs';
7071
import bundle from '@asyncapi/bundler';
7172

@@ -80,3 +81,37 @@ async function main() {
8081

8182
main().catch(e => console.error(e));
8283
```
84+
85+
**JavaScript CJS module system**
86+
```js
87+
'use strict';
88+
89+
const { readFileSync, writeFileSync } = require('fs');
90+
const bundle = require('@asyncapi/bundler');
91+
92+
async function main() {
93+
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
94+
referenceIntoComponents: true,
95+
});
96+
writeFileSync('asyncapi.yaml', document.yml());
97+
}
98+
99+
main().catch(e => console.error(e));
100+
```
101+
102+
**JavaScript ESM module system**
103+
```js
104+
'use strict';
105+
106+
import { readFileSync, writeFileSync } from 'fs';
107+
import bundle from '@asyncapi/bundler';
108+
109+
async function main() {
110+
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
111+
referenceIntoComponents: true,
112+
});
113+
writeFileSync('asyncapi.yaml', document.yml());
114+
}
115+
116+
main().catch(e => console.error(e));
117+
```

README.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,11 @@ npm install @asyncapi/bundler
163163

164164
AsyncAPI Bundler can be easily used within your JavaScript projects as a Node.js module:
165165

166-
```ts
167-
import { readFileSync, writeFileSync } from 'fs';
168-
import bundle from '@asyncapi/bundler';
166+
```js
167+
'use strict';
168+
169+
const { readFileSync, writeFileSync } = require('fs');
170+
const bundle = require('@asyncapi/bundler');
169171

170172
async function main() {
171173
const filePaths = ['./camera.yml','./audio.yml'];
@@ -264,9 +266,9 @@ components:
264266

265267
```
266268
</details>
269+
<br />
267270

268-
</br>
269-
271+
**TypeScript**
270272
```ts
271273
import { readFileSync, writeFileSync } from 'fs';
272274
import bundle from '@asyncapi/bundler';
@@ -281,9 +283,42 @@ async function main() {
281283
}
282284

283285
main().catch(e => console.error(e));
284-
285286
```
286287

288+
**JavaScript CJS module system**
289+
```js
290+
'use strict';
291+
292+
const { readFileSync, writeFileSync } = require('fs');
293+
const bundle = require('@asyncapi/bundler');
294+
295+
async function main() {
296+
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
297+
referenceIntoComponents: true,
298+
});
299+
writeFileSync('asyncapi.yaml', document.yml());
300+
}
301+
302+
main().catch(e => console.error(e));
303+
```
304+
305+
**JavaScript ESM module system**
306+
```js
307+
'use strict';
308+
309+
import { readFileSync, writeFileSync } from 'fs';
310+
import bundle from '@asyncapi/bundler';
311+
312+
async function main() {
313+
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
314+
referenceIntoComponents: true,
315+
});
316+
writeFileSync('asyncapi.yaml', document.yml());
317+
}
318+
319+
main().catch(e => console.error(e));
320+
321+
```
287322

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

example/bundle-cjs.cjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* In case `.cjs` extension is used, Node.js will recognize CJS module system
3+
* automatically.
4+
*/
5+
6+
'use strict';
7+
8+
const { readFileSync, writeFileSync } = require('fs');
9+
const bundle = require('@asyncapi/bundler');
10+
11+
async function main() {
12+
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
13+
referenceIntoComponents: true,
14+
});
15+
writeFileSync('asyncapi.yaml', document.yml());
16+
}
17+
18+
main().catch(e => console.error(e));

example/bundle-cjs.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* To use `.js` extension with CJS module system, make sure
3+
* `package.json`
4+
* DOES NOT contain line
5+
* `"type": "module",`
6+
*/
7+
8+
'use strict';
9+
10+
const { readFileSync, writeFileSync } = require('fs');
11+
const bundle = require('@asyncapi/bundler');
12+
13+
async function main() {
14+
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
15+
referenceIntoComponents: true,
16+
});
17+
writeFileSync('asyncapi.yaml', document.yml());
18+
}
19+
20+
main().catch(e => console.error(e));

example/bundle-esm.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* To use `.js` extension with ESM module system, first add to
3+
* `package.json`
4+
* line
5+
* `"type": "module",`
6+
*/
7+
8+
'use strict';
9+
10+
import { readFileSync, writeFileSync } from 'fs';
11+
import bundle from '@asyncapi/bundler';
12+
13+
async function main() {
14+
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
15+
referenceIntoComponents: true,
16+
});
17+
writeFileSync('asyncapi.yaml', document.yml());
18+
}
19+
20+
main().catch(e => console.error(e));

example/bundle-esm.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* In case `.mjs` extension is used, Node.js will recognize ESM module system
3+
* automatically.
4+
*/
5+
6+
'use strict';
7+
8+
import { readFileSync, writeFileSync } from 'fs';
9+
import bundle from '@asyncapi/bundler';
10+
11+
async function main() {
12+
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
13+
referenceIntoComponents: true,
14+
});
15+
writeFileSync('asyncapi.yaml', document.yml());
16+
}
17+
18+
main().catch(e => console.error(e));

example/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"dev": "node bundle.js"
7+
"start": "npm run start:cjs && npm run start:esm && npm run start:ts",
8+
"start:cjs": "node bundle-cjs.cjs",
9+
"start:esm": "node bundle-esm.mjs",
10+
"start:ts": "ts-node bundle.ts"
811
},
912
"keywords": [],
1013
"author": "",
1114
"license": "Apache-2.0",
1215
"dependencies": {
1316
"@asyncapi/bundler": "../",
14-
"@types/node": "^18.7.23"
17+
"@types/node": "^18.7.23",
18+
"ts-node": "^10.9.1",
19+
"typescript": "^4.8.4"
1520
}
1621
}

example/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"compilerOptions": {
33
"types": [
44
"node"
5-
]
5+
],
6+
"esModuleInterop": true,
67
}
78
}

src/index.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ import type { AsyncAPIObject } from './spec-types';
55

66
/**
77
*
8-
* @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`).
8+
* @param {string[]} files Array of stringified AsyncAPI documents in YAML
9+
* format, that are to be bundled (or array of filepaths, resolved and passed
10+
* via `Array.map()` and `fs.readFileSync`, which is the same, see `README.md`).
911
* @param {Object} [options]
10-
* @param {string | object} [options.base] Base object whose properties will be retained.
11-
* @param {boolean} [options.referenceIntoComponents] Pass `true` to resolve external references to components.
12+
* @param {string | object} [options.base] Base object whose properties will be
13+
* retained.
14+
* @param {boolean} [options.referenceIntoComponents] Pass `true` to resolve
15+
* external references to components.
1216
*
1317
* @return {Document}
1418
*
1519
* @example
1620
*
21+
* **TypeScript**
22+
* ```ts
1723
* import { readFileSync, writeFileSync } from 'fs';
1824
* import bundle from '@asyncapi/bundler';
1925
*
@@ -27,6 +33,41 @@ import type { AsyncAPIObject } from './spec-types';
2733
* }
2834
*
2935
* main().catch(e => console.error(e));
36+
* ```
37+
*
38+
* **JavaScript CJS module system**
39+
* ```js
40+
* 'use strict';
41+
*
42+
* const { readFileSync, writeFileSync } = require('fs');
43+
* const bundle = require('@asyncapi/bundler');
44+
*
45+
* async function main() {
46+
* const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
47+
* referenceIntoComponents: true,
48+
* });
49+
* writeFileSync('asyncapi.yaml', document.yml());
50+
* }
51+
*
52+
* main().catch(e => console.error(e));
53+
* ```
54+
*
55+
* **JavaScript ESM module system**
56+
* ```js
57+
* 'use strict';
58+
*
59+
* import { readFileSync, writeFileSync } from 'fs';
60+
* import bundle from '@asyncapi/bundler';
61+
*
62+
* async function main() {
63+
* const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
64+
* referenceIntoComponents: true,
65+
* });
66+
* writeFileSync('asyncapi.yaml', document.yml());
67+
* }
68+
*
69+
* main().catch(e => console.error(e));
70+
* ```
3071
*
3172
*/
3273
export default async function bundle(files: string[], options: any = {}) {
@@ -47,4 +88,6 @@ export default async function bundle(files: string[], options: any = {}) {
4788
return new Document(resolvedJsons as AsyncAPIObject[], options.base);
4889
}
4990

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

0 commit comments

Comments
 (0)