unplugin-ast


Manipulate the AST to transform your code.
Installation
npm i unplugin-ast
Vite
import AST from 'unplugin-ast/vite'
export default defineConfig({
plugins: [AST()],
})
Rollup
import AST from 'unplugin-ast/rollup'
export default {
plugins: [AST()],
}
esbuild
import { build } from 'esbuild'
build({
plugins: [require('unplugin-ast/esbuild')()],
})
Webpack
module.exports = {
plugins: [require('unplugin-ast/webpack')()],
}
Configuration
The following show the default values of the configuration
AST({
include: [/\.[jt]sx?$/],
exclude: undefined,
enforce: undefined,
parserOptions: {},
transformer: [],
})
Transformers
Built-in Transformers
RemoveWrapperFunction
import { RemoveWrapperFunction } from 'unplugin-ast/transformers'
export function RemoveWrapperFunction(
functionNames: Arrayable<string>,
): Transformer<CallExpression>
Transforms:
export default defineConfig(config)
To:
export default config
RemoveNode
import { RemoveNode } from 'unplugin-ast/transformers'
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]
},
})
License
MIT License © 2022-PRESENT 三咲智子