{"name":"parse-imports","version":"2.1.0","author":{"name":"Tomer Aberbach","email":"tomeraberbach@gmail.com","url":"https://tomeraberba.ch"},"description":"A blazing fast ES module imports parser.","keywords":["esm","imports","module","parser","imports"],"homepage":"https://github.com/TomerAberbach/parse-imports","repository":{"type":"git","url":"git+https://github.com/TomerAberbach/parse-imports.git"},"bugs":{"url":"https://github.com/TomerAberbach/parse-imports/issues"},"license":"Apache 2.0","type":"module","engines":{"node":">= 18"},"exports":{"types":"./dist/index.d.ts","require":"./dist/index.cjs","import":"./dist/index.js"},"types":"./dist/index.d.ts","main":"./dist/index.cjs","module":"./dist/index.js","prettier":"tomer/prettier-config","dependencies":{"es-module-lexer":"^1.5.3","slashes":"^3.0.12"},"devDependencies":{"@types/jest":"^29.5.12","@types/node":"^20.14.8","eslint":"^8.57.0","jest":"^29.7.0","prettier":"^3.3.2","tomer":"^3.2.4","typescript":"^5.5.2"},"scripts":{"format":"tomer format","lint":"tomer lint","typecheck":"tomer typecheck","test":"tomer test","build":"tomer build","clean":"tomer clean dist"},"_id":"parse-imports@2.1.0","_integrity":"sha512-JQWgmK2o4w8leUkZeZPatWdAny6vXGU/3siIUvMF6J2rDCud9aTt8h/px9oZJ6U3EcfhngBJ635uPFI0q0VAeA==","_resolved":"/private/var/folders/kn/41l_nk9568j6j0grkb3g35580000gn/T/e19ecd01f7f3d2e0f097af15b5510897/parse-imports-2.1.0.tgz","_from":"file:parse-imports-2.1.0.tgz","_nodeVersion":"22.1.0","_npmVersion":"10.7.0","dist":{"integrity":"sha512-JQWgmK2o4w8leUkZeZPatWdAny6vXGU/3siIUvMF6J2rDCud9aTt8h/px9oZJ6U3EcfhngBJ635uPFI0q0VAeA==","shasum":"22a152f4503da34e8fb89c902383895f59e93d2d","tarball":"http://registry-npm.ywwl.com/parse-imports/download/parse-imports-2.1.0.tgz","fileCount":6,"unpackedSize":50999,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDoQDdfD1VOFTlIv2mR2ntQu/dy+cBWlScQ7Ys/K6kX8AiA+Z+h49mxCCJxm+wmJIjLqA95Ch3vuch/zkNNY1NKxeA=="}],"size":10050,"noattachment":false,"key":"/parse-imports/-/parse-imports-2.1.0.tgz"},"_npmUser":{"name":"tomeraberbach","email":"tomeraberbach@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/parse-imports_2.1.0_1719188943744_0.9520764883166315"},"_hasShrinkwrap":false,"readme":"<h1 align=\"center\">\n  parse-imports\n</h1>\n\n<div align=\"center\">\n  <a href=\"https://npmjs.org/package/parse-imports\">\n    <img src=\"https://badgen.now.sh/npm/v/parse-imports\" alt=\"version\" />\n  </a>\n  <a href=\"https://github.com/TomerAberbach/parse-imports/actions\">\n    <img src=\"https://github.com/TomerAberbach/parse-imports/workflows/CI/badge.svg\" alt=\"CI\" />\n  </a>\n  <a href=\"https://github.com/sponsors/TomerAberbach\">\n    <img src=\"https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86\" alt=\"Sponsor\">\n  </a>\n</div>\n\n<div align=\"center\">\n  A blazing fast ES module imports parser.\n</div>\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 () => {\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//=>\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//=>\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//=>\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//=>\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//=>\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> [!IMPORTANT]\n>\n> You can only call `parseImportsSync` once the WASM has loaded. You can be sure\n> this has happened by awaiting the exported `wasmLoadPromise`.\n\nSee the [type definitions](./src/index.d.ts) for details.\n\n### Types\n\n<!-- eslint-skip -->\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\nFor pull requests, please read the\n[contributing guidelines](https://github.com/TomerAberbach/parse-imports/blob/main/contributing.md).\n\n## License\n\n[Apache 2.0](https://github.com/TomerAberbach/parse-imports/blob/main/license)\n\nThis is not an official Google product.\n","publish_time":1719188943934,"maintainers":[{"name":"tomeraberbach","email":"tomeraberbach@gmail.com"}],"_cnpm_publish_time":1719188943934,"dist-tags":{"latest":"3.0.0"}}