Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/expo-example/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ module.exports = (async () => {
path.join(workspaceRoot, 'node_modules'),
]

return withUniwindConfig(config, { cssEntryFile: 'global.css', extraThemes: ['premium'] })
return withUniwindConfig(config, { cssEntryFile: 'global.css', extraThemes: ['premium'], polyfills: { rem: 14 } })
})()
7 changes: 4 additions & 3 deletions packages/uniwind/src/metro/compileVirtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { compile } from '@tailwindcss/node'
import { polyfillWeb } from './polyfillWeb'
import { ProcessorBuilder } from './processor'
import { addMetaToStylesTemplate, serializeStylesheet } from './stylesheet'
import { Platform } from './types'
import { Platform, Polyfills } from './types'

type CompileVirtualConfig = {
cssPath: string
css: string
candidates: Array<string>
platform: Platform
themes: Array<string>
polyfills: Polyfills | undefined
}

export const compileVirtual = async ({ candidates, css, cssPath, platform, themes }: CompileVirtualConfig) => {
export const compileVirtual = async ({ candidates, css, cssPath, platform, themes, polyfills }: CompileVirtualConfig) => {
const compiler = await compile(css, {
base: cssPath,
onDependency: () => void 0,
Expand All @@ -23,7 +24,7 @@ export const compileVirtual = async ({ candidates, css, cssPath, platform, theme
return polyfillWeb(tailwindCSS)
}

const Processor = new ProcessorBuilder(themes)
const Processor = new ProcessorBuilder(themes, polyfills)

Processor.transform(tailwindCSS)

Expand Down
5 changes: 5 additions & 0 deletions packages/uniwind/src/metro/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { MetroConfig } from 'metro-config'

type Polyfills = {
rem?: number
}

type UniwindConfig = {
cssEntryFile: string
extraThemes?: Array<string>
dtsFile?: string
polyfills?: Polyfills
}

export declare function withUniwindConfig(config: MetroConfig, options: UniwindConfig): MetroConfig
9 changes: 5 additions & 4 deletions packages/uniwind/src/metro/processor/processor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Declaration, MediaQuery, Rule, transform } from 'lightningcss'
import { Polyfills } from '../types'
import { Color } from './color'
import { CSS } from './css'
import { Functions } from './functions'
Expand All @@ -9,9 +10,7 @@ import { Var } from './var'

export class ProcessorBuilder {
stylesheets = {} as Record<string, Array<any>>
vars = {
'--uniwind-em': 16,
} as Record<string, any>
vars = {} as Record<string, any>
CSS = new CSS(this)
RN = new RN(this)
Var = new Var(this)
Expand All @@ -22,7 +21,9 @@ export class ProcessorBuilder {

private declarationConfig = this.getDeclarationConfig()

constructor(private readonly themes: Array<string>) {}
constructor(private readonly themes: Array<string>, readonly polyfills: Polyfills | undefined) {
this.vars['--uniwind-em'] = polyfills?.rem ?? 16
}

transform(css: string) {
transform({
Expand Down
2 changes: 1 addition & 1 deletion packages/uniwind/src/metro/processor/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Units {
case 'vh':
return `rt.screen.height * ${length.value / 100}`
case 'rem':
return length.value * 16
return length.value * this.Processor.vars['--uniwind-em']
case 'em':
return `this[\`--uniwind-em\`] * ${length.value}`
default:
Expand Down
5 changes: 5 additions & 0 deletions packages/uniwind/src/metro/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ export type ExtendedFileSystem = {
getSha1: WithUniwindPatch<(filename: string) => string>
}

export type Polyfills = {
rem?: number
}

export type UniwindConfig = {
cssEntryFile: string
themes: Array<string>
extraThemes?: Array<string>
dtsFile?: string
polyfills?: Polyfills
}

export type MediaQueryResolver = {
Expand Down
1 change: 1 addition & 0 deletions packages/uniwind/src/metro/withUniwindConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const withUniwindConfig = (
css: uniwind.cssFile,
platform,
themes: uniwindConfig.themes,
polyfills: uniwindConfig.polyfills,
})

uniwind.virtualModules.set(getVirtualPath(platform), virtualFile)
Expand Down