@flex-development/import-regex
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

import-regex

github release npm codecov module type: esm license conventional commits typescript vitest yarn

import statement regex.

Contents

What is this?

This package contains regular expressions for matching dynamic and static import statements.

When should I use this?

Use this package when you need to match dynamic or static import statements.

Note:

  • Statements in docblock (/** */), multiline (/* */), and single-line (//) comments are ignored
  • Expressions are ECMAScript-compatible. They have not been tested with other flavors (PCRE, PCRE2, etc)

Install

This package is ESM only.

yarn add @flex-development/import-regex

From Git:

yarn add @flex-development/import-regex@flex-development/import-regex
See Git - Protocols | Yarn  for details on requesting a specific branch, commit, or tag.

Use

Suppose we have the following module:

import * as regexp from '@flex-development/import-regex'
import { omit } from 'radash'
import { dedent } from 'ts-dedent'

const code: string = dedent`
  import { defineBuildConfig, type Config } from '@flex-development/mkbuild'
  import type {
    Join,
    Nullable,
    Opaque,
    Simplify
  } from '@flex-development/tutils'
  import * as color from 'colorette'
  import consola from 'consola'
  import tsconfig from './tsconfig.json' assert { type: 'json' }

  const { readPackage } = await import('read-pkg')

  const se2 = 'side-effect-2.mjs'

  await import('./side-effect.mjs')
  await import(se2)
`

const print = (matches: IterableIterator<RegExpMatchArray>): void => {
  console.debug([...matches].map(match => omit(match, ['input'])))
}

print(code.matchAll(regexp.STATIC_IMPORT_REGEX))
print(code.matchAll(regexp.DYNAMIC_IMPORT_REGEX))

...running that yields:

[
  {
    '0': "import { defineBuildConfig, type Config } from '@flex-development/mkbuild'",
    '1': undefined,
    '2': '{ defineBuildConfig, type Config }',
    '3': '@flex-development/mkbuild',
    '4': undefined,
    index: 0,
    groups: [Object: null prototype] {
      type: undefined,
      imports: '{ defineBuildConfig, type Config }',
      specifier: '@flex-development/mkbuild',
      assertion: undefined
    }
  },
  {
    '0': 'import type {\n' +
      '  Join,\n' +
      '  Nullable,\n' +
      '  Opaque,\n' +
      '  Simplify\n' +
      "} from '@flex-development/tutils'",
    '1': 'type',
    '2': '{\n  Join,\n  Nullable,\n  Opaque,\n  Simplify\n}',
    '3': '@flex-development/tutils',
    '4': undefined,
    index: 75,
    groups: [Object: null prototype] {
      type: 'type',
      imports: '{\n  Join,\n  Nullable,\n  Opaque,\n  Simplify\n}',
      specifier: '@flex-development/tutils',
      assertion: undefined
    }
  },
  {
    '0': "import * as color from 'colorette'",
    '1': undefined,
    '2': '* as color',
    '3': 'colorette',
    '4': undefined,
    index: 164,
    groups: [Object: null prototype] {
      type: undefined,
      imports: '* as color',
      specifier: 'colorette',
      assertion: undefined
    }
  },
  {
    '0': "import consola from 'consola'",
    '1': undefined,
    '2': 'consola',
    '3': 'consola',
    '4': undefined,
    index: 199,
    groups: [Object: null prototype] {
      type: undefined,
      imports: 'consola',
      specifier: 'consola',
      assertion: undefined
    }
  },
  {
    '0': "import tsconfig from './tsconfig.json' assert { type: 'json' }",
    '1': undefined,
    '2': 'tsconfig',
    '3': './tsconfig.json',
    '4': "{ type: 'json' }",
    index: 229,
    groups: [Object: null prototype] {
      type: undefined,
      imports: 'tsconfig',
      specifier: './tsconfig.json',
      assertion: "{ type: 'json' }"
    }
  }
]
[
  {
    '0': "const { readPackage } = await import('read-pkg')",
    '1': '{ readPackage }',
    '2': "import('read-pkg')",
    '3': "'read-pkg'",
    '4': undefined,
    index: 293,
    groups: [Object: null prototype] {
      imports: '{ readPackage }',
      expression: "import('read-pkg')",
      specifier: "'read-pkg'",
      options: undefined
    }
  },
  {
    '0': "await import('./side-effect.mjs')",
    '1': undefined,
    '2': "import('./side-effect.mjs')",
    '3': "'./side-effect.mjs'",
    '4': undefined,
    index: 376,
    groups: [Object: null prototype] {
      imports: undefined,
      expression: "import('./side-effect.mjs')",
      specifier: "'./side-effect.mjs'",
      options: undefined
    }
  },
  {
    '0': 'await import(se2)',
    '1': undefined,
    '2': 'import(se2)',
    '3': 'se2',
    '4': undefined,
    index: 410,
    groups: [Object: null prototype] {
      imports: undefined,
      expression: 'import(se2)',
      specifier: 'se2',
      options: undefined
    }
  }
]

API

This package exports the identifiers DYNAMIC_IMPORT_REGEX and STATIC_IMPORT_REGEX.

There is no default export.

DYNAMIC_IMPORT_REGEX

Dynamic import statement regex. Ignores matches in comments.

Requires unicode support (flag u).

STATIC_IMPORT_REGEX

Static import statement regex. Ignores matches in comments.

Types

This package is fully typed with TypeScript.

Related

Contribute

See CONTRIBUTING.md.

Package Sidebar

Install

npm i @flex-development/import-regex

Weekly Downloads

7

Version

3.0.0

License

BSD-3-Clause

Unpacked Size

69.8 kB

Total Files

16

Last publish

Collaborators

  • unicornware