Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

magicast

> 编程语言
Open source

Programmatically modify JavaScript and TypeScript source codes with a simplified, elegant and familiar syntax powered by recast and babel.

2.5K stars0 likes0 views
WebsiteGitHub

About

Programmatically modify JavaScript and TypeScript source codes with a simplified, elegant and familiar syntax powered by recast and babel.

Magicast

npm version npm downloads bundle Codecov License JSDocs

Programmatically modify JavaScript and TypeScript source codes with a simplified, elegant and familiar syntax. Built on top of the AST parsed by recast and babel.

❯ Magical modify a JS/TS file and write back magically just like JSON!
❯ Exports/Import manipulate module's imports and exports at ease
❯ Function Arguments easily manipulate arguments passed to a function call, like defineConfig()
❯ Smart Formatting preseves the formatting style (quotes, tabs, etc.) from the original code
❯ ‍ Readable get rid of the complexity of AST manipulation and make your code super readable

Install

Install npm package:

bash
yarn add --dev magicast

npm install -D magicast

pnpm add -D magicast

Import utilities:

javascript
import { builders, createNode, generateCode, parseModule } from "magicast";

Examples

Example: Modify a file:

config.js:

javascript
export default {
  foo: ["a"],
};

Code to modify and append b to foo prop of defaultExport:

javascript
import { loadFile, writeFile } from "magicast";

const mod = await loadFile("config.js");

mod.exports.default.foo.push("b");

await writeFile(mod, "config.js");

Updated config.js:

javascript
export default {
  foo: ["a", "b"],
};

Example: Directly use AST utils:

javascript
import { generateCode, parseModule } from "magicast";

// Parse to AST
const mod = parseModule(`export default { }`);

// Ensure foo is an array
mod.exports.default.foo ||= [];
// Add a new array member
mod.exports.default.foo.push("b");
mod.exports.default.foo.unshift("a");

// Generate code
const { code, map } = generateCode(mod);

Generated code:

javascript
export default {
  foo: ["a", "b"],
};

Example: Get the AST directly:

javascript
import { generateCode, parseModule } from "magicast";

const mod = parseModule(`export default { }`);

const ast = mod.exports.default.$ast;
// do something with ast

Example: Function arguments:

javascript
import { generateCode, parseModule } from "magicast";

const mod = parseModule(`export default defineConfig({ foo: 'bar' })`);

// Support for both bare object export and `defineConfig` wrapper
const options
  = mod.exports.default.$type === "function-call"
    ? mod.exports.default.$args[0]
    : mod.exports.default;

console.log(options.foo); // bar

Example: Create a function call:

javascript
import { builders, generateCode, parseModule } from "magicast";

const mod = parseModule(`export default {}`);

const options = (mod.exports.default.list = builders.functionCall(
  "create",
  [1, 2, 3],
));

console.log(mod.generateCode()); // export default { list: create([1, 2, 3]) }

Notes

As 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:

typescript
import { loadFile, writeFile } from "magicast";

function updateConfig() {
  try {
    const mod = await loadFile("config.js");

    mod.exports.default.foo.push("b");

    await writeFile(mod);
  }
  catch {
    console.error("Unable to update config.js");
    console.error(
      "Please update it manually with the following instructions: ...",
    );
    // handle error
  }
}

Browser / Worker Support

The main export of magicast contains filesystem utilities. If you want to use it in a browser or worker, import from magicast/core instead.

javascript
import { parseModule } from "magicast/core";

High Level Helpers

We 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.

javascript
import {
  addNuxtModule,
  addVitePlugin,
  deepMergeObject,
  // ...
} from "magicast/helpers";

We recommend to check out the source code and test cases for more details.

Development

  • Clone this repository
  • Install latest LTS version of Node.js
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Made with

Published under MIT License.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言