0.10.0 • Published 6 days ago

unplugin-ast v0.10.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 days ago

unplugin-ast npm

Unit Test

Manipulate the AST to transform your code.

Installation

npm i unplugin-ast
// vite.config.ts
import AST from 'unplugin-ast/vite'

export default defineConfig({
  plugins: [AST()],
})

// rollup.config.js
import AST from 'unplugin-ast/rollup'

export default {
  plugins: [AST()],
}

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

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

// webpack.config.js
module.exports = {
  /* ... */
  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

RemoveWrapperFunction

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>

Transforms:

export default defineConfig(config)

To:

export default config

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 三咲智子

0.10.0

6 days ago

0.9.0

2 months ago

0.8.1

10 months ago

0.8.0

11 months ago

0.8.2

10 months ago

0.7.0

1 year ago

0.5.8

1 year ago

0.5.7

1 year ago

0.5.6

1 year ago

0.6.0

1 year ago

0.5.5

2 years ago

0.5.4

2 years ago

0.5.3

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago