Skip to content

Commit db31332

Browse files
authored
Added 'customAttributes' configuration to support custom class attribute names (#75)
1 parent 4de47a0 commit db31332

7 files changed

+69
-6
lines changed

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@
6060
},
6161
"homepage": "https://github.com/dcasia/mini-program-tailwind#readme",
6262
"devDependencies": {
63-
"@rollup/plugin-commonjs": "^21.0.2",
63+
"@rollup/plugin-commonjs": "^21.1.0",
6464
"@rollup/plugin-multi-entry": "^4.1.0",
6565
"@rollup/plugin-typescript": "^8.3.1",
6666
"@tarojs/service": "^3.4.9",
6767
"@types/jest": "^27.4.1",
68+
"@types/micromatch": "^4.0.2",
6869
"@typescript-eslint/eslint-plugin": "^5.13.0",
6970
"@typescript-eslint/parser": "^5.13.0",
7071
"eslint": "^8.10.0",
@@ -75,14 +76,16 @@
7576
"rollup": "^2.68.0",
7677
"rollup-plugin-dts": "^4.2.1",
7778
"tslib": "^2.4.0",
79+
"typescript": "^4.7.4",
7880
"vite": "^2.9.9",
7981
"webpack": "^5.69.1"
8082
},
8183
"dependencies": {
8284
"@babel/core": "^7.17.5",
8385
"@vivaxy/wxml": "^2.1.0",
86+
"micromatch": "^4.0.5",
8487
"postcss": "^8.4.7",
8588
"webpack-sources": "^1.4.3",
8689
"windicss-webpack-plugin": "^1.7.2"
8790
}
88-
}
91+
}

rollup.config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineConfig } from 'rollup'
22
import typescript from '@rollup/plugin-typescript'
3+
import commonjs from '@rollup/plugin-commonjs'
34

45
export default defineConfig([
56
{
@@ -34,6 +35,7 @@ export default defineConfig([
3435
'@vivaxy/wxml',
3536
'webpack-sources',
3637
'windicss-webpack-plugin',
38+
'micromatch',
3739
],
3840
plugins: [
3941
typescript(
@@ -51,6 +53,7 @@ export default defineConfig([
5153
'postcss',
5254
'@babel/core',
5355
'@vivaxy/wxml',
56+
'micromatch',
5457
],
5558
plugins: [
5659
typescript(
@@ -68,11 +71,13 @@ export default defineConfig([
6871
'@babel/core',
6972
'@vivaxy/wxml',
7073
'postcss',
74+
'micromatch',
7175
],
7276
plugins: [
7377
typescript(
7478
{ tsconfig: './tsconfig.json' },
7579
),
80+
commonjs(),
7681
],
7782
},
7883
// {

src/interfaces.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface Options {
88
spaceBetweenItems?: string[],
99
divideItems?: string[],
1010
customComponents?: string[],
11+
customAttributes?: Record<string, string | string[]>,
1112
},
1213
}
1314

src/template-handler.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import * as wxml from '@vivaxy/wxml'
2+
import micromatch from 'micromatch'
23
import { handleCharacters } from './utilities'
34
import { FileType } from './enum'
45
import { replaceStringLiteralPlugin } from './babel'
56
import * as babel from '@babel/core'
7+
import type { Options } from './interfaces'
68

79
const matchScriptsInsideClassNames = /({{)(.+?)(}})/g
810
const replaceMarker = '__MP_TW_PLUGIN_REPLACE__'
911

10-
export function handleTemplate(rawSource: string) {
11-
12+
export function handleTemplate(rawSource: string, options?: Options) {
1213
const parsed = wxml.parse(rawSource)
1314

1415
wxml.traverse(parsed, node => {
@@ -23,6 +24,28 @@ export function handleTemplate(rawSource: string) {
2324
node.attributes.virtualHostClass = node.attributes.class
2425
}
2526

27+
if (options?.utilitiesSettings?.customAttributes) {
28+
29+
for (const [match, attrs] of Object.entries(options.utilitiesSettings.customAttributes)) {
30+
31+
if (/^[\w-]+$/.test(match) ? match === node.tagName : micromatch.isMatch(node.tagName, match)) {
32+
const _attrs = Array.isArray(attrs) ? attrs : [attrs]
33+
34+
for (const attrKey of _attrs) {
35+
36+
// skip class because it has already been converted
37+
if (attrKey === 'class') continue
38+
39+
if (node.attributes[attrKey]) {
40+
node.attributes[attrKey] = handleClassNameInTemplate(node.attributes[attrKey])
41+
}
42+
43+
}
44+
}
45+
46+
}
47+
48+
}
2649
}
2750

2851
})

src/universal-handler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function handleSource(fileType: FileType, source: string, options?: Optio
1010
}
1111

1212
if (fileType === FileType.Template) {
13-
return handleTemplate(source)
13+
return handleTemplate(source, options)
1414
}
1515

1616
}

test/index.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,33 @@ test('css value infer', () => {
7373
expect(handledTemplate).toBe('.h--0-d-5px- {height: 0.5px;}')
7474

7575
})
76+
77+
test('customAttributes', () => {
78+
79+
const template = `<view class="w-[0.5px] w-0.5px w-[2rpx] w-2rpx" custom-class="w-[2rpx]"></view>`
80+
const handledTemplate = handleSource('template', template, {
81+
utilitiesSettings: {
82+
customAttributes: {
83+
'view': ['class', 'custom-class'],
84+
}
85+
}
86+
})
87+
88+
expect(handledTemplate).toBe('<view class="w--0-d-5px- w-0-d-5px w--2rpx- w-2rpx" custom-class="w--2rpx-"></view>')
89+
90+
})
91+
92+
test('customAttributes match', () => {
93+
94+
const template = `<custom-component class="w-[0.5px] w-0.5px w-[2rpx] w-2rpx" custom-class="w-[2rpx]"></custom-component>`
95+
const handledTemplate = handleSource('template', template, {
96+
utilitiesSettings: {
97+
customAttributes: {
98+
'custom-*': ['custom-class'],
99+
}
100+
}
101+
})
102+
103+
expect(handledTemplate).toBe('<custom-component class="w--0-d-5px- w-0-d-5px w--2rpx- w-2rpx" custom-class="w--2rpx-"></custom-component>')
104+
105+
})

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
// optional - in general it's a good practice to decouple declaration files from your actual transpiled JavaScript files
66
"declarationDir": "dts",
77
// optional if you're using babel to transpile TS - JS
8-
"emitDeclarationOnly": true
8+
"emitDeclarationOnly": true,
9+
"esModuleInterop": true
910
},
1011
"include": ["src/**/*"]
1112
}

0 commit comments

Comments
 (0)