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

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧀 Paneer

Modify code like a cheese! Using recast for AST modifications.

Usage

Install npm package:

# using yarn
yarn add --dev paneer

# using npm
npm install -D paneer

Import utilities:

// ESM / Bundler
import { parse, compile } from 'paneer'
import * as p from 'paneer'

// CommonJS
const { parse, compile } = 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 { load, write } from 'paneer'

const ast = await load('config.js')

ast.exports.default.props.foo.push('b')

await write(ast)

Updated config.js:

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

Example: Directly use AST utils:

import * as p from 'paneer'

// Parse to AST
const ast = p.parse(`export default { foo: ['a'] }`)

// Find default export
const defaultExport = p.defaultExport(ast)

// Get foo prop of object
const foo = p.get(p.defaultExport(ast), 'foo')

// Push 'b' literal to array
p.push(foo, 'b')

// Generate code
const { code, map } = p.generate(node)

License

MIT

About

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

Resources

License

Stars

Watchers

Forks