Skip to content

Commit

Permalink
[feat]support variant check
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderXL committed Jan 3, 2025
1 parent a00c069 commit f4132e9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/unocss-base/preset-rn/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import rules from './rules/index.js'
import theme from './theme.js'
import blocklistVariants from './variants/index.js'

export default function presetRnMpx () {
return {
name: '@mpxjs/unocss-preset-rn',
rules,
theme
theme,
blocklist: [
...blocklistVariants
]
}
}
75 changes: 75 additions & 0 deletions packages/unocss-base/preset-rn/variants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {
variantAria,
variantTaggedAriaAttributes,
variantChildren,
variantCombinators,
variantContainerQuery,
variantTaggedDataAttributes,
variantLanguageDirections,
variantStartingStyle,
variantSupports,
variantSelector,
variantCssLayer,
variantScope
} from '@unocss/preset-mini/variants'
import {
variantCombinators as windVariantCombinator,
variantContrasts,
variantMotions,
variantSpaceAndDivide,
variantStickyHover,
placeholderModifier
} from '@unocss/preset-wind/variants'

const wrapVariant = function (variant) {
return function (raw) {
const ctx = {
rawSelector: raw,
theme: this.config,
generator: this
}
const { match } = variant
if (match(raw, ctx)) {
this._mpx2rnUnsuportedRules = this._mpx2rnUnsuportedRules || []
this._mpx2rnUnsuportedRules.push(raw)
return true
}
}
}

const transformVariants = function (variantsArr) {
return variantsArr.map(variants => {
if (Array.isArray(variants)) {
return variants.map(variant => wrapVariant(variant))
} else {
return wrapVariant(variants)
}
}).reduce((preV, curV) => {
if (Array.isArray(curV)) {
return preV.concat(...curV)
} else {
return preV.concat(curV)
}
}, [])
}

export default transformVariants([
variantAria,
variantTaggedAriaAttributes,
variantChildren,
variantCombinators,
variantContainerQuery,
variantTaggedDataAttributes,
variantLanguageDirections,
variantStartingStyle,
variantSupports,
variantSelector,
variantCssLayer,
variantScope,
windVariantCombinator,
variantContrasts,
variantMotions,
variantSpaceAndDivide,
variantStickyHover,
placeholderModifier
])
6 changes: 6 additions & 0 deletions packages/unocss-plugin/lib/rn-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ function WebpackPlugin (configOrPath, defaults) {

compiler.hooks.beforeCompile.tapPromise(PLUGIN_NAME, async (compilation) => {
const ctx = await createContext(configOrPath, defaults)
ctx.uno.config.blocklist = ctx.uno.config.blocklist.map((item) => {
if (typeof item === 'function') {
return item.bind(ctx.uno)
}
return item
})
compiler.__unoCtx = ctx
return ctx
})
Expand Down
2 changes: 1 addition & 1 deletion packages/unocss-plugin/lib/web-plugin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function createContext (configOrPath, defaults = {}, extraConfigSources =

const result = await config.loadConfig(root, configOrPath, extraConfigSources, defaults)
rawConfig = result.config
uno.setConfig(rawConfig)
await uno.setConfig(rawConfig)
rollupFilter = pluginutils.createFilter(
rawConfig.include || defaultInclude,
rawConfig.exclude || defaultExclude
Expand Down

0 comments on commit f4132e9

Please sign in to comment.