### Magicast Development Environment Setup Source: https://github.com/unjs/magicast/blob/main/README.md Commands for setting up the Magicast development environment, including enabling Corepack, installing dependencies, and running interactive tests. ```sh corepack enable ``` ```sh pnpm install ``` ```sh pnpm dev ``` -------------------------------- ### Install Magicast Package Source: https://github.com/unjs/magicast/blob/main/README.md Instructions for installing the Magicast npm package using popular package managers like Yarn, npm, and pnpm. ```sh # using yarn yarn add --dev magicast ``` ```sh # using npm npm install -D magicast ``` ```sh # using pnpm pnpm add -D magicast ``` -------------------------------- ### Import Magicast Utilities Source: https://github.com/unjs/magicast/blob/main/README.md Examples demonstrating how to import core Magicast utilities such as `parseModule`, `generateCode`, `builders`, and `createNode` for both ESM/Bundler and CommonJS environments. ```js // ESM / Bundler import { parseModule, generateCode, builders, createNode } from "magicast"; ``` ```js // CommonJS const { parseModule, generateCode, builders, createNode } = require("magicast"); ``` -------------------------------- ### Robust Error Handling with Magicast Source: https://github.com/unjs/magicast/blob/main/README.md Provides an example of wrapping Magicast operations in a `try/catch` block to gracefully handle potential errors during file loading or modification, offering user guidance in case of failure. ```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 { console.error("Unable to update config.js"); console.error( "Please update it manually with the following instructions: ..." ); // handle error } } ``` -------------------------------- ### Importing Magicast High-Level Helpers Source: https://github.com/unjs/magicast/blob/main/README.md Shows how to import specialized helper functions from `magicast/helpers` for common tasks like deep merging objects or adding framework-specific plugins (e.g., Nuxt, Vite). ```js import { deepMergeObject, addNuxtModule, addVitePlugin, // ... } from "magicast/helpers"; ``` -------------------------------- ### Manipulating Function Arguments with Magicast Source: https://github.com/unjs/magicast/blob/main/README.md Demonstrates how to extract and access arguments from a function call (e.g., `defineConfig`) or a bare object export, allowing programmatic interaction with configuration options passed as arguments. ```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 ``` -------------------------------- ### Direct AST Manipulation with Magicast Source: https://github.com/unjs/magicast/blob/main/README.md Illustrates how to parse a module string into an AST, directly manipulate properties like ensuring an array exists and adding/prepending elements, and then generate the modified code back from the AST. ```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); ``` -------------------------------- ### Creating Function Calls with Magicast Builders Source: https://github.com/unjs/magicast/blob/main/README.md Illustrates how to programmatically create a new function call expression using `builders.functionCall` and assign it to a property within the module's default export, then generate the updated code. ```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]) } ``` -------------------------------- ### Initial config.js File Content Source: https://github.com/unjs/magicast/blob/main/README.md The initial content of a `config.js` file, showcasing a default export with a simple object containing an array property. ```js export default { foo: ["a"] }; ``` -------------------------------- ### Accessing Raw AST with Magicast Source: https://github.com/unjs/magicast/blob/main/README.md Shows how to parse a module and directly access the underlying raw AST object (`$ast`) of a node for advanced manipulation or inspection using external AST tools. ```js import { parseModule, generateCode } from "magicast"; const mod = parseModule(`export default { }`); const ast = mod.exports.default.$ast; // do something with ast ``` -------------------------------- ### Modify JavaScript File with Magicast Source: https://github.com/unjs/magicast/blob/main/README.md Demonstrates how to load an existing JavaScript file, modify its exported default object by pushing a new element into an array property, and then write the changes back to the file using `loadFile` and `writeFile`. ```js import { loadFile, writeFile } from "magicast"; const mod = await loadFile("config.js"); mod.exports.default.foo.push("b"); await writeFile(mod, "config.js"); ``` -------------------------------- ### Generated Code from AST Manipulation Source: https://github.com/unjs/magicast/blob/main/README.md The resulting JavaScript code after applying AST manipulations to add and prepend elements to the 'foo' array within the default export. ```js export default { foo: ["a", "b"] }; ``` -------------------------------- ### Updated config.js File Content Source: https://github.com/unjs/magicast/blob/main/README.md The content of the `config.js` file after being modified by Magicast, showing the 'b' element successfully appended to the 'foo' array. ```js export default { foo: ["a", "b"] }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.