Skip to content

Repository files navigation

CIcodecovNPM 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, require.resolve(), and createRequire() aliases.

Example

Given a file with some imports and exports:

// 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:

import{specifier}from'@knighted/specifier'constupdate=awaitspecifier.update('file.ts',({ value })=>{if(value==='some-package'){return'some-package/esm'}returnvalue.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{typeSpec,specifier}from'@knighted/specifier'constnodes: {node: Spec['node'];parent: Spec['parent']}[]=[]awaitspecifier.update('file.ts',({ node, parent })=>{nodes.push({ node, parent })})nodes.forEach(({ node, parent })=>{// Do something with the metadata})

About

Node.js tool for parsing imports and rewriting specifiers.

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages