magicast

所属分类:Python编程
开发工具:TypeScript
文件大小:0KB
下载次数:0
上传日期:2023-06-28 08:54:14
上 传 者sh-1993
说明:  使用简化、优雅和熟悉的语法功能,以编程方式修改JavaScript和TypeScript源代码...
(?? Programmatically modify JavaScript and TypeScript source codes with a simplified, elegant and familiar syntax powered by recast and babel.)

文件列表:
.editorconfig (224, 2023-11-10)
.eslintignore (34, 2023-11-10)
.eslintrc (176, 2023-11-10)
.prettierrc (3, 2023-11-10)
CHANGELOG.md (10918, 2023-11-10)
LICENSE (1120, 2023-11-10)
build.config.ts (867, 2023-11-10)
helpers.d.ts (35, 2023-11-10)
package.json (3206, 2023-11-10)
pnpm-lock.yaml (157627, 2023-11-10)
pnpm-workspace.yaml (26, 2023-11-10)
renovate.json (49, 2023-11-10)
scripts/ (0, 2023-11-10)
scripts/vendor.ts (2970, 2023-11-10)
src/ (0, 2023-11-10)
src/babel.ts (1959, 2023-11-10)
src/builders.ts (1289, 2023-11-10)
src/code.ts (2371, 2023-11-10)
src/error.ts (1127, 2023-11-10)
src/format.ts (3515, 2023-11-10)
src/helpers.ts (33, 2023-11-10)
src/helpers/ (0, 2023-11-10)
src/helpers/config.ts (2191, 2023-11-10)
src/helpers/deep-merge.ts (416, 2023-11-10)
src/helpers/index.ts (104, 2023-11-10)
src/helpers/nuxt.ts (542, 2023-11-10)
src/helpers/vite.ts (4620, 2023-11-10)
src/index.ts (128, 2023-11-10)
src/proxy/ (0, 2023-11-10)
src/proxy/_utils.ts (3878, 2023-11-10)
src/proxy/array.ts (3343, 2023-11-10)
src/proxy/exports.ts (3387, 2023-11-10)
src/proxy/function-call.ts (1078, 2023-11-10)
src/proxy/identifier.ts (442, 2023-11-10)
src/proxy/imports.ts (5942, 2023-11-10)
... ...

# Magicast [![npm version][npm-version-src]][npm-version-href] [![npm downloads][npm-downloads-src]][npm-downloads-href] [![bundle][bundle-src]][bundle-href] [![Codecov][codecov-src]][codecov-href] [![License][license-src]][license-href] [![JSDocs][jsdocs-src]][jsdocs-href] Programmatically 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/). **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: ```sh # using yarn yarn add --dev magicast # using npm npm install -D magicast # using pnpm pnpm add -D magicast ``` Import utilities: ```js // ESM / Bundler import { parseModule, generateCode, builders, createNode } from "magicast"; // CommonJS const { parseModule, generateCode, builders, createNode } = require("magicast"); ``` ## Examples **Example:** Modify a file: `config.js`: ```js export default { foo: ["a"], }; ``` Code to modify and append `b` to `foo` prop of defaultExport: ```js 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`: ```js export default { foo: ["a", "b"], }; ``` **Example:** Directly use AST utils: ```js import { parseModule, generateCode } 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: ```js export default { foo: ["a", "b"], }; ``` **Example:** Get the AST directly: ```js import { parseModule, generateCode } from "magicast"; const mod = parseModule(`export default { }`); const ast = mod.exports.default.$ast // do something with ast ``` **Example:** Function parameters: ```js import { parseModule, generateCode } 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: ```js import { parseModule, generateCode, builders } 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: ```ts import { loadFile, writeFile } from "magicast"; function updateConfig() { try { const mod = await loadFile("config.js"); mod.exports.default.foo.push("b"); await writeFile(mod); } catch (e) { console.error('Unable to update config.js') console.error('Please update it manually with the following instructions: ...') // handle error } } ``` ## 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. ```js import { deepMergeObject, addNuxtModule, addVitePlugin, // ... } from "magicast/helpers"; ``` We recommend to check out the [source code](./src/helpers) and [test cases](./test/helpers) for more details. ## Development - Clone this repository - Install latest LTS version of [Node.js](https://nodejs.org/en/) - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` - Install dependencies using `pnpm install` - Run interactive tests using `pnpm dev` ## License Made with Published under [MIT License](./LICENSE). [npm-version-src]: https://img.shields.io/npm/v/magicast?style=flat&colorA=18181B&colorB=F0DB4F [npm-version-href]: https://npmjs.com/package/magicast [npm-downloads-src]: https://img.shields.io/npm/dm/magicast?style=flat&colorA=18181B&colorB=F0DB4F [npm-downloads-href]: https://npmjs.com/package/magicast [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/magicast/main?style=flat&colorA=18181B&colorB=F0DB4F [codecov-href]: https://codecov.io/gh/unjs/magicast [bundle-src]: https://img.shields.io/bundlephobia/minzip/magicast?style=flat&colorA=18181B&colorB=F0DB4F [bundle-href]: https://bundlephobia.com/result?p=magicast [license-src]: https://img.shields.io/github/license/unjs/magicast.svg?style=flat&colorA=18181B&colorB=F0DB4F [license-href]: https://github.com/unjs/magicast/blob/main/LICENSE [jsdocs-src]: https://img.shields.io/badge/jsDocs.io-reference-18181B?style=flat&colorA=18181B&colorB=F0DB4F [jsdocs-href]: https://www.jsdocs.io/package/magicast

近期下载者

相关文件


收藏者