Modify code like a cheese! Powered by recast and babel.
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);