{"id":13495507,"url":"https://github.com/unjs/magicast","last_synced_at":"2025-05-08T22:22:32.908Z","repository":{"id":151141500,"uuid":"370689096","full_name":"unjs/magicast","owner":"unjs","description":"🧀  Programmatically modify JavaScript and TypeScript source codes with a simplified, elegant and familiar syntax powered by recast and babel.","archived":false,"fork":false,"pushed_at":"2025-05-07T18:51:57.000Z","size":660,"stargazers_count":2359,"open_issues_count":27,"forks_count":39,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-05-07T19:45:11.921Z","etag":null,"topics":[],"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/unjs.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":"2021-05-25T12:43:09.000Z","updated_at":"2025-05-05T16:20:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"44a1a4b4-1a10-44ae-88af-e6ad50a69a80","html_url":"https://github.com/unjs/magicast","commit_stats":{"total_commits":158,"total_committers":22,"mean_commits":7.181818181818182,"dds":0.5379746835443038,"last_synced_commit":"50e2207842672e2c1c75898f0b1b97909f3b6c92"},"previous_names":["unjs/paneer"],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unjs%2Fmagicast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unjs%2Fmagicast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unjs%2Fmagicast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unjs%2Fmagicast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/unjs","download_url":"https://codeload.github.com/unjs/magicast/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252962420,"owners_count":21832296,"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-07-31T19:01:35.398Z","updated_at":"2025-05-07T22:20:00.510Z","avatar_url":"https://github.com/unjs.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","others"],"sub_categories":[],"readme":"# 🧀 Magicast\n\n[![npm version][npm-version-src]][npm-version-href]\n[![npm downloads][npm-downloads-src]][npm-downloads-href]\n[![bundle][bundle-src]][bundle-href]\n[![Codecov][codecov-src]][codecov-href]\n[![License][license-src]][license-href]\n[![JSDocs][jsdocs-src]][jsdocs-href]\n\nProgrammatically modify JavaScript and TypeScript source codes with a simplified, elegant and familiar syntax. Built on top of the [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) parsed by [recast](https://github.com/benjamn/recast) and [babel](https://babeljs.io/).\n\n❯ 🧙🏼 **Magical** modify a JS/TS file and write back magically just like JSON!\u003cbr\u003e\n❯ 🔀 **Exports/Import** manipulate module's imports and exports at ease\u003cbr\u003e\n❯ 💼 **Function Arguments** easily manipulate arguments passed to a function call, like `defineConfig()`\u003cbr\u003e\n❯ 🎨 **Smart Formatting** preseves the formatting style (quotes, tabs, etc.) from the original code\u003cbr\u003e\n❯ 🧑‍💻 **Readable** get rid of the complexity of AST manipulation and make your code super readable\u003cbr\u003e\n\n## Install\n\nInstall npm package:\n\n```sh\n# using yarn\nyarn add --dev magicast\n\n# using npm\nnpm install -D magicast\n\n# using pnpm\npnpm add -D magicast\n```\n\nImport utilities:\n\n```js\n// ESM / Bundler\nimport { parseModule, generateCode, builders, createNode } from \"magicast\";\n\n// CommonJS\nconst { parseModule, generateCode, builders, createNode } = require(\"magicast\");\n```\n\n## Examples\n\n**Example:** Modify a file:\n\n`config.js`:\n\n```js\nexport default {\n  foo: [\"a\"],\n};\n```\n\nCode to modify and append `b` to `foo` prop of defaultExport:\n\n```js\nimport { loadFile, writeFile } from \"magicast\";\n\nconst mod = await loadFile(\"config.js\");\n\nmod.exports.default.foo.push(\"b\");\n\nawait writeFile(mod, \"config.js\");\n```\n\nUpdated `config.js`:\n\n```js\nexport default {\n  foo: [\"a\", \"b\"],\n};\n```\n\n**Example:** Directly use AST utils:\n\n```js\nimport { parseModule, generateCode } from \"magicast\";\n\n// Parse to AST\nconst mod = parseModule(`export default { }`);\n\n// Ensure foo is an array\nmod.exports.default.foo ||= [];\n// Add a new array member\nmod.exports.default.foo.push(\"b\");\nmod.exports.default.foo.unshift(\"a\");\n\n// Generate code\nconst { code, map } = generateCode(mod);\n```\n\nGenerated code:\n\n```js\nexport default {\n  foo: [\"a\", \"b\"],\n};\n```\n\n**Example:** Get the AST directly:\n\n```js\nimport { parseModule, generateCode } from \"magicast\";\n\nconst mod = parseModule(`export default { }`);\n\nconst ast = mod.exports.default.$ast;\n// do something with ast\n```\n\n**Example:** Function arguments:\n\n```js\nimport { parseModule, generateCode } from \"magicast\";\n\nconst mod = parseModule(`export default defineConfig({ foo: 'bar' })`);\n\n// Support for both bare object export and `defineConfig` wrapper\nconst options =\n  mod.exports.default.$type === \"function-call\"\n    ? mod.exports.default.$args[0]\n    : mod.exports.default;\n\nconsole.log(options.foo); // bar\n```\n\n**Example:** Create a function call:\n\n```js\nimport { parseModule, generateCode, builders } from \"magicast\";\n\nconst mod = parseModule(`export default {}`);\n\nconst options = (mod.exports.default.list = builders.functionCall(\n  \"create\",\n  [1, 2, 3],\n));\n\nconsole.log(mod.generateCode()); // export default { list: create([1, 2, 3]) }\n```\n\n## Notes\n\nAs JavaScript is a very dynamic language, you should be aware that Magicast's convention **CAN NOT cover all possible cases**. Magicast serves as a simple and maintainable interface to update static-ish JavaScript code. When interacting with Magicast node, be aware that every option might have chance to throw an error depending on the input code. We recommend to always wrap the code in a `try/catch` block (even better to do some defensive coding), for example:\n\n```ts\nimport { loadFile, writeFile } from \"magicast\";\n\nfunction updateConfig() {\n  try {\n    const mod = await loadFile(\"config.js\");\n\n    mod.exports.default.foo.push(\"b\");\n\n    await writeFile(mod);\n  } catch {\n    console.error(\"Unable to update config.js\");\n    console.error(\n      \"Please update it manually with the following instructions: ...\",\n    );\n    // handle error\n  }\n}\n```\n\n## High Level Helpers\n\nWe also experiment to provide a few high level helpers to make common tasks easier. You could import them from `magicast/helpers`. They might be moved to a separate package in the future.\n\n```js\nimport {\n  deepMergeObject,\n  addNuxtModule,\n  addVitePlugin,\n  // ...\n} from \"magicast/helpers\";\n```\n\nWe recommend to check out the [source code](./src/helpers) and [test cases](./test/helpers) for more details.\n\n## Development\n\n- Clone this repository\n- Install latest LTS version of [Node.js](https://nodejs.org/en/)\n- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`\n- Install dependencies using `pnpm install`\n- Run interactive tests using `pnpm dev`\n\n## License\n\nMade with 💛\n\nPublished under [MIT License](./LICENSE).\n\n\u003c!-- Badges --\u003e\n\n[npm-version-src]: https://img.shields.io/npm/v/magicast?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[npm-version-href]: https://npmjs.com/package/magicast\n[npm-downloads-src]: https://img.shields.io/npm/dm/magicast?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[npm-downloads-href]: https://npmjs.com/package/magicast\n[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/magicast/main?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[codecov-href]: https://codecov.io/gh/unjs/magicast\n[bundle-src]: https://img.shields.io/bundlephobia/minzip/magicast?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[bundle-href]: https://bundlephobia.com/result?p=magicast\n[license-src]: https://img.shields.io/github/license/unjs/magicast.svg?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[license-href]: https://github.com/unjs/magicast/blob/main/LICENSE\n[jsdocs-src]: https://img.shields.io/badge/jsDocs.io-reference-18181B?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[jsdocs-href]: https://www.jsdocs.io/package/magicast\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funjs%2Fmagicast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funjs%2Fmagicast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funjs%2Fmagicast/lists"}