Skip to content

Commit

Permalink
feat: add postprocess transformSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
MellowCo committed Jul 11, 2022
1 parent a6bff17 commit 853028e
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 992 deletions.
34 changes: 1 addition & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ yarn add -D unocss @unocss/webpack unplugin-transform-wx-class unocss-preset-wxa
const UnoCSS = require('unocss/webpack').default
const presetWxapp = require('unocss-preset-wxapp').default
const transformWxClass = require('unplugin-transform-wx-class/webpack')
const transformSelector = require('unplugin-transform-wx-class/transformSelector')

module.exports = {
configureWebpack: {
Expand All @@ -122,10 +121,6 @@ module.exports = {
'center': 'flex justify-center items-center',
},
],
postprocess: (css) => {
css.selector = transformSelector(css.selector)
return css
},
}),
transformWxClass(),
],
Expand Down Expand Up @@ -168,7 +163,6 @@ yarn add -D unocss @unocss/webpack unplugin-transform-wx-class unocss-preset-wxa
const UnoCSS = require('unocss/webpack').default
const presetWxapp = require('unocss-preset-wxapp').default
const transformWxClass = require('unplugin-transform-wx-class/webpack')
const transformSelector = require('unplugin-transform-wx-class/transformSelector')

const config = {
mini: {
Expand All @@ -185,10 +179,6 @@ const config = {
'center': 'flex justify-center items-center',
},
],
postprocess: (css) => {
css.selector = transformSelector(css.selector)
return css
},
}))
chain
.plugin('transformWxClass')
Expand All @@ -209,10 +199,6 @@ const config = {
'center': 'flex justify-center items-center',
},
],
postprocess: (css) => {
css.selector = transformSelector(css.selector)
return css
},
}))
chain
.plugin('transformWxClass')
Expand Down Expand Up @@ -256,7 +242,6 @@ yarn add -D unocss @unocss/webpack unplugin-transform-wx-class unocss-preset-wxa
const UnoCSS = require('unocss/webpack').default
const presetWxapp = require('unocss-preset-wxapp').default
const transformWxClass = require('unplugin-transform-wx-class/webpack')
const transformSelector = require('unplugin-transform-wx-class/transformSelector')

const config = {
mini: {
Expand All @@ -273,10 +258,6 @@ const config = {
'center': 'flex justify-center items-center',
},
],
postprocess: (css) => {
css.selector = transformSelector(css.selector)
return css
},
}))
chain
.plugin('transformWxClass')
Expand All @@ -297,10 +278,6 @@ const config = {
'center': 'flex justify-center items-center',
},
],
postprocess: (css) => {
css.selector = transformSelector(css.selector)
return css
},
}))
chain
.plugin('transformWxClass')
Expand Down Expand Up @@ -343,7 +320,6 @@ yarn add -D unocss @unocss/webpack unplugin-transform-wx-class unocss-preset-wxa
const UnoCSS = require('unocss/webpack').default
const presetWxapp = require('unocss-preset-wxapp').default
const transformWxClass = require('unplugin-transform-wx-class/webpack')
const transformSelector = require('unplugin-transform-wx-class/transformSelector')

const config = {
mini: {
Expand All @@ -360,10 +336,6 @@ const config = {
'center': 'flex justify-center items-center',
},
],
postprocess: (css) => {
css.selector = transformSelector(css.selector)
return css
},
}))
chain
.plugin('transformWxClass')
Expand Down Expand Up @@ -436,7 +408,7 @@ import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import Unocss from 'unocss/vite'
import presetWxapp from 'unocss-preset-wxapp'
import { transformSelector, transformWxClass } from 'unplugin-transform-wx-class/vite'
import transformWxClass from 'unplugin-transform-wx-class/vite'

export default defineConfig({
plugins: [
Expand All @@ -451,10 +423,6 @@ export default defineConfig({
'center': 'flex justify-center items-center',
},
],
postprocess: (css) => {
css.selector = transformSelector(css.selector)
return css
},
}),
transformWxClass(),
],
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@
"test": "vitest",
"uniapp3": "npm -C examples/uniapp_vue3 run dev:mp-weixin",
"uniapp2": "nr build &&npm -C examples/uniapp_vue2 run dev:mp-weixin",
"taro": "nr build &&npm -C examples/taro_react run dev:weapp",
"taro_vue2": "nr build &&npm -C examples/taro_vue2 run dev:weapp",
"taro_vue3": "nr build &&npm -C examples/taro_vue3 run dev:weapp",
"uniapp3:h5": "npm run build && npm -C examples/uniapp_vue3 run dev:h5",
"prepublishOnly": "nr build",
"release": "bumpp && npm publish"
"release": "bumpp"
},
"devDependencies": {
"@antfu/eslint-config": "^0.25.2",
Expand All @@ -78,7 +81,7 @@
"typescript": "^4.7.4",
"unbuild": "^0.7.4",
"vite": "^2.9.14",
"vitest": "^0.17.1"
"vitest": "^0.18.0"
},
"dependencies": {
"@unocss/core": "^0.44.0"
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Postprocessor, Preset, PresetOptions } from '@unocss/core'
import type { Preset, PresetOptions } from '@unocss/core'
import { preflights } from './preflights'
import { rules } from './rules'
import type { Theme, ThemeAnimation } from './theme'
import { theme } from './theme'
import { transformSelector } from './transform'
import { variants } from './variants'

export { preflights } from './preflights'
Expand All @@ -11,7 +12,7 @@ export { parseColor } from './utils'

export type { ThemeAnimation, Theme }

// v0.41.2
// v0.44.0

export interface PresetMiniOptions extends PresetOptions {
/**
Expand Down Expand Up @@ -45,6 +46,10 @@ export const presetWeapp = (options: PresetMiniOptions = {}): Preset<Theme> => {
variants: variants(options),
options,
postprocess(css) {
// 转换
css.selector = transformSelector(css.selector)

// 设置变量前缀
if (options.variablePrefix && options.variablePrefix !== 'un-')
VarPrefixPostprocessor(options.variablePrefix, css)
},
Expand Down
49 changes: 29 additions & 20 deletions src/transform/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
/*
* @Author: licl
* @Date: 2022-07-07 21:07:52
* @LastEditTime: 2022-07-07 21:17:38
* @LastEditors: licl
* @Description:
*/
const rules: { [key: string]: string } = {
'-d-': '.',
'-s-': '/',
'-c-': ':',
'-p-': '%',
'-e-': '!',
'-w-': '#',
'-bl-': '(',
'-br-': ')',
'-fl-': '[',
'-fr-': ']',
'-r-': '$',
import { escapeRegExp } from '@unocss/core'

const rules: Record<string, string> = {
'.': '-d-',
'/': '-s-',
':': '-c-',
'%': '-p-',
'!': '-e-',
'#': '-w-',
'(': '-bl-',
')': '-br-',
'[': '-fl-',
']': '-fr-',
'$': '-r-',
}

/**
Expand All @@ -27,6 +22,20 @@ const rules: { [key: string]: string } = {
*/
export function restoreSelector(selector: string) {
for (const rule in rules)
selector = selector.replaceAll(rule, rules[rule])
selector = selector.replaceAll(rules[rule], rule)
return selector
}

export function transformSelector(selector: string) {
// selector = selector.slice(1)
// .ring-offset-size-\$variable => .ring-offset-size--r-variable

if (/[\.\/:%!#\(\)\[\]$]/.test(selector)) {
for (const rule in rules) {
const replaceReg = new RegExp(escapeRegExp(`\\${rule}`), 'g')
// selector = selector.replace(replaceReg, transformRules[transformRule])
selector = selector.replace(replaceReg, rules[rule])
}
}
return selector
}
Loading

0 comments on commit 853028e

Please sign in to comment.