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
3 changes: 2 additions & 1 deletion CONTEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ Metro integration:

- `withUniwindConfig(config, uniwindConfig)` patches Metro graph support for uncached modules.
- Metro adds `css` as source extension and removes it from asset extensions.
- Metro transformer handles the configured CSS entry file specially.
- Metro transformer handles the configured CSS entry file specially. In development, native entries declare imported local CSS files as Metro dependencies, including nested imports and workspace files resolved outside `node_modules`, so token-only edits trigger recompilation. Dependencies are collected afresh on each compile.
- Non-entry native CSS is an empty module in plain Metro; Expo keeps its own CSS handling. Web CSS handling is unchanged.
- Metro transformer worker selection is lazy, cached per Expo/non-Expo config type, and follows Expo transformer paths or Expo-specific config markers.
- Native platform CSS transforms into a JS module that calls `Uniwind.__reinit(...)` with a fingerprint of the generated styles and themes. During development, the native runtime skips reinitialization when that fingerprint is unchanged.
- Web platform CSS transforms into CSS plus web runtime setup.
Expand Down
18 changes: 17 additions & 1 deletion packages/uniwind/src/bundler/adapters/metro/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,28 @@ export const transform = async (
}

if (!isCss) {
if (!config.uniwind.isExpoProject && options.platform !== Platform.Web && options.type !== 'asset' && filePath.endsWith('.css')) {
// Plain Metro parses CSS as JavaScript; these modules only register watched files.
return worker.transform(config, projectRoot, `${filePath}.js`, Buffer.from(''), options)
}

return worker.transform(config, projectRoot, filePath, data, options)
}

const bundlerConfig = UniwindBundlerConfig.fromMetroConfig(config.uniwind, options.platform)
await bundlerConfig.generateArtifacts(cssArtifactPath)
const virtualCode = await compileCSS(bundlerConfig)
const isWeb = bundlerConfig.platform === Platform.Web
const importedStylesheets = new Set<string>()
const virtualCode = await compileCSS(bundlerConfig, dependency => {
if (!isWeb && options.dev && dependency.endsWith('.css') && !dependency.includes(`${path.sep}node_modules${path.sep}`)) {
importedStylesheets.add(dependency)
}
})
const importedStylesheetRequires = Array.from(importedStylesheets).sort().map(stylesheet => {
const relativePath = path.relative(path.dirname(bundlerConfig.cssPath), stylesheet).split(path.sep).join('/')

return `require(${JSON.stringify(relativePath.startsWith('../') ? relativePath : `./${relativePath}`)});`
})
const nativeStylesFingerprint = isWeb
? undefined
: createHash('sha256')
Expand All @@ -82,6 +97,7 @@ export const transform = async (
isWeb
? virtualCode
: [
...importedStylesheetRequires,
Comment thread
Brentlok marked this conversation as resolved.
`const { Uniwind } = require('uniwind');`,
`Uniwind.__reinit(rt => ${virtualCode}, ${bundlerConfig.stringifiedThemes}, '${nativeStylesFingerprint}');`,
].join(''),
Expand Down
4 changes: 2 additions & 2 deletions packages/uniwind/src/bundler/css-compiler/compileCSS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { compileNativeCSS } from './compileNativeCSS'
import { compileTailwind } from './compileTailwind'
import { compileWebCSS } from './compileWebCSS'

export const compileCSS = async (bundlerConfig: UniwindBundlerConfig) => {
const tailwindCSS = await compileTailwind(bundlerConfig)
export const compileCSS = async (bundlerConfig: UniwindBundlerConfig, onDependency?: (dependency: string) => void) => {
const tailwindCSS = await compileTailwind(bundlerConfig, onDependency)

if (bundlerConfig.platform === Platform.Web) {
return compileWebCSS(bundlerConfig, tailwindCSS)
Expand Down
7 changes: 5 additions & 2 deletions packages/uniwind/src/bundler/css-compiler/compileTailwind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import fs from 'fs'
import path from 'path'
import type { UniwindBundlerConfig } from '../config'

export const compileTailwind = async (bundlerConfig: UniwindBundlerConfig) => {
export const compileTailwind = async (
bundlerConfig: UniwindBundlerConfig,
onDependency: (dependency: string) => void = () => void 0,
) => {
const css = await fs.promises.readFile(bundlerConfig.cssPath, 'utf-8')
const compiler = await compile(css, {
base: path.dirname(bundlerConfig.cssPath),
onDependency: () => void 0,
onDependency,
})
const scanner = new Scanner({
sources: [
Expand Down
Loading