{"name":"parse-imports","version":"1.1.1","keywords":["esm","imports","module","parser","imports"],"author":{"url":"https://tomeraberba.ch","name":"Tomer Aberbach","email":"tomeraberbach@gmail.com"},"license":"Apache 2.0","_id":"parse-imports@1.1.1","homepage":"https://github.com/TomerAberbach/parse-imports","bugs":{"url":"https://github.com/TomerAberbach/parse-imports/issues"},"dist":{"shasum":"901bd33ba347f78160db74401f4eb46f7c97f671","tarball":"http://registry-npm.ywwl.com/parse-imports/download/parse-imports-1.1.1.tgz","fileCount":14,"integrity":"sha512-zaUWlM+jgoYfkh5+kUnRN9CTpvj6b1jzahSFYlfpSBbxbvPz4AWj49pjp9zODXZTfxHNHe88hzJubyeQjmnXPw==","signatures":[{"sig":"MEUCIDSefZZPavZHKvuE/O6AR/xgGrekbsgThQnp4fc5bSpbAiEA2x/Mxu27SVeFfyy4bjUNKWMT6z4j3I3WTJkA/kQzr1o=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":34291,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkUbKqACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoGuQ/+PNRJjIWHsEl6NfZp6YLf075r/2Z1kufkWxwCKy0aGhvnUaGX\r\nfpreWtuGzSQGE3S2hUTJapYu2lKZ/2+voMk0kZ/neb88bs6hq+wZGOb0h+Ov\r\nb51USNXReWKSYHpIq07lJvcqNZWOj31scrYUJOmknq2ICpU7EMI9sCw3aE0s\r\nUQbjC9/xplpPtIduXQwSR6KSPm4ozPGFTnm6zUVKc40nMA1y5XMxGowka7jD\r\nT76Ds+kWUMRsbb2c0dhLkgdrXjOdy+sy23uRvC58qMY1zCWXEEbK1D2fc9SK\r\nFUisgA2ihDIKxugzY1+4GiIuBQhUyb57aNYsNLPnav2I+62WgjP3Er71i+vx\r\neeb1A2kntgFT8BFZy4TMPwAJyMm78CsQO7m6GNwNAilebasSK+sLXVD5DLdl\r\nxdq66Vqa0fNDilSIVPZ/LReiUfYIH+DgR/xKQkCDzNWIrr1Fg82+K8vy+uOQ\r\n9873SSECwXNX+bS+qbYfDH1d6FaD1Ej4UQp1vs2R9j2EQ6pecA3tKzygcnEM\r\nN31/bekmpFtWpU/wQs4etu5CBMmLK2lmfQLFtYnqNHY50aNgP1ugfiinMSmL\r\np9SJ1wYfUIW5s0wYh0GOAzseWAOg9b1zHsDF6oLUbmHiR8PURahc80BUMo99\r\nD23zcz5Kefu2Pkx3o4uUGvQ9T6Hf0/nxdB8=\r\n=I/k1\r\n-----END PGP SIGNATURE-----\r\n","size":9169,"noattachment":false,"key":"/parse-imports/-/parse-imports-1.1.1.tgz"},"type":"module","_from":"file:parse-imports-1.1.1.tgz","types":"./src/index.d.ts","module":"./src/index.js","engines":{"node":">= 12.17"},"exports":"./src/index.js","scripts":{"lint":"tomer lint","test":"tomer test","format":"tomer format","typecheck":"tomer typecheck"},"_npmUser":{"name":"tomeraberbach","email":"tomeraberbach@gmail.com"},"prettier":"tomer/prettier-config","_resolved":"/private/var/folders/kn/41l_nk9568j6j0grkb3g35580000gn/T/5480acbbce9985472a0ea8ba2bf27389/parse-imports-1.1.1.tgz","_integrity":"sha512-zaUWlM+jgoYfkh5+kUnRN9CTpvj6b1jzahSFYlfpSBbxbvPz4AWj49pjp9zODXZTfxHNHe88hzJubyeQjmnXPw==","repository":{"url":"git+https://github.com/TomerAberbach/parse-imports.git","type":"git"},"_npmVersion":"9.6.4","description":"A blazing fast ES module imports parser.","directories":{},"_nodeVersion":"20.0.0","dependencies":{"slashes":"^3.0.12","es-module-lexer":"^1.2.1"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^29.5.0","tomer":"^2.0.0","eslint":"^8.39.0","prettier":"^2.8.8","typescript":"^5.0.4","@types/jest":"^29.5.1","@types/node":"^18.16.3"},"_npmOperationalInternal":{"tmp":"tmp/parse-imports_1.1.1_1683075753950_0.6302428955431993","host":"s3://npm-registry-packages"},"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":1683075754128,"maintainers":[{"name":"tomeraberbach","email":"tomeraberbach@gmail.com"}],"_cnpm_publish_time":1683075754128,"dist-tags":{"latest":"3.0.0"}}