Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env sh
npx lint-staged
4 changes: 4 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
npm run prettier:check
npm run lint
npm test
44 changes: 28 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ Node.js utility for transforming a JavaScript or TypeScript file from an ES modu
- ES module ➡️ CommonJS
- CommonJS ➡️ ES module

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:
> [!IMPORTANT]
> 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).

- Converting `import`/`export` to `require`/`exports`
- Extensions to be updated in relative specifiers
- Write transformed source code to a filename
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.

## Requirements

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

await transform('./file.js', {
type: 'commonjs'
moduleLoading: true,
out: './file.cjs'
target: 'commonjs',
out: './file.cjs',
})
```

Expand All @@ -65,7 +63,10 @@ const { realpath } = require('node:fs/promises')
const detectCalledFromCli = async path => {
const realPath = await realpath(path)

if (require('node:url').pathToFileURL(__filename).toString() === pathToFileURL(realPath).href) {
if (
require('node:url').pathToFileURL(__filename).toString() ===
pathToFileURL(realPath).href
) {
console.log('invoked directly by node')
}
}
Expand All @@ -84,18 +85,29 @@ invoked directly by node

```ts
type ModuleOptions = {
/* What module system to convert to. */
type?: 'module' | 'commonjs'
/* Whether import/export and require/exports should be transformed. */
modules?: boolean
/* Whether to change specifier extensions to the assigned value. If omitted they are left alone. */
specifier?: '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts'
/* What filepath to write the transformed source to. */
target: 'module' | 'commonjs'
sourceType?: 'auto' | 'module' | 'commonjs'
transformSyntax?: boolean
liveBindings?: 'strict' | 'loose' | 'off'
rewriteSpecifier?:
| '.js'
| '.mjs'
| '.cjs'
| '.ts'
| '.mts'
| '.cts'
| ((value: string) => string | null | undefined)
dirFilename?: 'inject' | 'preserve' | 'error'
importMeta?: 'preserve' | 'shim' | 'error'
requireSource?: 'builtin' | 'create-require'
cjsDefault?: 'module-exports' | 'auto' | 'none'
topLevelAwait?: 'error' | 'wrap' | 'preserve'
out?: string
inPlace?: boolean
}
```

## Roadmap

- Support option `modules`.
- Remove `@knighted/specifier` and avoid double parsing.
- Flesh out live-binding and top-level await handling.
41 changes: 0 additions & 41 deletions eslint.config.js

This file was deleted.

30 changes: 30 additions & 0 deletions oxlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"ignorePatterns": ["dist/**", "coverage/**", "node_modules/**"],
"plugins": ["eslint", "typescript", "unicorn", "import", "oxc", "node"],
"rules": {
"no-console": "error",
"unicorn/filename-case": ["error", { "case": "camelCase" }],
"import/no-unused-modules": [
"warn",
{ "missingExports": true, "unusedExports": true }
]
},
"overrides": [
{
"files": ["test/fixtures/**", "testing.*", "test/helpers/**"],
"rules": {
"no-unused-vars": "off",
"no-unused-expressions": "off",
"no-dupe-class-members": "off",
"no-useless-rename": "off",
"unicorn/no-empty-file": "off"
}
},
{
"files": ["test/fixtures/identifiers/**"],
"rules": {
"no-unused-vars": "off"
}
}
]
}
Loading