unplugin-ast
TypeScript icon, indicating that this package has built-in type declarations

0.9.0 • Public • Published

unplugin-ast npm

Unit Test

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()],
}


esbuild
// esbuild.config.js
import { build } from 'esbuild'

build({
  plugins: [require('unplugin-ast/esbuild')()],
})


Webpack
// webpack.config.js
module.exports = {
  /* ... */
  plugins: [require('unplugin-ast/webpack')()],
}


Vue CLI
// vue.config.js
module.exports = {
  configureWebpack: {
    plugins: [require('unplugin-ast/webpack')()],
  },
}


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

import { RemoveWrapperFunction } from 'unplugin-ast/transformers'

/**
 * Removes wrapper function. e.g `defineComponent`, `defineConfig`...
 * @param functionNames - function names to remove
 */
declare const RemoveWrapperFunction: (
  functionNames: Arrayable<string>,
) => Transformer<CallExpression>

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

MIT License © 2022-PRESENT 三咲智子

Package Sidebar

Install

npm i unplugin-ast

Weekly Downloads

21,657

Version

0.9.0

License

MIT

Unpacked Size

26.8 kB

Total Files

33

Last publish

Collaborators

  • sxzz