Skip to content

转换器

¥Transformers

转换器用于转换源代码以支持约定。

¥Transformers are used to transform the source code to support conventions.

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

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

ts
// my-transformer.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.