{"id":13516981,"url":"https://github.com/TomerAberbach/parse-imports","last_synced_at":"2025-03-31T07:30:42.202Z","repository":{"id":49432377,"uuid":"268002725","full_name":"TomerAberbach/parse-imports","owner":"TomerAberbach","description":"⚡ A blazing fast ES module imports parser.","archived":false,"fork":false,"pushed_at":"2024-10-15T04:12:30.000Z","size":838,"stargazers_count":52,"open_issues_count":4,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-19T19:47:25.346Z","etag":null,"topics":["dynamic-import","dynamic-imports","es-module","es6","esm","identifier","import-export","imports-parser","javascript","nodejs","parse-imports","parser","statement","wasm"],"latest_commit_sha":null,"homepage":"https://npm.im/parse-imports","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TomerAberbach.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license-apache","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"TomerAberbach"}},"created_at":"2020-05-30T03:34:19.000Z","updated_at":"2024-11-03T01:12:05.000Z","dependencies_parsed_at":"2024-06-18T15:32:27.353Z","dependency_job_id":"42a1ea9d-c0ea-4cd6-beb2-20b57f1998a7","html_url":"https://github.com/TomerAberbach/parse-imports","commit_stats":{"total_commits":76,"total_committers":3,"mean_commits":"25.333333333333332","dds":0.02631578947368418,"last_synced_commit":"ecb71ffa9dfa5ab43b1a58141ca0ab5778c5ddbc"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomerAberbach%2Fparse-imports","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomerAberbach%2Fparse-imports/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomerAberbach%2Fparse-imports/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomerAberbach%2Fparse-imports/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TomerAberbach","download_url":"https://codeload.github.com/TomerAberbach/parse-imports/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245960753,"owners_count":20700781,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dynamic-import","dynamic-imports","es-module","es6","esm","identifier","import-export","imports-parser","javascript","nodejs","parse-imports","parser","statement","wasm"],"created_at":"2024-08-01T05:01:28.083Z","updated_at":"2025-03-31T07:30:42.172Z","avatar_url":"https://github.com/TomerAberbach.png","language":"TypeScript","funding_links":["https://github.com/sponsors/TomerAberbach"],"categories":["JavaScript"],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  parse-imports\n\u003c/h1\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://npmjs.org/package/parse-imports\"\u003e\n    \u003cimg src=\"https://badgen.now.sh/npm/v/parse-imports\" alt=\"version\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/TomerAberbach/parse-imports/actions\"\u003e\n    \u003cimg src=\"https://github.com/TomerAberbach/parse-imports/workflows/CI/badge.svg\" alt=\"CI\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/sponsors/TomerAberbach\"\u003e\n    \u003cimg src=\"https://img.shields.io/static/v1?label=Sponsor\u0026message=%E2%9D%A4\u0026logo=GitHub\u0026color=%23fe8e86\" alt=\"Sponsor\"\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n  A blazing fast ES module imports parser.\n\u003c/div\u003e\n\n## Features\n\n- Uses the superb WASM-based\n  [`es-module-lexer`](https://github.com/guybedford/es-module-lexer) under the\n  hood\n- Identifies module specifier types (e.g. relative file import, package import,\n  builtin import, etc.)\n- Unescapes module specifier escape sequences\n- Collects default, named, and namespace imports\n- Works with dynamic imports\n- Resolves module specifier paths via `require.resolve`\n\n## Install\n\n```sh\n$ npm i parse-imports\n```\n\n## Usage\n\n```js\nimport { parseImports } from 'parse-imports'\n\nconst code = `\n  import a from 'b'\n  import * as c from './d'\n  import { e as f, g as h, i } from '/j'\n  import k, { l as m } from 'n'\n  import o, * as p from \"./q\"\n  import r, { s as t, u } from \"/v\"\n  import fs from 'fs'\n\n  ;(async () =\u003e {\n    await import(\"w\")\n    await import(\"x\" + \"y\")\n  })()\n`\n\n// Lazily iterate over iterable of imports\nfor (const $import of await parseImports(code)) {\n  console.log($import)\n}\n\n// Or get as an array of imports\nconst imports = [...(await parseImports(code))]\n\nconsole.log(imports[0])\n//=\u003e\n// {\n//   startIndex: 3,\n//   endIndex: 20,\n//   isDynamicImport: false,\n//   moduleSpecifier: {\n//     type: 'package',\n//     startIndex: 17,\n//     endIndex: 20,\n//     isConstant: true,\n//     code: `'b'`,\n//     value: 'b',\n//     resolved: undefined\n//   },\n//   importClause: {\n//     default: 'a',\n//     named: [],\n//     namespace: undefined\n//   }\n// }\n\nconsole.log(imports[1])\n//=\u003e\n// {\n//   startIndex: 23,\n//   endIndex: 47,\n//   isDynamicImport: false,\n//   moduleSpecifier: {\n//     type: 'relative',\n//     startIndex: 42,\n//     endIndex: 47,\n//     isConstant: true,\n//     code: `'./d'`,\n//     value: './d',\n//     resolved: undefined\n//   },\n//   importClause: {\n//     default: undefined,\n//     named: [],\n//     namespace: 'c'\n//   }\n// }\n\nconsole.log(imports[5])\n//=\u003e\n// {\n//   startIndex: 153,\n//   endIndex: 186,\n//   isDynamicImport: false,\n//   moduleSpecifier: {\n//     type: 'absolute',\n//     startIndex: 182,\n//     endIndex: 186,\n//     isConstant: true,\n//     code: '\"/v\"',\n//     value: '/v',\n//     resolved: undefined\n//   },\n//   importClause: {\n//     default: 'r',\n//     named: [\n//       { specifier: 's', binding: 't' },\n//       { specifier: 'u', binding: 'u' }\n//     ],\n//     namespace: undefined\n//   }\n// }\n\nconsole.log(imports[7])\n//=\u003e\n// {\n//   startIndex: 238,\n//   endIndex: 249,\n//   isDynamicImport: true,\n//   moduleSpecifier: {\n//     type: 'package',\n//     startIndex: 245,\n//     endIndex: 248,\n//     isConstant: true,\n//     code: '\"w\"',\n//     value: 'w',\n//     resolved: undefined\n//   },\n//   importClause: undefined\n// }\n\nconsole.log(imports[8])\n//=\u003e\n// {\n//   startIndex: 260,\n//   endIndex: 277,\n//   isDynamicImport: true,\n//   moduleSpecifier: {\n//     type: 'unknown',\n//     startIndex: 267,\n//     endIndex: 276,\n//     isConstant: false,\n//     code: '\"x\" + \"y\"',\n//     value: undefined,\n//     resolved: undefined\n//   },\n//   importClause: undefined\n// }\n```\n\n## API\n\nUse `parseImports` when you're able to await a `Promise` result and\n`parseImportsSync` otherwise.\n\n\u003e [!IMPORTANT]\n\u003e\n\u003e You can only call `parseImportsSync` once the WASM has loaded. You can be sure\n\u003e this has happened by awaiting the exported `wasmLoadPromise`.\n\nSee the [type definitions](./src/index.d.ts) for details.\n\n### Types\n\n\u003c!-- eslint-skip --\u003e\n\n```ts\ntype ModuleSpecifierType =\n  | 'invalid'\n  | 'absolute'\n  | 'relative'\n  | 'builtin'\n  | 'package'\n  | 'unknown'\n\ntype Import = {\n  startIndex: number\n  endIndex: number\n  isDynamicImport: boolean\n  moduleSpecifier: {\n    type: ModuleSpecifierType\n    startIndex: number\n    endIndex: number\n    isConstant: boolean\n    code: string\n    value?: string\n    resolved?: string\n  }\n  importClause?: {\n    default?: string\n    named: string[]\n    namespace?: string\n  }\n}\n```\n\n#### `Import`\n\n`code.substring(startIndex, endIndex)` returns the full import statement or\nexpression.\n`code.substring(moduleSpecifier.startIndex, moduleSpecifier.endIndex)` returns\nthe module specifier including quotes.\n\n`moduleSpecifier.isConstant` is `true` when the import is not a dynamic import\n(`isDynamicImport` is `false`), or when the import is a dynamic import where the\nspecifier is a simple string literal (e.g. `import('fs')`, `import(\"fs\")`,\n``import(`fs`)``).\n\nIf `moduleSpecifier.isConstant` is `false`, then `moduleSpecifier.type` is\n`'unknown'`. Otherwise, it is set according to the following rules:\n\n- `'invalid'` if the module specifier is the empty string\n- `'absolute'` if the module specifier is an absolute file path\n- `'relative'` if the module specifier is a relative file path\n- `'builtin'` if the module specifier is the name of a builtin Node.js package\n- `'package'` otherwise\n\n`moduleSpecifier.code` is the module specifier as it was written in the code.\nFor non-constant dynamic imports it could be a complex expression.\n\n`moduleSpecifier.value` is `moduleSpecifier.code` without string literal quotes\nand unescaped if `moduleSpecifier.isConstant` is `true`. Otherwise, it is\n`undefined`.\n\n`moduleSpecifier.resolved` is set if the `resolveFrom` option is set and\n`moduleSpecifier.value` is not `undefined`.\n\n`importClause` is only `undefined` if `isDynamicImport` is `true`.\n\n`importClause.default` is the default import identifier or `undefined` if the\nimport statement does not have a default import.\n\n`importClause.named` is the array of objects representing the named imports of\nthe import statement. It is empty if the import statement does not have any\nnamed imports. Each object in the array has a `specifier` field set to the\nimported identifier and a `binding` field set to the identifier for accessing\nthe imported value. For example, `import { a, x as y } from 'something'` would\nhave the following array for `importClause.named`:\n`[{ specifier: 'a', binding: 'a' }, { specifier: 'x', binding: 'y' }]`.\n\n`importClause.namespace` is the namespace import identifier or `undefined` if\nthe import statement does not have a namespace import.\n\n## Contributing\n\nStars are always welcome!\n\nFor bugs and feature requests,\n[please create an issue](https://github.com/TomerAberbach/parse-imports/issues/new).\n\n## License\n\n[MIT](https://github.com/TomerAberbach/parse-imports/blob/main/license) ©\n[Tomer Aberbach](https://github.com/TomerAberbach) \\\n[Apache 2.0](https://github.com/TomerAberbach/parse-imports/blob/main/license-apache) ©\n[Google](https://github.com/TomerAberbach/parse-imports/blob/main/notice-apache)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTomerAberbach%2Fparse-imports","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTomerAberbach%2Fparse-imports","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTomerAberbach%2Fparse-imports/lists"}