Skip to content

Commit e7e9de2

Browse files
docs: update.
1 parent 452c955 commit e7e9de2

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ Node.js utility for transforming a JavaScript or TypeScript file from an ES modu
99
- ES module ➡️ CommonJS
1010
- CommonJS ➡️ ES module
1111

12+
Highlights
13+
14+
- Defaults to safe CommonJS output: strict live bindings, import.meta shims, and specifier preservation.
15+
- Opt into stricter/looser behaviors: live binding enforcement, import.meta.main gating, and top-level await strategies.
16+
- Can optionally rewrite relative specifiers and write transformed output to disk.
17+
1218
> [!IMPORTANT]
1319
> All parsing logic is applied under the assumption the code is in [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) which [modules run under by default](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_classic_scripts).
1420
@@ -18,9 +24,15 @@ By default `@knighted/module` transforms the one-to-one [differences between ES
1824

1925
- Node >= 20.11.0
2026

27+
## Install
28+
29+
```bash
30+
npm install @knighted/module
31+
```
32+
2133
## Example
2234

23-
Given an ES module
35+
Given an ES module:
2436

2537
**file.js**
2638

@@ -40,7 +52,7 @@ const detectCalledFromCli = async path => {
4052
detectCalledFromCli(argv[1])
4153
```
4254
43-
You can transform it to the equivalent CommonJS module
55+
Transform it to CommonJS:
4456
4557
```js
4658
import { transform } from '@knighted/module'
@@ -51,7 +63,7 @@ await transform('./file.js', {
5163
})
5264
```
5365
54-
Which produces
66+
Which produces:
5567
5668
**file.cjs**
5769
@@ -99,6 +111,7 @@ type ModuleOptions = {
99111
| ((value: string) => string | null | undefined)
100112
dirFilename?: 'inject' | 'preserve' | 'error'
101113
importMeta?: 'preserve' | 'shim' | 'error'
114+
importMetaMain?: 'shim' | 'warn' | 'error'
102115
requireSource?: 'builtin' | 'create-require'
103116
cjsDefault?: 'module-exports' | 'auto' | 'none'
104117
topLevelAwait?: 'error' | 'wrap' | 'preserve'
@@ -107,7 +120,24 @@ type ModuleOptions = {
107120
}
108121
```
109122
123+
Behavior notes (defaults in parentheses)
124+
125+
- `target` (`commonjs`): output module system.
126+
- `transformSyntax` (true): enable/disable the ESM↔CJS lowering pass.
127+
- `liveBindings` (`strict`): getter-based live bindings, or snapshot (`loose`/`off`).
128+
- `dirFilename` (`inject`): inject `__dirname`/`__filename`, preserve existing, or throw.
129+
- `importMeta` (`shim`): rewrite `import.meta.*` to CommonJS equivalents.
130+
- `importMetaMain` (`shim`): gate `import.meta.main` with shimming/warning/error when Node support is too old.
131+
- `topLevelAwait` (`error`): throw, wrap, or preserve when TLA appears in CommonJS output.
132+
- `rewriteSpecifier` (off): rewrite relative specifiers to a chosen extension or via a callback.
133+
- `requireSource` (`builtin`): whether `require` comes from Node or `createRequire`.
134+
- `cjsDefault` (`auto`): bundler-style default interop vs direct `module.exports`.
135+
- `out`/`inPlace`: write the transformed code to a file; otherwise the function returns the transformed string only.
136+
137+
See [docs/esm-to-cjs.md](docs/esm-to-cjs.md) for deeper notes on live bindings, interop helpers, top-level await behavior, and `import.meta.main` handling.
138+
110139
## Roadmap
111140
112141
- Remove `@knighted/specifier` and avoid double parsing.
113-
- Flesh out live-binding and top-level await handling.
142+
- Emit source maps and clearer diagnostics for transform choices.
143+
- Broaden fixtures covering live-binding and top-level await edge cases across Node versions.

0 commit comments

Comments
 (0)