{"id":22502534,"url":"https://github.com/cyansalt/parseport","last_synced_at":"2026-02-27T23:40:14.959Z","repository":{"id":265605796,"uuid":"896321307","full_name":"CyanSalt/parseport","owner":"CyanSalt","description":"Get static value from a JavaScript or TypeScript file","archived":false,"fork":false,"pushed_at":"2026-01-15T02:52:26.000Z","size":516,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-15T08:49:07.547Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CyanSalt.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-11-30T03:45:34.000Z","updated_at":"2026-01-15T02:52:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"eca21229-6e38-48c8-9881-48e1c47f55e4","html_url":"https://github.com/CyanSalt/parseport","commit_stats":null,"previous_names":["cyansalt/parseport"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/CyanSalt/parseport","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CyanSalt%2Fparseport","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CyanSalt%2Fparseport/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CyanSalt%2Fparseport/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CyanSalt%2Fparseport/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CyanSalt","download_url":"https://codeload.github.com/CyanSalt/parseport/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CyanSalt%2Fparseport/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29919176,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-27T19:37:42.220Z","status":"ssl_error","status_checked_at":"2026-02-27T19:37:41.463Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-12-06T23:19:08.138Z","updated_at":"2026-02-27T23:40:14.943Z","avatar_url":"https://github.com/CyanSalt.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# parseport\n\n[![npm](https://img.shields.io/npm/v/parseport.svg)](https://www.npmjs.com/package/parseport)\n\nGet static value from a JavaScript or TypeScript file.\n\n```ts\n// my-module.ts\nexport const name = 'my-module'\n\nexport default function () {\n  return `My name is \"${name}\"`\n}\n```\n\n```ts\n// another-file.ts\nimport { parseport } from 'parseport'\n\nconst { value } = await parseport('./file', { meta: import.meta })\n\nvalue.name // 'my-module'\nvalue.default() // 'My name is \"my-module\"'\n```\n\n\u003e [!NOTE]\n\u003e Parseport is NOT an evaluator. Actually, it only analyzes JS/TS module exports. This means that the following code will not be evaluated correctly (instead, it evaluates to a function that always returns a fixed value):\n\u003e ```js\n\u003e export let name = 'my-module'\n\u003e\n\u003e export default function () {\n\u003e   name = 'still ' + name\n\u003e   return `My name is \"${name}\"`\n\u003e }\n\u003e ```\n\n## Usage\n\n```ts\nimport { parseport } from 'parseport'\n\nconst { value } = await parseport('./file', { meta: import.meta })\n```\n\nOr use more basic functions:\n\n```ts\nimport {\n  parseportFile,\n  parseportCode,\n  parseportNode,\n} from 'parseport'\n\nawait parseportFile('/path/to/file') // { value: ... }\n\nawait parseportCode('export default 2 + [1, 3].length as never', { lang: 'ts' }) // { value: { default: 4 } }\n\nawait parseportNode(babelNode) // { value: ... }\n```\n\n### Resolving\n\nYou can customize the module resolving process with `resolver`.\n\n```ts\nconst { value } = await parseport('./file', {\n  meta: import.meta,\n  resolver: (file, meta) =\u003e {\n    // ...\n  },\n})\n```\n\nParseport will use [`oxc-resolver`](https://github.com/oxc-project/oxc-resolver) to resolve modules by default. You can also use `nodeResolver` with Node.js `require()` machinery, but it may not work as expected when handling modules such as suffix-free.\n\n```ts\nimport { nodeResolver } from 'parseport'\n\nawait parseport('./file', {\n  meta: import.meta,\n  resolver: nodeResolver,\n})\n```\n\nIn most cases, the `meta` accepted by `resolver` is `ImportMeta`, and it will be a mock meta object about the source file when traversing deeply.\n\n`resolver` should return a target file path synchronously or asynchronously. Parseport will mark the file value as `PARSEPORT_UNKNOWN` when `resolver` throws, so you can safely use errors to handle modules you cannot resolve.\n\nIt can be roughly considered that `resolver` transforms the first argument of `parseport` into the first argument of `parseportFile`.\n\n### Loading\n\nYou can customize the file loading process through `loader`.\n\n```ts\nconst { value } = await parseport('./file', {\n  meta: import.meta,\n  loader: file =\u003e {\n    // ...\n  },\n})\n```\n\nParseport will use the `fs` module in Node.js to load file contents by default.\n\n`loader` accepts the absolute path of a file and returns the **text** content of the file synchronously or asynchronously. Similar to `resolver`, parseport will also mark the value of the file as `PARSEPORT_UNKNOWN` when `loader` throws.\n\nIt can be roughly considered that `loader` transforms the first argument of `parseportFile` into the first argument of `parseportCode`.\n\n### Parsing\n\nYou can customize the code parsing process through `parser`.\n\n```ts\nconst { value } = await parseport('./file', {\n  meta: import.meta,\n  parser: (code, file, lang) =\u003e {\n    // ...\n  },\n})\n```\n\nParseport will choose different Babel configurations based on `lang`, which will be automatically inferred from `file` when left empty.\n\n`loader` accepts the text content of the file and returns the `Program` node of the Babel AST synchronously or asynchronously. Similarly, thrown in `parser` will also result in `PARSEPORT_UNKNOWN`.\n\n`parser` also provides integration capabilities with non-JavaScript/TypeScript syntax. For example, you can combine custom parser with the built-in parser.\n\n```ts\nimport { defaultParser, parseport } from 'parseport'\n\nconst { value } = await parseport('./file', {\n  meta: import.meta,\n  parser: (code, file, lang) =\u003e {\n    if (lang === 'my-lang') {\n      const ast = parseMyLang(code)\n      return ast\n    }\n    return defaultParser(code, file, lang)\n  },\n})\n```\n\nIt can be roughly considered that `parser` transforms the first argument of `parseportCode` and the first argument of `parseportFile` into the first argument of `parseportNode`.\n\n### Traversing\n\nThere are three options that may affect the AST traversal behavior.\n\n- `deep`: When `true`, module declarations will also call `parseport` recursively to get the imported values; otherwise these values ​​will all be `PARSEPORT_UNKNOWN`.\n\n- `variables`: This object can be used to declare variables that you want to replace. This is helpful for situations where you want to handle certain values ​​in a limited runtime way. e.g:\n\n```ts\nconst { value } = await parseportCode(`\n  import { head } from 'lodash'\n\n  export default head([1, 2])\n`, {\n  meta: import.meta,\n  variables: {\n    head: array =\u003e array[0],\n  },\n})\n```\n\nFor example, `parseport` does not execute any unknown functions that not declared in `variables` and `modules` by default. If you want to get the result of some built-in function (such as `Object.assign`), you can add it to `variables`:\n\n```ts\nconst { value } = await parseportCode(`\n  const defaultValue = { bar: false }\n\n  export default Object.assign({ foo: true }, defaultValue)\n`, {\n  meta: import.meta,\n  variables: {\n    Object,\n  },\n})\n```\n\n- `modules`: Similar to `variables`, this object can be used to declare modules that you want to replace.\n\n```ts\nconst { value } = await parseportCode(`\n  import { head } from 'lodash'\n\n  export default head([1, 2])\n`, {\n  meta: import.meta,\n  modules: {\n    lodash: {\n      head: array =\u003e array[0],\n    },\n  },\n})\n```\n\nYou can also pass in a Promise or a function to load the implementation dynamically/lazily:\n\n```ts\nconst { value } = await parseportCode(`\n  import { head } from 'lodash'\n\n  export default head([1, 2])\n`, {\n  meta: import.meta,\n  modules: {\n    lodash: () =\u003e import('lodash-es'),\n  },\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyansalt%2Fparseport","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcyansalt%2Fparseport","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyansalt%2Fparseport/lists"}