Skip to content

Commit 69b05e6

Browse files
feat: replace babel with oxc-parser. (#15)
1 parent e4a8da9 commit 69b05e6

42 files changed

Lines changed: 3906 additions & 1848 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env sh
2+
npx lint-staged

.husky/pre-push

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
npm run prettier:check
3+
npm run lint
4+
npm test

README.md

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

12-
By default `@knighted/module` transforms the one-to-one [differences between ES modules and CommonJS](https://nodejs.org/api/esm.html#differences-between-es-modules-and-commonjs), but it also accepts options that allow:
12+
> [!IMPORTANT]
13+
> 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).
1314
14-
- Converting `import`/`export` to `require`/`exports`
15-
- Extensions to be updated in relative specifiers
16-
- Write transformed source code to a filename
15+
By default `@knighted/module` transforms the one-to-one [differences between ES modules and CommonJS](https://nodejs.org/api/esm.html#differences-between-es-modules-and-commonjs). Options let you control syntax rewriting, specifier updates, and output.
1716

1817
## Requirements
1918

@@ -47,9 +46,8 @@ You can transform it to the equivalent CommonJS module
4746
import { transform } from '@knighted/module'
4847

4948
await transform('./file.js', {
50-
type: 'commonjs'
51-
moduleLoading: true,
52-
out: './file.cjs'
49+
target: 'commonjs',
50+
out: './file.cjs',
5351
})
5452
```
5553
@@ -65,7 +63,10 @@ const { realpath } = require('node:fs/promises')
6563
const detectCalledFromCli = async path => {
6664
const realPath = await realpath(path)
6765

68-
if (require('node:url').pathToFileURL(__filename).toString() === pathToFileURL(realPath).href) {
66+
if (
67+
require('node:url').pathToFileURL(__filename).toString() ===
68+
pathToFileURL(realPath).href
69+
) {
6970
console.log('invoked directly by node')
7071
}
7172
}
@@ -84,18 +85,29 @@ invoked directly by node
8485
8586
```ts
8687
type ModuleOptions = {
87-
/* What module system to convert to. */
88-
type?: 'module' | 'commonjs'
89-
/* Whether import/export and require/exports should be transformed. */
90-
modules?: boolean
91-
/* Whether to change specifier extensions to the assigned value. If omitted they are left alone. */
92-
specifier?: '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts'
93-
/* What filepath to write the transformed source to. */
88+
target: 'module' | 'commonjs'
89+
sourceType?: 'auto' | 'module' | 'commonjs'
90+
transformSyntax?: boolean
91+
liveBindings?: 'strict' | 'loose' | 'off'
92+
rewriteSpecifier?:
93+
| '.js'
94+
| '.mjs'
95+
| '.cjs'
96+
| '.ts'
97+
| '.mts'
98+
| '.cts'
99+
| ((value: string) => string | null | undefined)
100+
dirFilename?: 'inject' | 'preserve' | 'error'
101+
importMeta?: 'preserve' | 'shim' | 'error'
102+
requireSource?: 'builtin' | 'create-require'
103+
cjsDefault?: 'module-exports' | 'auto' | 'none'
104+
topLevelAwait?: 'error' | 'wrap' | 'preserve'
94105
out?: string
106+
inPlace?: boolean
95107
}
96108
```
97109
98110
## Roadmap
99111
100-
- Support option `modules`.
101112
- Remove `@knighted/specifier` and avoid double parsing.
113+
- Flesh out live-binding and top-level await handling.

eslint.config.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

oxlint.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"ignorePatterns": ["dist/**", "coverage/**", "node_modules/**"],
3+
"plugins": ["eslint", "typescript", "unicorn", "import", "oxc", "node"],
4+
"rules": {
5+
"no-console": "error",
6+
"unicorn/filename-case": ["error", { "case": "camelCase" }],
7+
"import/no-unused-modules": [
8+
"warn",
9+
{ "missingExports": true, "unusedExports": true }
10+
]
11+
},
12+
"overrides": [
13+
{
14+
"files": ["test/fixtures/**", "testing.*", "test/helpers/**"],
15+
"rules": {
16+
"no-unused-vars": "off",
17+
"no-unused-expressions": "off",
18+
"no-dupe-class-members": "off",
19+
"no-useless-rename": "off",
20+
"unicorn/no-empty-file": "off"
21+
}
22+
},
23+
{
24+
"files": ["test/fixtures/identifiers/**"],
25+
"rules": {
26+
"no-unused-vars": "off"
27+
}
28+
}
29+
]
30+
}

0 commit comments

Comments
 (0)