{"id":19065933,"url":"https://github.com/esm-dev/cjs-module-lexer","last_synced_at":"2025-04-28T12:24:18.716Z","repository":{"id":246545440,"uuid":"821364600","full_name":"esm-dev/cjs-module-lexer","owner":"esm-dev","description":"A lexer for detecting the module.exports of a CJS module.","archived":false,"fork":false,"pushed_at":"2025-01-01T05:38:25.000Z","size":104,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-18T15:17:37.714Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/esm-dev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2024-06-28T11:27:00.000Z","updated_at":"2025-04-01T13:21:59.000Z","dependencies_parsed_at":"2024-06-28T16:18:25.363Z","dependency_job_id":"b3779feb-0c6c-4e32-bc58-450922bea442","html_url":"https://github.com/esm-dev/cjs-module-lexer","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"3bb1d05b6540adfab6f7e798ace45ec4eb2f6231"},"previous_names":["esm-dev/esm-cjs-lexer","esm-dev/cjs-module-lexer"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esm-dev%2Fcjs-module-lexer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esm-dev%2Fcjs-module-lexer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esm-dev%2Fcjs-module-lexer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esm-dev%2Fcjs-module-lexer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/esm-dev","download_url":"https://codeload.github.com/esm-dev/cjs-module-lexer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250563703,"owners_count":21450880,"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":[],"created_at":"2024-11-09T00:53:43.440Z","updated_at":"2025-04-28T12:24:18.697Z","avatar_url":"https://github.com/esm-dev.png","language":"Rust","readme":"# cjs-module-lexer\n\nA lexer for detecting the `module.exports` of a CJS module, written in Rust.\n\n## Installation\n\nYou can install cjs-module-lexer via npm CLI:\n\n```bash\nnpm i @esm.sh/cjs-module-lexer\n```\n\n## Usage\n\ncjs-module-lexer provides a `parse` function that detects the `module.exports` of a commonjs module. The function returns an object with two properties: `exports` and `reexports`. The `exports` property is an array of the exported names, and the `reexports` property is an array of the reexported modules.\n\n```js\nimport { parse } from \"@esm.sh/cjs-module-lexer\";\n\n// named exports by assignment\n// exports: [\"a\", \"b\", \"c\", \"__esModule\", \"foo\"]\nconst { exports } = parse(\"index.cjs\", `\n  exports.a = \"a\";\n  module.exports.b = \"b\";\n  Object.defineProperty(exports, \"c\", { value: 1 });\n  Object.defineProperty(module.exports, \"__esModule\", { value: true })\n  const key = \"foo\"\n  Object.defineProperty(exports, key, { value: \"e\" });\n`);\n\n// reexports\n// reexports: [\"./lib\"]\nconst { reexports } = parse(\"index.cjs\", `\n  module.exports = require(\"./lib\");\n`);\n\n// object exports(spread syntax supported)\n// exports: [\"foo\", \"baz\"]\n// reexports: [\"./lib\"]\nconst { exports, reexports } = parse(\"index.cjs\", `\n  const foo = \"bar\"\n  const obj = { baz: 123 }\n  module.exports = { foo, ...obj, ...require(\"./lib\") };\n`);\n\n// if expression\n// exports: [\"foo\", \"cjs\"]\nconst { exports } = parse(\"index.cjs\", `\n  module.exports.a = \"a\";\n  if (true) {\n    exports.foo = \"bar\";\n  }\n  const mtype = \"cjs\";\n  if (mtype === \"cjs\") {\n    exports.cjs = true;\n  } else {\n    exports.esm = true;\n  }\n  if (false) {\n    exports.unreachable = true;\n  }\n`);\n\n// condition exports by checking if `process.env.NODE_ENV` equals to `nodeEnv` option\n// reexports: [\"./index.development.js\"]\nconst { reexports } = parse(\"index.cjs\", `\n  if (process.env.NODE_ENV === \"development\") {\n    module.exports = require(\"./index.development.js\")\n  } else {\n    module.exports = require(\"./index.production.js\")\n  }\n`, { nodeEnv: \"development\" });\n\n// block\u0026IIFE\n// exports: [\"foo\", \"baz\", \"__esModule\"]\nconst { exports } = parse(\"index.cjs\", `\n  {\n    exports.foo = 'bar'\n  }\n  (function () {\n    exports.baz = 'qux'\n    if (true) {\n      return\n    }\n    exports.unreachable = true\n  })();\n  exports.__esModule = true\n`);\n\n// UMD format\n// exports: [\"foo\"]\nconst { exports } = parse(\"index.cjs\", `\n  (function (global, factory) {\n    typeof exports === \"object\" \u0026\u0026 typeof module !== \"undefined\" ? factory(exports) :\n    typeof define === \"function\" \u0026\u0026 define.amd ? define([\"exports\"], factory) :\n    (factory((global.MMDParser = global.MMDParser || {})));\n  }(this, function (exports) {\n    exports.foo = \"bar\";\n  }))\n`);\n\n// exports by calling a function\n// exports: [\"foo\"]\nconst { exports } = parse(\"index.cjs\", `\n  function module() {\n    return { foo: \"bar\" }\n  }\n  module.exports = module()\n`);\n\n// annotated export names for ESM import\n// exports: [\"foo\", \"bar\"]\nconst { exports } = parse(\"lib.cjs\", `\n0 \u0026\u0026 (module.exports = {\n foo,\n bar,\n})\n`);\n\n// call reexports\n// reexports: [\"./lib()\"]\nconst { reexports } = parse(\"index.cjs\", `\n  module.exports = require(\"./lib\")()\n`);\n// apply call reexports\n// exports: [\"foo\"]\nconst { exports } = parse(\"lib.cjs\", `\n  module.exports = function() {\n    return { foo: \"bar\" }\n  }\n`, { callMode: true });\n```\n\nThe `parse` function has the following types definition:\n\n```ts\nexport function parse(\n  specifier: string,\n  code: string,\n  options? {\n    nodeEnv?: 'development' | 'production',\n    callMode?: boolean,\n  }\n): {\n  exports: string[],\n  reexports: string[],\n};\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesm-dev%2Fcjs-module-lexer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fesm-dev%2Fcjs-module-lexer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesm-dev%2Fcjs-module-lexer/lists"}