Skip to content

Commit 847a1ec

Browse files
committed
Update readme
1 parent 5bc461d commit 847a1ec

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

README.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# esm-cjs-lexer
22

3-
A **WASM** module to parse the `module.exports` of a commonjs module for **ESM** converting, powered by [swc](https://github.com/swc-project/swc).
3+
A **WASM** module to parse the `module.exports` of a commonjs module, powered by [swc](https://github.com/swc-project/swc).
44

55
## Installation
66

7-
[esm-cjs-lexer](https://npmjs.org/esm-cjs-lexer) is a npm package and can be installed with:
7+
esm-cjs-lexer only supports node.js environment currently. You can install it via `npm`:
88

99
```bash
1010
npm i esm-cjs-lexer
1111
```
1212

1313
## Usage
1414

15-
[esm-cjs-lexer](https://npmjs.org/esm-cjs-lexer) provides a `parse` function to parse the `module.exports` of a commonjs module. It returns an object with `exports` and `reexports` fields. For example:
15+
esm-cjs-lexer provides a `parse` function that lookups the `module.exports` of a commonjs module by parsing the given code. The function returns an object with two properties: `exports` and `reexports`. The `exports` property is an array of the exported names, and the `reexports` property is an array of the reexported modules.
1616

1717
```js
18-
const { parse } = require('esm-cjs-lexer');
18+
const { parse } = require("esm-cjs-lexer");
1919

2020
// named exports
21-
// exports: ['a', 'b', 'c', '__esModule', 'foo']
22-
const { exports } = parse('index.cjs', `
21+
// exports: ["a", "b", "c", "__esModule", "foo"]
22+
const { exports } = parse("index.cjs", `
2323
exports.a = "a";
2424
module.exports.b = "b";
2525
Object.defineProperty(exports, "c", { value: "c" });
@@ -29,23 +29,23 @@ const { exports } = parse('index.cjs', `
2929
`);
3030

3131
// reexports
32-
// reexports: ['./lib']
33-
const { reexports } = parse('index.cjs', `
32+
// reexports: ["./lib"]
33+
const { reexports } = parse("index.cjs", `
3434
module.exports = require("./lib");
3535
`);
3636

3737
// object exports(spread supported)
38-
// exports: ['foo', 'baz']
39-
// reexports: ['./lib']
40-
const { exports, reexports } = parse('index.cjs', `
41-
const foo = 'bar'
38+
// exports: ["foo", "baz"]
39+
// reexports: ["./lib"]
40+
const { exports, reexports } = parse("index.cjs", `
41+
const foo = "bar"
4242
const obj = { baz: 123 }
4343
module.exports = { foo, ...obj, ...require("./lib") };
4444
`);
4545

4646
// if condition
47-
// exports: ['foo', 'cjs']
48-
const { exports } = parse('index.cjs', `
47+
// exports: ["foo", "cjs"]
48+
const { exports } = parse("index.cjs", `
4949
module.exports.a = "a";
5050
if (true) {
5151
exports.foo = "bar";
@@ -62,24 +62,24 @@ const { exports } = parse('index.cjs', `
6262
`);
6363

6464
// if condition by checking `process.env.NODE_ENV`
65-
// reexports: ['./index.development']
66-
const { reexports } = parse('index.cjs', `
65+
// reexports: ["./index.development.js"]
66+
const { reexports } = parse("index.cjs", `
6767
if (process.env.NODE_ENV === "development") {
68-
module.exports = require("./index.development")
68+
module.exports = require("./index.development.js")
6969
} else {
70-
module.exports = require("./index.production")
70+
module.exports = require("./index.production.js")
7171
}
72-
`, { nodeEnv: 'development' });
72+
`, { nodeEnv: "development" });
7373

7474
// block&IIFE
75-
// exports: ['foo', 'baz', '__esModule']
76-
const { exports } = parse('index.cjs', `
75+
// exports: ["foo", "baz", "__esModule"]
76+
const { exports } = parse("index.cjs", `
7777
(function () {
7878
exports.foo = "bar"
7979
if (true) {
8080
return
8181
}
82-
exports.ignore = '-'
82+
exports.ignore = "-"
8383
})();
8484
{
8585
exports.baz = 123
@@ -88,46 +88,46 @@ const { exports } = parse('index.cjs', `
8888
`);
8989

9090
// function called exports
91-
// exports: ['foo']
92-
const { exports } = parse('index.cjs', `
91+
// exports: ["foo"]
92+
const { exports } = parse("index.cjs", `
9393
function Fn() {
9494
return { foo: "bar" }
9595
}
9696
module.exports = Fn()
9797
`);
9898

99-
// annotated exports
100-
// exports: ['foo', 'bar']
101-
const { exports } = parse('lib.cjs', `
99+
// annotated export names for ESM import
100+
// exports: ["foo", "bar"]
101+
const { exports } = parse("lib.cjs", `
102102
0 && (module.exports = {
103103
foo,
104104
bar,
105105
})
106106
`);
107107

108108
// UMD format
109-
// exports: ['foo']
110-
const { exports } = parse('index.cjs', `
109+
// exports: ["foo"]
110+
const { exports } = parse("index.cjs", `
111111
(function (global, factory) {
112-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
113-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
112+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) :
113+
typeof define === "function" && define.amd ? define(["exports"], factory) :
114114
(factory((global.MMDParser = global.MMDParser || {})));
115115
}(this, function (exports) {
116116
exports.foo = "bar";
117117
}))
118118
`);
119119

120120
// function reexports
121-
// reexports: ['./lib()']
122-
const { reexports } = parse('index.cjs', `
121+
// reexports: ["./lib()"]
122+
const { reexports } = parse("index.cjs", `
123123
module.exports = require("./lib")()
124124
`);
125125

126126
// apply function exports (call mode)
127-
// exports: ['foo']
128-
const { exports } = parse('lib.cjs', `
127+
// exports: ["foo"]
128+
const { exports } = parse("lib.cjs", `
129129
module.exports = function() {
130-
return { foo: 'bar' }
130+
return { foo: "bar" }
131131
}
132132
`, { callMode: true });
133133
```

0 commit comments

Comments
 (0)