{"id":26962223,"url":"https://github.com/remcohaszing/recma-module-to-function","last_synced_at":"2026-02-20T01:32:18.409Z","repository":{"id":285799399,"uuid":"959404913","full_name":"remcohaszing/recma-module-to-function","owner":"remcohaszing","description":"A recma plugin to convert an ESTree module into a function body.","archived":false,"fork":false,"pushed_at":"2025-04-13T14:25:04.000Z","size":187,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-15T15:29:32.712Z","etag":null,"topics":["esast","estree","eval","evaluate","mdx","on-demand","ondemand","recma"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/remcohaszing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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},"funding":{"github":"remcohaszing"}},"created_at":"2025-04-02T18:27:28.000Z","updated_at":"2025-04-13T14:25:07.000Z","dependencies_parsed_at":"2025-04-02T18:51:48.999Z","dependency_job_id":"e8d6197d-3923-4604-8ad1-b6bfb3e035ec","html_url":"https://github.com/remcohaszing/recma-module-to-function","commit_stats":null,"previous_names":["remcohaszing/recma-module-to-function"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/remcohaszing/recma-module-to-function","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcohaszing%2Frecma-module-to-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcohaszing%2Frecma-module-to-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcohaszing%2Frecma-module-to-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcohaszing%2Frecma-module-to-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/remcohaszing","download_url":"https://codeload.github.com/remcohaszing/recma-module-to-function/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/remcohaszing%2Frecma-module-to-function/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29638632,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T22:32:43.237Z","status":"ssl_error","status_checked_at":"2026-02-19T22:32:38.330Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: 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":["esast","estree","eval","evaluate","mdx","on-demand","ondemand","recma"],"created_at":"2025-04-03T05:19:29.078Z","updated_at":"2026-02-20T01:32:18.378Z","avatar_url":"https://github.com/remcohaszing.png","language":"TypeScript","readme":"# recma-module-to-function\n\n[![github actions](https://github.com/remcohaszing/recma-module-to-function/actions/workflows/ci.yaml/badge.svg)](https://github.com/remcohaszing/recma-module-to-function/actions/workflows/ci.yaml)\n[![codecov](https://codecov.io/gh/remcohaszing/recma-module-to-function/branch/main/graph/badge.svg)](https://codecov.io/gh/remcohaszing/recma-module-to-function)\n[![npm version](https://img.shields.io/npm/v/recma-module-to-function)](https://www.npmjs.com/package/recma-module-to-function)\n[![npm downloads](https://img.shields.io/npm/dm/recma-module-to-function)](https://www.npmjs.com/package/recma-module-to-function)\n\nA [recma](https://github.com/mdx-js/recma) plugin to convert an ESTree module into a function body.\nIt’s compatible with [MDX](https://mdxjs.com).\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [API](#api)\n  - [`recma().use(recmaModuleToFunction, options?)`](#recmauserecmamoduletofunction-options)\n- [Examples](#examples)\n  - [Evaluate JavaScript](#evaluate-javascript)\n  - [Module map](#module-map)\n  - [Dynamic import](#dynamic-import)\n  - [CDN](#cdn)\n  - [MDX with React](#mdx-with-react)\n- [Security](#security)\n- [Compatibility](#compatibility)\n- [License](#license)\n\n## Installation\n\n```sh\nnpm install recma-module-to-function\n```\n\n## Usage\n\nYou can use this with [recma](https://github.com/mdx-js/recma) to convert a module to a function\nbody.\n\n```typescript\nimport { recma } from 'recma'\nimport recmaModuleToFunction from 'recma-module-to-function'\n\nconst source = `\nimport assert from 'node:assert/strict'\n\nexport { assert }\n`\n\nconst file = recma()\n  .use({ settings: { module: true } })\n  .use(recmaModuleToFunction)\n  .processSync(source)\n\nconsole.log(String(file))\n```\n\n## API\n\nThis module exports a single function named `recmaModuleToFunction`.\n\n### `recma().use(recmaModuleToFunction, options?)`\n\nConvert an estree module into a function body. This modifies the input AST.\n\n#### Options\n\n- `importName`: A custom name for the import. By default, `import()` expressions are used. If this\n  option is given, import expressions and import meta properties are transformed into identifiers\n  using this name. (type: `string`)\n\n## Examples\n\n### Evaluate JavaScript\n\nYou can use [recma](https://github.com/mdx-js/recma) to transform a JavaScript module, then use the\n[`AsyncFunction`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction)\nconstructor to evaluate the result. Note that imports will be resolved relative to the module that\ninvokes the `AsyncFunction` constructor.\n\n```js\nimport { recma } from 'recma'\nimport recmaModuleToFunction from 'recma-module-to-function'\n\nconst source = `\nimport os from 'node:os'\nimport recmaModuleToFunction from 'recma-module-to-function'\n\nexport const home = os.homedir()\nexport { recmaModuleToFunction }\n`\n\nconst file = recma()\n  .use({ settings: { module: true } })\n  .use(recmaModuleToFunction)\n  .processSync(source)\n\nconst AsyncFunction = (async () =\u003e {\n  // We only define this function to get its constructor.\n}).constructor\n\nconst fn = new AsyncFunction(String(file))\nconst result = await fn()\n\nconsole.log(result)\n```\n\n### Module map\n\nIt’s often desirable to explicitly define which modules may be imported. One strategy to do this is\nto define map of imports that can be imported. Then define a custom import function.\n\n```js\nimport { recma } from 'recma'\nimport recmaModuleToFunction from 'recma-module-to-function'\n\nimport * as b from './b.js'\n\nconst a = { A: 1 }\n\nconst modules = new Map([\n  ['a', a],\n  ['b', b]\n])\n\nasync function customImport(name) {\n  const module = modules.get(name)\n  if (module) {\n    return module\n  }\n\n  throw new Error(`Cannot find module '${name}'`)\n}\n\nconst importName = '_import'\nconst source = `\nimport { A } from 'a'\nimport { B } from 'b'\n\nconsole.log(A)\nconsole.log(B)\n`\n\nconst file = recma()\n  .use({ settings: { module: true } })\n  .use(recmaModuleToFunction, { importName })\n  .processSync(source)\n\nconst AsyncFunction = customImport.constructor\n\nconst fn = new AsyncFunction(importName, String(file))\nconst result = await fn()\n```\n\n### Dynamic import\n\nIf you use a bundler, it may interpret support relative dynamic imports. For example, you may have a\nfile structure like this:\n\n```\n├── lib\n│   └── eval.js\n└── modules\n    ├── a.js\n    └── b.js\n```\n\nThen `eval.js` could look something like this:\n\n```js\nimport { recma } from 'recma'\nimport recmaModuleToFunction from 'recma-module-to-function'\n\nasync function customImport(name) {\n  return import(`../modules/${name}.js`)\n}\n\nconst importName = '_import'\nconst source = `\nimport { A } from 'a'\nimport { B } from 'b'\n\nconsole.log(A)\nconsole.log(B)\n`\n\nconst file = recma()\n  .use({ settings: { module: true } })\n  .use(recmaModuleToFunction, { importName })\n  .processSync(source)\n\nconst AsyncFunction = customImport.constructor\n\nconst fn = new AsyncFunction(importName, String(file))\nconst result = await fn()\n```\n\n### CDN\n\nYou can define a custom import to resolve imports code to a CDN such as [esm.sh](https://esm.sh).\nYou can even allow import attributes to let the user decide on the CDN.\n\n```ts\nimport { recma } from 'recma'\nimport recmaModuleToFunction from 'recma-module-to-function'\n\nasync function customImport(name, options) {\n  return import(`https://esm.sh/${name}`, options)\n}\n\nconst importName = '_import'\nconst source = `\nimport confetti from 'canvas-confetti'\n\nconfetti()\n`\n\nconst file = recma()\n  .use({ settings: { module: true } })\n  .use(recmaModuleToFunction, { importName })\n  .processSync(source)\n\nconst AsyncFunction = customImport.constructor\n\nconst fn = new AsyncFunction(importName, String(file))\nconst result = await fn()\n```\n\n### MDX with React\n\nThis project is compatible with MDX. One of the main goals is to be an alternative strategy to\nimplement [MDX on demand](https://mdxjs.com/guides/mdx-on-demand/).\n\nOn the server, you can compile the MDX content with\n[`compile()`](https://mdxjs.com/packages/mdx/#compilefile-options). Then pass the compiled code to a\nclient component.\n\n```tsx\n// app/page.tsx\nimport { compileSync } from '@mdx-js/mdx'\nimport { type ReactNode } from 'react'\nimport recmaModuleToFunction from 'recma-module-to-function'\n\nimport { Eval } from '../components/Eval.tsx'\n\nconst mdx = `\nimport { Button } from 'components'\n\nHello {props.name}!\n\n\u003cButton\u003eClick me!\u003c/Button\u003e\n`\n\nconst code = compileSync(mdx, {\n  recmaPlugins: [[recmaModuleToFunction, { importName: '_import' }]]\n})\n\nexport default function Page(): ReactNode {\n  return \u003cEval code={String(code)} importName={importName} /\u003e\n}\n```\n\nIn the client component, evaluate the code with the\n[`AsyncFunction`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction)\nconstructor. Use a custom import implementation to explicitly define which modules the code is\nallowed to access.\n\n```tsx\n// components/Eval.tsx\n'use client'\n\nimport { type ComponentProps, type ReactNode, useEffect, useState } from 'react'\nimport * as runtime from 'react/jsx-runtime'\nimport { type Import } from 'recma-module-to-function'\n\n// Define some components\nfunction Button(props: ComponentProps\u003c'button'\u003e): ReactNode {\n  return \u003cbutton type=\"button\" {...props} /\u003e\n}\n\nconst modules = new Map([\n  // Define the `components` module\n  ['components', { Button }],\n  // Make sure the JSX automatic runtime can be imported.\n  ['react/jsx-runtime', runtime]\n])\n\n// A custom import implementation which allows importing modules define by our map.\nconst customImport: Import = async (name) =\u003e {\n  const module = modules.get(name)\n  if (module) {\n    return module\n  }\n\n  throw new Error(`Module not found '${name}'`)\n}\n\n// Grab the AsyncFunction constructor from any async function.\nconst AsyncFunction = customImport.constructor\n\ninterface EvalProps {\n  code: string\n  importName: string\n}\n\n// The client component which can asynchronously render code.\nexport function Eval({ code, importName }: EvalProps): ReactNode {\n  const [content, setContent] = useState\u003cReactNode\u003e()\n  const [error, setError] = useState\u003cunknown\u003e()\n\n  useEffect(() =\u003e {\n    let cancelled = false\n\n    const fn = new AsyncFunction(importName, code)\n    fn(customImport).then(\n      ({ default: MDXContent }) =\u003e {\n        if (!cancelled) {\n          setContent(\u003cMDXContent name=\"User\" /\u003e)\n          setError()\n        }\n      },\n      (err) =\u003e {\n        if (!cancelled) {\n          setContent(null)\n          setError(err)\n        }\n      }\n    )\n\n    return () =\u003e {\n      cancelled = true\n    }\n  }, [code, importName])\n\n  if (error !== undefined) {\n    throw error\n  }\n\n  return content\n}\n```\n\n## Security\n\nThis package only transforms the AST input, which is safe to use on its own. However, it was created\nwith the use case in mind to evaluate a JavaScript module. Evaluating user input is dangerous and\nshould be avoided.\n\nIf you use MDX, consider using a build tool integration such as\n[`@mdx-js/loader`](https://mdxjs.com/packages/loader/) or\n[`@mdx-js/rollup`](https://mdxjs.com/packages/rollup/) instead.\n\n## Compatibility\n\nThis project is compatible with Node.js 20 or greater.\n\n## License\n\n[MIT](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing)\n","funding_links":["https://github.com/sponsors/remcohaszing"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremcohaszing%2Frecma-module-to-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fremcohaszing%2Frecma-module-to-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fremcohaszing%2Frecma-module-to-function/lists"}