Skip to content

Commit 7842ed0

Browse files
committed
Update readme
1 parent 0e45a59 commit 7842ed0

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

README.md

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
# esm-cjs-lexer
22

3-
A **WASM** module to parse the `module.exports` of a commonjs module, powered by [swc](https://github.com/swc-project/swc).
3+
A commonjs module lexer written in Rust for detecting the `module.exports` of a commonjs module. The lexer is powered by [swc](https://github.com/swc-project/swc), compiled to WebAssembly.
44

5-
## Installation
5+
## Usage
66

7-
esm-cjs-lexer only supports node.js environment currently. You can install it via `npm`:
7+
esm-cjs-lexer currently only supports Node.js environment. You can install it via npm CLI:
88

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

13-
## Usage
14-
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.
13+
esm-cjs-lexer provides a `parse` function that detects the `module.exports` of a commonjs module. 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.
1614

1715
```js
1816
const { parse } = require("esm-cjs-lexer");
1917

20-
// named exports
18+
// named exports by assignment
2119
// exports: ["a", "b", "c", "__esModule", "foo"]
2220
const { exports } = parse("index.cjs", `
2321
exports.a = "a";
2422
module.exports.b = "b";
25-
Object.defineProperty(exports, "c", { value: "c" });
23+
Object.defineProperty(exports, "c", { value: 1 });
2624
Object.defineProperty(module.exports, "__esModule", { value: true })
2725
const key = "foo"
2826
Object.defineProperty(exports, key, { value: "e" });
@@ -34,7 +32,7 @@ const { reexports } = parse("index.cjs", `
3432
module.exports = require("./lib");
3533
`);
3634

37-
// object exports(spread supported)
35+
// object exports(spread syntax supported)
3836
// exports: ["foo", "baz"]
3937
// reexports: ["./lib"]
4038
const { exports, reexports } = parse("index.cjs", `
@@ -43,7 +41,7 @@ const { exports, reexports } = parse("index.cjs", `
4341
module.exports = { foo, ...obj, ...require("./lib") };
4442
`);
4543

46-
// if condition
44+
// if expression
4745
// exports: ["foo", "cjs"]
4846
const { exports } = parse("index.cjs", `
4947
module.exports.a = "a";
@@ -57,11 +55,11 @@ const { exports } = parse("index.cjs", `
5755
exports.esm = true;
5856
}
5957
if (false) {
60-
exports.ignore = "ignore";
58+
exports.unreachable = true;
6159
}
6260
`);
6361

64-
// if condition by checking `process.env.NODE_ENV`
62+
// condition exports by checking if `process.env.NODE_ENV` equals to `nodeEnv` option
6563
// reexports: ["./index.development.js"]
6664
const { reexports } = parse("index.cjs", `
6765
if (process.env.NODE_ENV === "development") {
@@ -74,26 +72,38 @@ const { reexports } = parse("index.cjs", `
7472
// block&IIFE
7573
// exports: ["foo", "baz", "__esModule"]
7674
const { exports } = parse("index.cjs", `
75+
{
76+
exports.foo = 'bar'
77+
}
7778
(function () {
78-
exports.foo = "bar"
79+
exports.baz = 'qux'
7980
if (true) {
8081
return
8182
}
82-
exports.ignore = "-"
83+
exports.unreachable = true
8384
})();
84-
{
85-
exports.baz = 123
86-
}
8785
exports.__esModule = true
8886
`);
8987

90-
// function called exports
88+
// UMD format
89+
// exports: ["foo"]
90+
const { exports } = parse("index.cjs", `
91+
(function (global, factory) {
92+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) :
93+
typeof define === "function" && define.amd ? define(["exports"], factory) :
94+
(factory((global.MMDParser = global.MMDParser || {})));
95+
}(this, function (exports) {
96+
exports.foo = "bar";
97+
}))
98+
`);
99+
100+
// exports by calling a function
91101
// exports: ["foo"]
92102
const { exports } = parse("index.cjs", `
93-
function Fn() {
103+
function module() {
94104
return { foo: "bar" }
95105
}
96-
module.exports = Fn()
106+
module.exports = module()
97107
`);
98108

99109
// annotated export names for ESM import
@@ -105,25 +115,12 @@ const { exports } = parse("lib.cjs", `
105115
})
106116
`);
107117

108-
// UMD format
109-
// exports: ["foo"]
110-
const { exports } = parse("index.cjs", `
111-
(function (global, factory) {
112-
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) :
113-
typeof define === "function" && define.amd ? define(["exports"], factory) :
114-
(factory((global.MMDParser = global.MMDParser || {})));
115-
}(this, function (exports) {
116-
exports.foo = "bar";
117-
}))
118-
`);
119-
120-
// function reexports
118+
// call reexports
121119
// reexports: ["./lib()"]
122120
const { reexports } = parse("index.cjs", `
123121
module.exports = require("./lib")()
124122
`);
125-
126-
// apply function exports (call mode)
123+
// apply call reexports
127124
// exports: ["foo"]
128125
const { exports } = parse("lib.cjs", `
129126
module.exports = function() {

0 commit comments

Comments
 (0)