2.0.6 • Published 10 months ago

@knighted/specifier v2.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@knighted/specifier

CI codecov NPM version

Node.js tool for parsing imports to change ESM and CJS specifiers.

  • Rewrite specifier values.
  • Read metadata about a specifier's AST node.
  • Updates files or strings.
  • Parses import, import(), import.meta.resolve(), export, require, and require.resolve().

Example

Given a file with some imports and exports:

// file.ts

import { 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:

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:

import { type Spec, specifier } from '@knighted/specifier'

const nodes: { node: Spec['node']; parent: Spec['parent'] }[] = []

await specifier.update('file.ts', ({ node, parent }) => {
  nodes.push({ node, parent })
})

nodes.forEach(({ node, parent }) => {
  // Do something with the metadata
})
2.0.0-rc.2

12 months ago

2.0.0-rc.3

12 months ago

2.0.0-rc.4

12 months ago

2.0.3

11 months ago

2.0.2

11 months ago

2.0.5

11 months ago

2.0.4

11 months ago

2.0.6

10 months ago

2.0.1

12 months ago

2.0.0

12 months ago

2.0.0-rc.0

2 years ago

2.0.0-rc.1

2 years ago

1.0.1

2 years ago

1.0.0

3 years ago

1.0.0-rc.4

3 years ago

1.0.0-rc.3

3 years ago

1.0.0-rc.2

3 years ago

1.0.0-rc.1

3 years ago

1.0.0-rc.0

3 years ago

1.0.0-alpha.7

3 years ago

1.0.0-alpha.6

3 years ago

1.0.0-alpha.5

3 years ago

1.0.0-alpha.4

3 years ago

1.0.0-alpha.3

3 years ago

1.0.0-alpha.2

3 years ago

1.0.0-alpha.1

3 years ago

1.0.0-alpha.0

3 years ago