Skip to content

转换器

¥Transformers

提供统一的接口来转换源代码以支持约定。

¥Provides a unified interface to transform source code in order to support conventions.

my-transformer.ts
ts
import { createFilter } from '@rollup/pluginutils'
import { SourceCodeTransformer } from 'unocss'

export default function myTransformers(options: MyOptions = {}): SourceCodeTransformer {
  return {
    name: 'my-transformer',
    enforce: 'pre', // enforce before other transformers
    idFilter(id) {
      // only transform .tsx and .jsx files
      return id.match(/\.[tj]sx$/)
    },
    async transform(code, id, { uno }) {
      // code is a MagicString instance
      code.appendRight(0, '/* my transformer */')
    },
  }
}

你可以查看 官方转换器 以获取更多示例。

¥You can check official transformers for more examples.