Skip to content

Commit

Permalink
fix: add classTags to control whether to generate class characters #55
Browse files Browse the repository at this point in the history
  • Loading branch information
MellowCo committed May 6, 2023
1 parent 4cdb58a commit 86144d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface PresetWeappOptions extends PresetOptions {

/**
* taro 设计稿尺寸换算规则
* @default { 640: 2.34 / 2, 750: 1, 828: 1.81 / 2}
* @default '{ 640: 2.34 / 2, 750: 1, 828: 1.81 / 2}'
* @link https://taro-docs.jd.com/taro/docs/size
*/
deviceRatio?: Record<number, number>
Expand Down
24 changes: 17 additions & 7 deletions src/transformer/transformerClass/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ interface Options {
* @default [/\.[jt]sx?$/, /\.vue$/, /\.vue\?vue/]
*/
include?: FilterPattern

/**
* 是否生成 class 标签
* 会在模板中生成 <!-- class --> 标签,用于 unocss vscode 插件识别
* https://github.com/MellowCo/unocss-preset-weapp/issues/53
* @default true
*/
classTags?: boolean
}

export default function transformerClass(options: Options = {}): SourceCodeTransformer {
export default function transformerClass(options: Options = { classTags: true }): SourceCodeTransformer {
const idFilter = createFilter(
options.include || [/\.[jt]sx?$/, /\.vue$/, /\.vue\?vue/],
options.exclude || [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/],
Expand All @@ -39,13 +47,15 @@ export default function transformerClass(options: Options = {}): SourceCodeTrans
transform(code, id) {
let newCode = transformCode(code.toString(), options.transformRules)

const classNames = getClass(code.toString())
const injectStr = Array.from(new Set(classNames.map(x => x[1]).filter(x => x).flatMap(x => x.split(' ')))).join(' ')
if (options.classTags) {
const classNames = getClass(code.toString())
const injectStr = Array.from(new Set(classNames.map(x => x[1]).filter(x => x).flatMap(x => x.split(' ')))).join(' ')

if (vueFilter(id))
newCode = newCode.replace('<template>', `<template>\n<!-- ${injectStr} -->\n`)
else
newCode = `/* ${injectStr} */\n${code}`
if (vueFilter(id))
newCode = newCode.replace('<template>', `<template>\n<!-- ${injectStr} -->\n`)
else
newCode = `/* ${injectStr} */\n${code}`
}

code.overwrite(0, code.original.length, newCode)
},
Expand Down

0 comments on commit 86144d0

Please sign in to comment.