https://github.com/knightedcodemonkey/specifier
Node.js tool for parsing imports and rewriting specifiers.
https://github.com/knightedcodemonkey/specifier
cjs esm export import jsx nodejs require specifier typescript
Last synced: 2 months ago
JSON representation
Node.js tool for parsing imports and rewriting specifiers.
- Host: GitHub
- URL: https://github.com/knightedcodemonkey/specifier
- Owner: knightedcodemonkey
- License: mit
- Created: 2023-07-25T04:06:46.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-15T15:35:43.000Z (2 months ago)
- Last Synced: 2025-04-15T16:43:51.889Z (2 months ago)
- Topics: cjs, esm, export, import, jsx, nodejs, require, specifier, typescript
- Language: TypeScript
- Homepage:
- Size: 275 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [`@knighted/specifier`](https://www.npmjs.com/package/@knighted/specifier)

[](https://codecov.io/gh/knightedcodemonkey/specifier)
[](https://www.npmjs.com/package/@knighted/specifier)Node.js tool for parsing imports to change ESM and CJS [specifiers](https://nodejs.org/api/esm.html#import-specifiers).
- Rewrite specifier values.
- Updates files or strings.
- Read metadata about a specifier's [AST](https://www.npmjs.com/package/oxc-parser) node.
- Parses `import`, `import()`, `import.meta.resolve()`, `export`, `require`, and `require.resolve()`.## Example
Given a file with some imports and exports:
```ts
// file.tsimport { someLib } from 'some-package'
import { foo } from './path/to/foo.js'export { bar } from './path/to/bar.js'
```You can use `specifier` to change the values:
```ts
import { specifier } from '@knighted/specifier'const update = await specifier.update('file.ts', ({ value }) => {
if (value === 'some-package') {
return 'some-package/esm'
}return value.replace('.js', '.mjs')
})console.log(update)
/*
import { someLib } from 'some-package/esm'
import { foo } from './path/to/foo.mjs'export { bar } from './path/to/bar.mjs'
*/
```Or collect the AST nodes:
```ts
import { type Spec, specifier } from '@knighted/specifier'const nodes: { node: Spec['node']; parent: Spec['parent'] }[] = []
await specifier.update('file.ts', ({ parent, node }) => {
nodes.push({ node, parent })
})nodes.forEach(({ node, parent }) => {
// Do something with the metadata
})
```