Skip to content

Commit 33dfb61

Browse files
authored
feat: add rem polyfill to withUniwindConfig (#71)
1 parent 9b89af7 commit 33dfb61

7 files changed

Lines changed: 22 additions & 9 deletions

File tree

apps/expo-example/metro.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ module.exports = (async () => {
1717
path.join(workspaceRoot, 'node_modules'),
1818
]
1919

20-
return withUniwindConfig(config, { cssEntryFile: 'global.css', extraThemes: ['premium'] })
20+
return withUniwindConfig(config, { cssEntryFile: 'global.css', extraThemes: ['premium'], polyfills: { rem: 14 } })
2121
})()

packages/uniwind/src/metro/compileVirtual.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import { compile } from '@tailwindcss/node'
22
import { polyfillWeb } from './polyfillWeb'
33
import { ProcessorBuilder } from './processor'
44
import { addMetaToStylesTemplate, serializeStylesheet } from './stylesheet'
5-
import { Platform } from './types'
5+
import { Platform, Polyfills } from './types'
66

77
type CompileVirtualConfig = {
88
cssPath: string
99
css: string
1010
candidates: Array<string>
1111
platform: Platform
1212
themes: Array<string>
13+
polyfills: Polyfills | undefined
1314
}
1415

15-
export const compileVirtual = async ({ candidates, css, cssPath, platform, themes }: CompileVirtualConfig) => {
16+
export const compileVirtual = async ({ candidates, css, cssPath, platform, themes, polyfills }: CompileVirtualConfig) => {
1617
const compiler = await compile(css, {
1718
base: cssPath,
1819
onDependency: () => void 0,
@@ -23,7 +24,7 @@ export const compileVirtual = async ({ candidates, css, cssPath, platform, theme
2324
return polyfillWeb(tailwindCSS)
2425
}
2526

26-
const Processor = new ProcessorBuilder(themes)
27+
const Processor = new ProcessorBuilder(themes, polyfills)
2728

2829
Processor.transform(tailwindCSS)
2930

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import type { MetroConfig } from 'metro-config'
22

3+
type Polyfills = {
4+
rem?: number
5+
}
6+
37
type UniwindConfig = {
48
cssEntryFile: string
59
extraThemes?: Array<string>
610
dtsFile?: string
11+
polyfills?: Polyfills
712
}
813

914
export declare function withUniwindConfig(config: MetroConfig, options: UniwindConfig): MetroConfig

packages/uniwind/src/metro/processor/processor.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Declaration, MediaQuery, Rule, transform } from 'lightningcss'
2+
import { Polyfills } from '../types'
23
import { Color } from './color'
34
import { CSS } from './css'
45
import { Functions } from './functions'
@@ -9,9 +10,7 @@ import { Var } from './var'
910

1011
export class ProcessorBuilder {
1112
stylesheets = {} as Record<string, Array<any>>
12-
vars = {
13-
'--uniwind-em': 16,
14-
} as Record<string, any>
13+
vars = {} as Record<string, any>
1514
CSS = new CSS(this)
1615
RN = new RN(this)
1716
Var = new Var(this)
@@ -22,7 +21,9 @@ export class ProcessorBuilder {
2221

2322
private declarationConfig = this.getDeclarationConfig()
2423

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

2728
transform(css: string) {
2829
transform({

packages/uniwind/src/metro/processor/units.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class Units {
2121
case 'vh':
2222
return `rt.screen.height * ${length.value / 100}`
2323
case 'rem':
24-
return length.value * 16
24+
return length.value * this.Processor.vars['--uniwind-em']
2525
case 'em':
2626
return `this[\`--uniwind-em\`] * ${length.value}`
2727
default:

packages/uniwind/src/metro/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ export type ExtendedFileSystem = {
2525
getSha1: WithUniwindPatch<(filename: string) => string>
2626
}
2727

28+
export type Polyfills = {
29+
rem?: number
30+
}
31+
2832
export type UniwindConfig = {
2933
cssEntryFile: string
3034
themes: Array<string>
3135
extraThemes?: Array<string>
3236
dtsFile?: string
37+
polyfills?: Polyfills
3338
}
3439

3540
export type MediaQueryResolver = {

packages/uniwind/src/metro/withUniwindConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export const withUniwindConfig = (
132132
css: uniwind.cssFile,
133133
platform,
134134
themes: uniwindConfig.themes,
135+
polyfills: uniwindConfig.polyfills,
135136
})
136137

137138
uniwind.virtualModules.set(getVirtualPath(platform), virtualFile)

0 commit comments

Comments
 (0)