{"_id":"@aptre-cjs/esm-cjs-lexer","name":"@aptre-cjs/esm-cjs-lexer","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@aptre-cjs/esm-cjs-lexer","description":"WASM module to parse the `module.exports` of a commonjs module.","version":"0.1.0","license":"MIT","repository":{"type":"git","url":"git+https://github.com/paralin/esm-cjs-lexer.sh.git"},"main":"esm_cjs_lexer.js","types":"esm_cjs_lexer.d.ts","_id":"@aptre-cjs/esm-cjs-lexer@0.1.0","gitHead":"ccc4b20ef87da02048e0f21df96e0a2b47e0bec9","bugs":{"url":"https://github.com/paralin/esm-cjs-lexer.sh/issues"},"homepage":"https://github.com/paralin/esm-cjs-lexer.sh#readme","_nodeVersion":"22.3.0","_npmVersion":"10.8.1","dist":{"integrity":"sha512-SMQ7gG6ZNi7Jxq5P90W74MNpzu4A767QHxS9uXawSedrhHzUylsHhAKY/O7i90JQ3v3wj3OJoW2xLDGeIClfTA==","shasum":"0a495aaf6aab4b74139ef8a7b039f9b1651f853b","tarball":"https://registry.npmjs.org/@aptre-cjs/esm-cjs-lexer/-/esm-cjs-lexer-0.1.0.tgz","fileCount":6,"unpackedSize":909612,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDFgtYPBN9h5obM3uH2v49sNrlqDIA4CQnzHPlQzeV5QgIhAJ5OpCEGFR+ewkjtESOs0MMC/H1opASxT9DKC1nUhYen"}]},"_npmUser":{"name":"paralin","email":"christian@paral.in"},"directories":{},"maintainers":[{"name":"paralin","email":"christian@paral.in"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/esm-cjs-lexer_0.1.0_1718334921284_0.026838163815246396"},"_hasShrinkwrap":false}},"time":{"created":"2024-06-14T03:15:21.131Z","0.1.0":"2024-06-14T03:15:21.468Z","modified":"2024-06-14T03:15:21.861Z"},"maintainers":[{"name":"paralin","email":"christian@paral.in"}],"description":"WASM module to parse the `module.exports` of a commonjs module.","homepage":"https://github.com/paralin/esm-cjs-lexer.sh#readme","repository":{"type":"git","url":"git+https://github.com/paralin/esm-cjs-lexer.sh.git"},"bugs":{"url":"https://github.com/paralin/esm-cjs-lexer.sh/issues"},"license":"MIT","readme":"# esm-cjs-lexer\n\n**WASM** module to parse the `module.exports` of a commonjs module, powered by [swc].\n\n[swc]: https://github.com/swc-project/swc\n\nThis is a fork of the [upstream package].\n\n[upstream package]: https://github.com/esm-dev/esm.sh/tree/main/packages/esm-cjs-lexer\n\n## Installation\n\n[esm-cjs-lexer] is a npm package and can be installed with:\n\n[esm-cjs-lexer]: https://npmjs.org/@aptre-cjs/esm-cjs-lexer\n\n```bash\nnpm i @aptre-cjs/esm-cjs-lexer\n```\n\n## Usage\n\n[esm-cjs-lexer] provides a `parse` function to parse the `module.exports` of a commonjs module. It returns an object with `exports` and `reexports` fields. For example:\n\n```js\nconst { parse } = require('esm-cjs-lexer');\n\n// named exports\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: \"c\" });\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 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 condition\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.ignore = \"ignore\";\n  }\n`);\n\n// if condition by checking `process.env.NODE_ENV`\n// reexports: ['./index.development']\nconst { reexports } = parse('index.cjs', `\n  if (process.env.NODE_ENV === \"development\") {\n    module.exports = require(\"./index.development\")\n  } else {\n    module.exports = require(\"./index.production\")\n  }\n`, { nodeEnv: 'development' });\n\n// block&IIFE\n// exports: ['foo', 'baz', '__esModule']\nconst { exports } = parse('index.cjs', `\n  (function () {\n    exports.foo = \"bar\"\n    if (true) {\n      return\n    }\n    exports.ignore = '-'\n  })();\n  {\n    exports.baz = 123\n  }\n  exports.__esModule = true\n`);\n\n// function called exports\n// exports: ['foo']\nconst { exports } = parse('index.cjs', `\n  function Fn() {\n    return { foo: \"bar\" }\n  }\n  module.exports = Fn()\n`);\n\n// annotated exports\n// exports: ['foo', 'bar']\nconst { exports } = parse('lib.cjs', `\n0 && (module.exports = {\n foo,\n bar,\n})\n`);\n\n// UMD format\n// exports: ['foo']\nconst { exports } = parse('index.cjs', `\n  (function (global, factory) {\n    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :\n    typeof define === 'function' && define.amd ? define(['exports'], factory) :\n    (factory((global.MMDParser = global.MMDParser || {})));\n  }(this, function (exports) {\n    exports.foo = \"bar\";\n  }))\n`);\n\n// function reexports\n// reexports: ['./lib()']\nconst { reexports } = parse('index.cjs', `\n  module.exports = require(\"./lib\")()\n`);\n\n// apply function exports (call mode)\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\n## Development Setup\n\nYou will need [rust](https://www.rust-lang.org/tools/install) 1.56+ and [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/).\n\n## Build\n\n```bash\nwasm-pack build --target nodejs --scope \"aptre-cjs\"\n```\n\n## Run tests\n\n```bash\ncargo test --all\n```\n\n## License\n\nMIT\n","readmeFilename":"README.md"}