Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
unplugin/unplugin-astWorks with
•JSR Score88%•This package works with Node.js, BunIt is unknown whether this package works with Cloudflare Workers, Deno, Browsers




Published3 months ago (0.16.0)
Manipulate the AST to transform your code.
unplugin-ast
Manipulate the AST to transform your code.
Installation
npm i unplugin-ast
Vite
// vite.config.ts import AST from 'unplugin-ast/vite' export default defineConfig({ plugins: [AST()], })
Rollup
// rollup.config.js import AST from 'unplugin-ast/rollup' export default { plugins: [AST()], }
Rolldown / tsdown
// rolldown.config.ts / tsdown.config.ts import AST from 'unplugin-ast/rolldown' export default { plugins: [AST()], }
esbuild
import { build } from 'esbuild' import AST from 'unplugin-ast/esbuild' build({ plugins: [AST()], })
Webpack
// webpack.config.js import AST from 'unplugin-ast/webpack' export default { /* ... */ plugins: [AST()], }
Rspack
// rspack.config.js import AST from 'unplugin-ast/rspack' export default { /* ... */ plugins: [AST()], }
Configuration
The following show the default values of the configuration
AST({ // filters for transforming targets include: [/\.[jt]sx?$/], exclude: undefined, // Rollup and esbuild do not support using enforce to control the order of plugins. Users need to maintain the order manually. enforce: undefined, // https://babeljs.io/docs/en/babel-parser#options parserOptions: {}, // Refer to Custom Transformers belows transformer: [], })
Transformers
Built-in Transformers
RemoveWrapperFunction
import { RemoveWrapperFunction } from 'unplugin-ast/transformers' /** * Removes wrapper function. e.g `defineComponent`, `defineConfig`... * @param functionNames - function names to remove * * @example defineComponent() * @example tw`text-red-500 ${expr}` */ export function RemoveWrapperFunction( functionNames: Arrayable<string>, ): Transformer<CallExpression>
Transforms:
export default defineConfig(config)
To:
export default config
RemoveNode
import { RemoveNode } from 'unplugin-ast/transformers' /** * Removes arbitrary nodes. */ export function RemoveNode( onNode: ( node: Node, parent: Node | null | undefined, index: number | null | undefined, ) => Awaitable<boolean>, ): Transformer
Custom Transformers
import type { CallExpression } from '@babel/types' import type { Transformer } from 'unplugin-ast' export const RemoveWrapperFunction = ( functionNames: string[], ): Transformer<CallExpression> => ({ onNode: (node) => node.type === 'CallExpression' && node.callee.type === 'Identifier' && functionNames.includes(node.callee.name), transform(node) { return node.arguments[0] }, })
Sponsors
License
Built and signed on
GitHub Actions
Add Package
deno add jsr:@unplugin/ast
Import symbol
import * as ast from "@unplugin/ast";
Import directly with a jsr specifier
import * as ast from "jsr:@unplugin/ast";