https://github.com/flex-development/import-regex
Import statement regex
https://github.com/flex-development/import-regex
dynamic-import ecmascript import regex static-import typescript
Last synced: 5 months ago
JSON representation
Import statement regex
- Host: GitHub
- URL: https://github.com/flex-development/import-regex
- Owner: flex-development
- License: bsd-3-clause
- Created: 2022-11-24T04:47:12.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-23T00:34:38.000Z (about 1 year ago)
- Last Synced: 2024-12-11T22:47:13.149Z (6 months ago)
- Topics: dynamic-import, ecmascript, import, regex, static-import, typescript
- Language: JavaScript
- Homepage: https://github.com/flex-development/import-regex
- Size: 3.26 MB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/funding.yml
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# import-regex
[](https://github.com/flex-development/import-regex/releases/latest)
[](https://npmjs.com/package/@flex-development/import-regex)
[](https://codecov.io/github/flex-development/import-regex)
[](https://github.com/voxpelli/badges-cjs-esm)
[](LICENSE.md)
[](https://conventionalcommits.org/)
[](https://typescriptlang.org/)
[](https://vitest.dev/)
[](https://yarnpkg.com/)`import` statement regex.
## Contents
- [What is this?](#what-is-this)
- [When should I use this?](#when-should-i-use-this)
- [Install](#install)
- [Use](#use)
- [API](#api)
- [Types](#types)
- [Related](#related)
- [Contribute](#contribute)## What is this?
This package contains regular expressions for matching [dynamic][1] and [static][2] `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][3].
```sh
yarn add @flex-development/import-regex
```From Git:
```sh
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:
```ts
import * as regexp from '@flex-development/import-regex'
import { omit } from '@flex-development/tutils'
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): 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:
```sh
[
{
'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`
- **Source**: [`src/import-dynamic.ts`](src/import-dynamic.ts)
Dynamic `import` statement regex. Ignores matches in comments.
**Requires unicode support ([flag `u`][4])**.
### `STATIC_IMPORT_REGEX`
- **Source**: [`src/import-static.ts`](src/import-static.ts)
Static `import` statement regex. Ignores matches in comments.
## Types
This package is fully typed with [TypeScript][5].
## Related
- [`export-regex`][6] — `export` statement regex
## Contribute
See [`CONTRIBUTING.md`](CONTRIBUTING.md).
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import
[3]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[4]: https://javascript.info/regexp-unicode
[5]: https://www.typescriptlang.org
[6]: https://github.com/flex-development/export-regex