Skip to content

Commit e656d99

Browse files
authored
Merge pull request #18 from dcasia/polyfill-space-between
Compatible with 'space between' utilities
2 parents 306fcf5 + 9c0c181 commit e656d99

File tree

8 files changed

+62
-15
lines changed

8 files changed

+62
-15
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ module.exports = {
2222
'unicorn/prevent-abbreviations': 'off',
2323
'multiline-comment-style': 'off',
2424
'capitalized-comments': 'off',
25+
'unicorn/no-array-for-each': 'off',
2526
},
2627
}

package-lock.json

Lines changed: 23 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
{
22
"name": "@dcasia/mini-program-tailwind-webpack-plugin",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "让你的小程序用上原汁原味的 Tailwind/Windi CSS",
55
"keywords": [
6-
"miniprogram",
76
"mini-program",
7+
"tailwind",
88
"tailwindcss",
99
"windicss",
1010
"wechat",
1111
"taro",
1212
"css",
1313
"postcss",
14-
"uni-app"
14+
"uni-app",
15+
"miniprogram",
16+
"weapp",
17+
"atomic-css",
18+
"unocss"
1519
],
1620
"main": "dist/index.js",
1721
"files": [
@@ -70,6 +74,7 @@
7074
"jest": "^27.5.1",
7175
"rollup": "^2.68.0",
7276
"rollup-plugin-dts": "^4.2.1",
77+
"tslib": "^2.4.0",
7378
"vite": "^2.9.9",
7479
"webpack": "^5.69.1"
7580
},

src/interfaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { TaroFramework } from './enum'
44
export interface Options {
55
enableRpx?: boolean,
66
designWidth?: number,
7+
utilitiesSettings?: {
8+
spaceBetweenItems?: string[],
9+
},
710
}
811

912
export interface TaroWebpackPluginOptions {

src/postcss/index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import { FileType } from '../enum'
2-
import { handleCharacters } from '../utilities'
2+
import { customReplace, handleCharacters } from '../utilities'
33
import { Options } from '../interfaces'
44

5-
export function transformSelector() {
5+
export function transformSelector(options: Options) {
66

77
const processed = Symbol('processed')
8+
const defaultSpaceBetweenItems = [ 'view', 'button', 'text', 'image' ]
9+
const usersSpaceBetweenItems = options?.utilitiesSettings?.spaceBetweenItems || []
10+
const spaceBetweenItems = Array.from(new Set([ ...defaultSpaceBetweenItems, ...usersSpaceBetweenItems ]))
11+
const customReplacement = new Map()
12+
13+
/**
14+
* A polyfill that is compatible 'space-[x,y]-\d' syntax
15+
* Note that in mini program environment ':not()' selector can only be used when it's combined with other selectors
16+
* e.g. view:not() works but the standalone :not() selector couldn't work
17+
*/
18+
customReplacement.set(/^(\.-?space-\w)(-.+?)\s.*/, spaceBetweenItems.map(item => `$1$2:not($1-reverse) > ${ item }:not([hidden]):not(:first-child), $1$2$1-reverse > ${ item }:not([hidden]):not(:last-child)`).join(', '))
19+
customReplacement.set(/^(\.-?space-\w-reverse).*/, spaceBetweenItems.map(item => `$1 > ${ item }:not([hidden])`).join(', '))
820

921
return {
1022
postcssPlugin: 'transformSelectorName',
@@ -13,6 +25,7 @@ export function transformSelector() {
1325
if (!node[ processed ]) {
1426

1527
node.selector = handleCharacters(node.selector, FileType.Style)
28+
node.selector = customReplace(node.selector, customReplacement)
1629
node[ processed ] = true
1730

1831
}

src/style-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { transformSelector, transformValue } from './postcss'
55
export function handleStyle(rawSource: string, option?: Options) {
66

77
const processor = postcss()
8-
.use(transformSelector)
8+
.use(transformSelector(option))
99

1010
if (option?.enableRpx) {
1111
processor.use(transformValue(option))

src/universal-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function handleSource(fileType: FileType, source: string, options?: Optio
1515

1616
}
1717

18-
export default {
18+
export {
1919
handleStyle,
2020
handleTemplate,
2121
}

src/utilities.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,13 @@ export function handleCharacters(content, type: FileType) {
6363
return content
6464

6565
}
66+
67+
export function customReplace(raw: string, rules: Map<RegExp, string>) {
68+
69+
rules.forEach((target, exp) => {
70+
raw = raw.replace(exp, target)
71+
})
72+
73+
return raw
74+
75+
}

0 commit comments

Comments
 (0)