Skip to content
/ magicast Public

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

License

Notifications You must be signed in to change notification settings

unjs/magicast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧀 Paneer

npm version npm downloads Github Actions Codecov

Modify code like a cheese! Powered by recast and babel.

Usage

Install npm package:

# using yarn
yarn add --dev paneer

# using npm
npm install -D paneer

# using pnpm
pnpm add -D paneer

Import utilities:

// ESM / Bundler
import { parseCode, generateCode } from "paneer";
import * as p from "paneer";

// CommonJS
const { parseCode, generateCode } = require("panner");
const p = require("panner");

Example: Modify a file:

config.js:

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

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

import { loadFile, writeFile } from "paneer";

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

_module.exports.default.props.foo.push("b");

await writeFile(_module);

Updated config.js:

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

Example: Directly use AST utils:

import { parseCode, generateCode } from "paneer";

// Parse to AST
const _module = parseCode(`export default { foo: ['a'] }`);

// Add a new array member
_module.exports.default.props.foo.push("b");

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