Manipulate the AST to transform your code.
npm install unplugin-ast[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![JSR][jsr-src]][jsr-href]
[![Unit Test][unit-test-src]][unit-test-href]
Manipulate the AST to transform your code.
``bash`
npm i unplugin-ast
Vite
`ts
// vite.config.ts
import AST from 'unplugin-ast/vite'
export default defineConfig({
plugins: [AST()],
})
`
Rollup
`ts
// rollup.config.js
import AST from 'unplugin-ast/rollup'
export default {
plugins: [AST()],
}
`
Rolldown / tsdown
`ts
// rolldown.config.ts / tsdown.config.ts
import AST from 'unplugin-ast/rolldown'
export default {
plugins: [AST()],
}
`
esbuild
`ts
import { build } from 'esbuild'
import AST from 'unplugin-ast/esbuild'
build({
plugins: [AST()],
})
`
Webpack
`js
// webpack.config.js
import AST from 'unplugin-ast/webpack'
export default {
/ ... /
plugins: [AST()],
}
`
Rspack
`ts
// rspack.config.js
import AST from 'unplugin-ast/rspack'
export default {
/ ... /
plugins: [AST()],
}
`
The following show the default values of the configuration
`ts
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: [],
})
`
#### RemoveWrapperFunction
`ts
import { RemoveWrapperFunction } from 'unplugin-ast/transformers'
/**
* Removes wrapper function. e.g defineComponent, defineConfig...text-red-500 ${expr}
* @param functionNames - function names to remove
*
* @example defineComponent()
* @example tw`
*/
export function RemoveWrapperFunction(
functionNames: Arrayable
): Transformer
Transforms:
`ts`
export default defineConfig(config)
To:
`ts`
export default config
#### RemoveNode
`ts
import { RemoveNode } from 'unplugin-ast/transformers'
/**
* Removes arbitrary nodes.
*/
export function RemoveNode(
onNode: (
node: Node,
parent: Node | null | undefined,
index: number | null | undefined,
) => Awaitable
): Transformer
`
`ts
import type { CallExpression } from '@babel/types'
import type { Transformer } from 'unplugin-ast'
export const RemoveWrapperFunction = (
functionNames: string[],
): Transformer
onNode: (node) =>
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
functionNames.includes(node.callee.name),
transform(node) {
return node.arguments[0]
},
})
``
MIT License © 2022-PRESENT 三咲智子
[npm-version-src]: https://img.shields.io/npm/v/unplugin-ast.svg
[npm-version-href]: https://npmjs.com/package/unplugin-ast
[npm-downloads-src]: https://img.shields.io/npm/dm/unplugin-ast
[npm-downloads-href]: https://www.npmcharts.com/compare/unplugin-ast?interval=30
[jsr-src]: https://jsr.io/badges/@unplugin/ast
[jsr-href]: https://jsr.io/@unplugin/ast
[unit-test-src]: https://github.com/unplugin/unplugin-ast/actions/workflows/unit-test.yml/badge.svg
[unit-test-href]: https://github.com/unplugin/unplugin-ast/actions/workflows/unit-test.yml