rnwind 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/lib/cjs/core/normalize-classname.cjs +25 -0
  2. package/lib/cjs/core/normalize-classname.cjs.map +1 -0
  3. package/lib/cjs/core/normalize-classname.d.ts +10 -0
  4. package/lib/cjs/core/style-builder/build-style.cjs +258 -58
  5. package/lib/cjs/core/style-builder/build-style.cjs.map +1 -1
  6. package/lib/cjs/core/style-builder/build-style.d.ts +6 -1
  7. package/lib/cjs/core/style-builder/union-builder.cjs +37 -3
  8. package/lib/cjs/core/style-builder/union-builder.cjs.map +1 -1
  9. package/lib/cjs/core/style-builder/union-builder.d.ts +21 -1
  10. package/lib/cjs/metro/css-imports.cjs +81 -0
  11. package/lib/cjs/metro/css-imports.cjs.map +1 -0
  12. package/lib/cjs/metro/css-imports.d.ts +8 -0
  13. package/lib/cjs/metro/dts.cjs +7 -16
  14. package/lib/cjs/metro/dts.cjs.map +1 -1
  15. package/lib/cjs/metro/dts.d.ts +2 -4
  16. package/lib/cjs/metro/state.cjs +38 -86
  17. package/lib/cjs/metro/state.cjs.map +1 -1
  18. package/lib/cjs/metro/state.d.ts +8 -25
  19. package/lib/cjs/metro/transformer.cjs +193 -34
  20. package/lib/cjs/metro/transformer.cjs.map +1 -1
  21. package/lib/cjs/metro/with-config.cjs +2 -2
  22. package/lib/cjs/metro/with-config.cjs.map +1 -1
  23. package/lib/cjs/metro/with-config.d.ts +11 -26
  24. package/lib/cjs/metro/wrap-imports.cjs +273 -0
  25. package/lib/cjs/metro/wrap-imports.cjs.map +1 -0
  26. package/lib/cjs/metro/wrap-imports.d.ts +26 -0
  27. package/lib/cjs/runtime/components/rnwind-provider.cjs +0 -17
  28. package/lib/cjs/runtime/components/rnwind-provider.cjs.map +1 -1
  29. package/lib/cjs/runtime/components/rnwind-provider.d.ts +0 -14
  30. package/lib/cjs/runtime/hooks/use-css.cjs +16 -10
  31. package/lib/cjs/runtime/hooks/use-css.cjs.map +1 -1
  32. package/lib/cjs/runtime/hooks/use-css.d.ts +15 -9
  33. package/lib/cjs/runtime/index.cjs +11 -13
  34. package/lib/cjs/runtime/index.cjs.map +1 -1
  35. package/lib/cjs/runtime/index.d.ts +4 -9
  36. package/lib/cjs/runtime/lookup-css.cjs +10 -0
  37. package/lib/cjs/runtime/lookup-css.cjs.map +1 -1
  38. package/lib/cjs/runtime/lookup-css.d.ts +7 -0
  39. package/lib/cjs/runtime/resolve.cjs +348 -0
  40. package/lib/cjs/runtime/resolve.cjs.map +1 -0
  41. package/lib/cjs/runtime/resolve.d.ts +61 -0
  42. package/lib/cjs/runtime/wrap.cjs +254 -0
  43. package/lib/cjs/runtime/wrap.cjs.map +1 -0
  44. package/lib/cjs/runtime/wrap.d.ts +37 -0
  45. package/lib/cjs/testing/index.cjs +81 -50
  46. package/lib/cjs/testing/index.cjs.map +1 -1
  47. package/lib/esm/core/normalize-classname.d.ts +10 -0
  48. package/lib/esm/core/normalize-classname.mjs +23 -0
  49. package/lib/esm/core/normalize-classname.mjs.map +1 -0
  50. package/lib/esm/core/style-builder/build-style.d.ts +6 -1
  51. package/lib/esm/core/style-builder/build-style.mjs +258 -58
  52. package/lib/esm/core/style-builder/build-style.mjs.map +1 -1
  53. package/lib/esm/core/style-builder/union-builder.d.ts +21 -1
  54. package/lib/esm/core/style-builder/union-builder.mjs +37 -3
  55. package/lib/esm/core/style-builder/union-builder.mjs.map +1 -1
  56. package/lib/esm/metro/css-imports.d.ts +8 -0
  57. package/lib/esm/metro/css-imports.mjs +79 -0
  58. package/lib/esm/metro/css-imports.mjs.map +1 -0
  59. package/lib/esm/metro/dts.d.ts +2 -4
  60. package/lib/esm/metro/dts.mjs +7 -16
  61. package/lib/esm/metro/dts.mjs.map +1 -1
  62. package/lib/esm/metro/state.d.ts +8 -25
  63. package/lib/esm/metro/state.mjs +39 -85
  64. package/lib/esm/metro/state.mjs.map +1 -1
  65. package/lib/esm/metro/transformer.mjs +194 -35
  66. package/lib/esm/metro/transformer.mjs.map +1 -1
  67. package/lib/esm/metro/with-config.d.ts +11 -26
  68. package/lib/esm/metro/with-config.mjs +2 -2
  69. package/lib/esm/metro/with-config.mjs.map +1 -1
  70. package/lib/esm/metro/wrap-imports.d.ts +26 -0
  71. package/lib/esm/metro/wrap-imports.mjs +250 -0
  72. package/lib/esm/metro/wrap-imports.mjs.map +1 -0
  73. package/lib/esm/runtime/components/rnwind-provider.d.ts +0 -14
  74. package/lib/esm/runtime/components/rnwind-provider.mjs +1 -17
  75. package/lib/esm/runtime/components/rnwind-provider.mjs.map +1 -1
  76. package/lib/esm/runtime/hooks/use-css.d.ts +15 -9
  77. package/lib/esm/runtime/hooks/use-css.mjs +16 -10
  78. package/lib/esm/runtime/hooks/use-css.mjs.map +1 -1
  79. package/lib/esm/runtime/index.d.ts +4 -9
  80. package/lib/esm/runtime/index.mjs +4 -4
  81. package/lib/esm/runtime/index.mjs.map +1 -1
  82. package/lib/esm/runtime/lookup-css.d.ts +7 -0
  83. package/lib/esm/runtime/lookup-css.mjs +10 -1
  84. package/lib/esm/runtime/lookup-css.mjs.map +1 -1
  85. package/lib/esm/runtime/resolve.d.ts +61 -0
  86. package/lib/esm/runtime/resolve.mjs +341 -0
  87. package/lib/esm/runtime/resolve.mjs.map +1 -0
  88. package/lib/esm/runtime/wrap.d.ts +37 -0
  89. package/lib/esm/runtime/wrap.mjs +251 -0
  90. package/lib/esm/runtime/wrap.mjs.map +1 -0
  91. package/lib/esm/testing/index.mjs +84 -53
  92. package/lib/esm/testing/index.mjs.map +1 -1
  93. package/package.json +2 -1
  94. package/src/core/normalize-classname.ts +19 -0
  95. package/src/core/style-builder/build-style.ts +286 -55
  96. package/src/core/style-builder/union-builder.ts +36 -3
  97. package/src/metro/css-imports.ts +75 -0
  98. package/src/metro/dts.ts +7 -19
  99. package/src/metro/state.ts +38 -83
  100. package/src/metro/transformer.ts +190 -34
  101. package/src/metro/with-config.ts +13 -28
  102. package/src/metro/wrap-imports.ts +260 -0
  103. package/src/runtime/components/rnwind-provider.tsx +0 -17
  104. package/src/runtime/hooks/use-css.ts +17 -11
  105. package/src/runtime/index.ts +3 -26
  106. package/src/runtime/lookup-css.ts +10 -0
  107. package/src/runtime/resolve.ts +381 -0
  108. package/src/runtime/wrap.tsx +267 -0
  109. package/src/testing/index.ts +106 -56
  110. package/lib/cjs/core/parser/text-truncate.cjs +0 -78
  111. package/lib/cjs/core/parser/text-truncate.cjs.map +0 -1
  112. package/lib/cjs/metro/transform-ast.cjs +0 -1472
  113. package/lib/cjs/metro/transform-ast.cjs.map +0 -1
  114. package/lib/cjs/metro/transform-ast.d.ts +0 -88
  115. package/lib/cjs/runtime/haptics.cjs +0 -113
  116. package/lib/cjs/runtime/haptics.cjs.map +0 -1
  117. package/lib/cjs/runtime/haptics.d.ts +0 -48
  118. package/lib/cjs/runtime/interactive-box.cjs +0 -35
  119. package/lib/cjs/runtime/interactive-box.cjs.map +0 -1
  120. package/lib/cjs/runtime/interactive-box.d.ts +0 -40
  121. package/lib/esm/core/parser/text-truncate.mjs +0 -75
  122. package/lib/esm/core/parser/text-truncate.mjs.map +0 -1
  123. package/lib/esm/metro/transform-ast.d.ts +0 -88
  124. package/lib/esm/metro/transform-ast.mjs +0 -1451
  125. package/lib/esm/metro/transform-ast.mjs.map +0 -1
  126. package/lib/esm/runtime/haptics.d.ts +0 -48
  127. package/lib/esm/runtime/haptics.mjs +0 -110
  128. package/lib/esm/runtime/haptics.mjs.map +0 -1
  129. package/lib/esm/runtime/interactive-box.d.ts +0 -40
  130. package/lib/esm/runtime/interactive-box.mjs +0 -33
  131. package/lib/esm/runtime/interactive-box.mjs.map +0 -1
  132. package/src/metro/transform-ast.ts +0 -1729
  133. package/src/runtime/haptics.ts +0 -120
  134. package/src/runtime/interactive-box.tsx +0 -57
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../../../../src/testing/index.ts"],"sourcesContent":["/* eslint-disable sonarjs/no-unused-vars */\nimport type { File as BabelFile } from '@babel/types'\nimport { parse } from '@babel/parser'\nimport generateImport from '@babel/generator'\nimport { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'\nimport { createRequire } from 'node:module'\nimport { tmpdir } from 'node:os'\nimport path from 'node:path'\nimport * as React from 'react'\nimport type {\n render as testingLibraryRender,\n renderHook as testingLibraryRenderHook,\n RenderHookOptions,\n} from '@testing-library/react-native'\nimport { configureRnwindState, resetRnwindState, transform as metroTransform } from '../metro'\nimport { __resetLookupCssState, lookupCss, registerAtoms } from '../runtime/lookup-css'\nimport type { Insets } from '../runtime/components/rnwind-provider'\nimport { RnwindProvider, useRnwind, useR_ } from '../runtime/components/rnwind-provider'\nimport { triggerHaptic, useMountHaptic } from '../runtime/haptics'\nimport type { OnHaptics } from '../core/parser/haptics'\nimport type { Scheme } from '../runtime/types'\n\n// ────────────────────────────────────────────────────────────────────────\n// Private constants\n// ────────────────────────────────────────────────────────────────────────\n\n/**\n * Minimal theme every `renderWithCss` call defaults to. Ships the two\n * conventional schemes so atoms carrying `dark:` / `light:` prefixes\n * resolve without the caller having to craft a theme CSS string.\n */\nconst DEFAULT_THEME_CSS = `@import 'tailwindcss';\n@layer theme {\n :root {\n @variant light { --color-bg: #ffffff; --color-fg: #0a0a0a; }\n @variant dark { --color-bg: #0a0a0a; --color-fg: #ffffff; }\n }\n}\n`\n\n/** `StyleSheet.create` stub the evaluated bundle calls — identity at test time. */\nconst BUNDLE_STYLE_SHEET = { create: <T>(styles: T): T => styles, hairlineWidth: 1 }\n\n/** Stubbed interactive-state hook return matching `useInteract()`. */\nconst NOOP_INTERACT = {\n state: undefined,\n onPressIn: (): void => undefined,\n onPressOut: (): void => undefined,\n onFocus: (): void => undefined,\n onBlur: (): void => undefined,\n}\n\n/**\n * Identity chain helper matching `chainPress` / `chainFocus`. Returns a\n * single callback that invokes every supplied handler in turn.\n * @param handlers Handlers to chain.\n * @returns Combined callback.\n */\nconst NOOP_CHAIN =\n (...handlers: ReadonlyArray<((...args: unknown[]) => unknown) | undefined>) =>\n (...args: unknown[]): void => {\n for (const handler of handlers) {\n if (typeof handler === 'function') handler(...args)\n }\n }\n\n// Synthesize a require rooted at the consumer's cwd so optional peer\n// lookups (`@testing-library/react-native`, `esbuild`) resolve from THEIR\n// node_modules, not rnwind's. A workspace-local rnwind install resolves\n// through a different node_modules chain than the test cwd — we want the\n// test's chain.\nconst localRequire: NodeRequire = createRequire(path.join(process.cwd(), 'package.json'))\n\nconst generate: typeof generateImport = (generateImport as { default?: typeof generateImport }).default ?? generateImport\n\n/**\n * Pre-loaded `@testing-library/react-native`. Resolved once at module\n * init (not lazily per-call) because the testing library registers\n * `beforeAll`/`afterEach` cleanup hooks on import — and Bun's test\n * runner refuses to register lifecycle hooks from inside a running\n * test body, which is where the first `renderWithCss` call lands.\n */\nconst TESTING_LIBRARY: {\n render: typeof testingLibraryRender\n renderHook: typeof testingLibraryRenderHook\n} = (() => {\n try {\n return localRequire('@testing-library/react-native') as {\n render: typeof testingLibraryRender\n renderHook: typeof testingLibraryRenderHook\n }\n } catch (error) {\n throw new Error(\n 'rnwind/testing: cannot load `@testing-library/react-native`. Add it to your dev dependencies. Underlying error: ' +\n (error instanceof Error ? error.message : String(error)),\n )\n }\n})()\n\n// ────────────────────────────────────────────────────────────────────────\n// Private helpers\n// ────────────────────────────────────────────────────────────────────────\n\n/**\n * Scheme string typed to match what `<RnwindProvider>` accepts. Users\n * who declare their own scheme names via `rnwind-types.d.ts` get them\n * through `Scheme`; everyone else gets `'light' | 'dark' | string`.\n */\ntype SchemeName = Scheme\n\n/**\n * Default lookup for the test process's `react-native` module. Users\n * who run with a mocked `react-native` (via Bun's `mock.module` or\n * Jest's `jest.mock`) get the mock here automatically.\n * @returns React Native namespace bindings.\n */\nfunction resolveReactNative(): Record<string, unknown> {\n return localRequire('react-native') as Record<string, unknown>\n}\n\n/**\n * Compile JSX + TypeScript source to plain JS. Prefers Bun's built-in\n * transpiler (fast, zero-dep); falls back to `esbuild` if running under\n * Node (Jest users). Throws with an install hint if neither is available.\n * @param source Source text to compile.\n * @returns JavaScript suitable for `new Function(...)` evaluation.\n */\nfunction compileToJs(source: string): string {\n const globalBun = (\n globalThis as { Bun?: { Transpiler: new (options: unknown) => { transformSync: (text: string) => string } } }\n ).Bun\n if (globalBun?.Transpiler) {\n return new globalBun.Transpiler({\n loader: 'tsx',\n tsconfig: JSON.stringify({ compilerOptions: { jsx: 'react', target: 'esnext' } }),\n }).transformSync(source)\n }\n try {\n const esbuild = localRequire('esbuild') as {\n transformSync: (text: string, options: { loader: string; jsx: string; target?: string }) => { code: string }\n }\n return esbuild.transformSync(source, { loader: 'tsx', jsx: 'transform', target: 'esnext' }).code\n } catch {\n throw new Error(\n 'rnwind/testing: cannot compile JSX. Run tests under Bun (which has a built-in transpiler) ' +\n 'or install `esbuild` as a dev dependency for Node / Jest setups.',\n )\n }\n}\n\n/**\n * Evaluate the union `style.js` the ledger wrote during the transform\n * so its `registerAtoms` call lands in the process-global registry.\n * Strips the `react-native` + `rnwind` imports from the source and\n * forwards local bindings into the evaluated closure.\n * @param cacheDir The rnwind cache dir where the ledger writes bundles.\n */\nfunction evaluateStyleBundle(cacheDir: string): void {\n const filePath = path.join(cacheDir, 'style.js')\n if (!existsSync(filePath)) return\n const body = readFileSync(filePath, 'utf8')\n .replaceAll(/import \\{ StyleSheet \\} from 'react-native'\\s*\\n/g, '')\n .replaceAll(/import \\{[^}]+\\} from 'rnwind'\\s*\\n/g, '')\n // Bundle body is generated by rnwind itself — not user-controlled.\n // eslint-disable-next-line sonarjs/code-eval\n new Function('StyleSheet', 'registerAtoms', body)(BUNDLE_STYLE_SHEET, registerAtoms)\n}\n\n/**\n * Evaluate the transformer's rewritten source as a standalone module:\n * strip synthetic imports, forward `import ... from 'rnwind'` +\n * `'react-native'` to local bindings, compile JSX via the compiler, and\n * capture the default export.\n * @param transformedSource Post-transformer source.\n * @param reactNative `react-native` namespace bindings to forward.\n * @returns The default-exported component.\n */\nfunction evaluateRewrittenModule(\n transformedSource: string,\n reactNative: Record<string, unknown>,\n): React.ComponentType<Record<string, unknown>> {\n const prepared = transformedSource\n .replaceAll(/import\\s+[\"']rnwind\\/__generated\\/[^\"']+[\"'];?\\s*\\n?/g, '')\n .replaceAll(/import\\s+\\{([^}]+)\\}\\s+from\\s+[\"']rnwind[\"'];?/g, 'const {$1} = __rnwind;')\n .replaceAll(/import\\s+\\{([^}]+)\\}\\s+from\\s+[\"']react-native[\"'];?/g, 'const {$1} = __reactNative;')\n .replace(/export\\s+default\\s+/, 'module.exports.default = ')\n\n const compiled = compileToJs(prepared)\n const rnwindEnv = {\n lookupCss,\n useRnwind,\n useR_,\n useMountHaptic,\n triggerHaptic,\n useInteract: () => NOOP_INTERACT,\n chainPress: NOOP_CHAIN,\n chainFocus: NOOP_CHAIN,\n }\n const moduleObject: { exports: { default?: React.ComponentType<Record<string, unknown>> } } = { exports: {} }\n // Compiled source originates from rnwind's own transformer + the JSX compiler.\n // eslint-disable-next-line sonarjs/code-eval\n new Function('React', '__rnwind', '__reactNative', 'module', compiled)(React, rnwindEnv, reactNative, moduleObject)\n if (!moduleObject.exports.default) {\n throw new Error('rnwind/testing: evaluated module did not export a default component.')\n }\n return moduleObject.exports.default\n}\n\n/**\n * Build the root element `@testing-library/react-native`'s `render`\n * receives. When a scheme is specified, wrap the component in a live\n * `<RnwindProvider>` so hooks like `useScheme()` and `useCss()` return\n * the requested scheme. Otherwise render bare (context falls back to the\n * default `'light'`).\n * @param Component Component to render.\n * @param scheme Optional active scheme override.\n * @param insets\n * @param onHaptics\n * @returns The React element to hand to `render`.\n */\nfunction buildRootElement(\n Component: React.ComponentType<Record<string, unknown>>,\n scheme: string | undefined,\n insets: Partial<Insets> | undefined,\n onHaptics: OnHaptics | undefined,\n): React.ReactElement {\n const child = React.createElement(Component)\n if (scheme === undefined && !insets && !onHaptics) return child\n const providerProps: { scheme: SchemeName; insets?: Partial<Insets>; onHaptics?: OnHaptics } = {\n scheme: (scheme ?? 'light') as SchemeName,\n }\n if (insets) providerProps.insets = insets\n if (onHaptics) providerProps.onHaptics = onHaptics\n return React.createElement(RnwindProvider, providerProps, child)\n}\n\n/**\n * Compose the `wrapper` option `renderHook` accepts. When a scheme is\n * set, wrap the hook's execution context in `<RnwindProvider>` so hooks\n * that read `useScheme()` see the requested scheme. If the user also\n * supplies their own wrapper, nest it inside the provider so both apply.\n * @param scheme Optional active scheme override.\n * @param userWrapper Optional user-supplied wrapper component.\n * @returns A wrapper component suitable for `renderHook`'s `wrapper` option.\n */\nfunction composeHookWrapper(\n scheme: string | undefined,\n userWrapper: React.ComponentType<{ children?: React.ReactNode }> | undefined,\n): React.ComponentType<{ children?: React.ReactNode }> | undefined {\n if (scheme === undefined && !userWrapper) return undefined\n return function RnwindTestWrapper({ children }: { children?: React.ReactNode }): React.ReactElement {\n const inner = userWrapper ? React.createElement(userWrapper, null, children) : (children as React.ReactElement)\n if (scheme === undefined) return inner\n return React.createElement(RnwindProvider, { scheme: scheme as SchemeName }, inner)\n }\n}\n\n/**\n * Spin up an ephemeral rnwind project and register the atoms produced\n * by `source` (when provided) into the runtime. Returns the project\n * state and the post-transform source so callers can render the\n * rewritten module or just rely on the registered atoms.\n *\n * Shared between `renderWithCss` (needs the rewritten component) and\n * `renderHookWithCss` (only needs the atoms in the registry).\n * @param options Options controlling the theme and pre-registered source.\n * @param options.themeCss Theme CSS override.\n * @param options.source Source whose Tailwind candidates should be registered.\n * @returns Paths plus the transformed source and a `cleanup` closure.\n */\nasync function bootstrapRnwindRuntime(options: { themeCss?: string; source?: string }): Promise<{\n projectRoot: string\n cacheDir: string\n transformedSource: string\n cleanup: () => void\n}> {\n const projectRoot = mkdtempSync(path.join(tmpdir(), 'rnwind-test-'))\n const cacheDir = path.join(projectRoot, '.rnwind-cache')\n const cssPath = path.join(projectRoot, 'theme.css')\n writeFileSync(cssPath, options.themeCss ?? DEFAULT_THEME_CSS)\n configureRnwindState(cssPath, cacheDir)\n\n let transformedSource = ''\n if (options.source !== undefined) {\n const filename = path.join(projectRoot, 'App.tsx')\n writeFileSync(filename, options.source)\n const ast: BabelFile = parse(options.source, { sourceType: 'module', plugins: ['typescript', 'jsx'] })\n const result = await metroTransform({ filename, src: options.source, options: { projectRoot }, ast })\n transformedSource = generate(result.ast).code\n evaluateStyleBundle(cacheDir)\n }\n\n const cleanup = (): void => {\n __resetLookupCssState()\n resetRnwindState()\n if (existsSync(projectRoot)) rmSync(projectRoot, { recursive: true, force: true })\n }\n\n return { projectRoot, cacheDir, transformedSource, cleanup }\n}\n\n/**\n * Synthesize a throwaway source file that mentions each className in a\n * JSX `className=\"...\"` attribute so the Metro transformer's scanner\n * picks them up and records their resolved styles into the union\n * `style.js`. Used by `renderHookWithCss` to pre-register atoms the\n * hook under test will look up.\n * @param classNames Class names to include.\n * @returns Source string suitable for `bootstrapRnwindRuntime`.\n */\nfunction sourceFromClassNames(classNames: readonly string[]): string {\n const joined = classNames.join(' ').replaceAll('\"', String.raw`\\\"`)\n return `const V: any = () => null\\nexport default () => <V className=\"${joined}\" />\\n`\n}\n\n// ────────────────────────────────────────────────────────────────────────\n// Public exports — types\n// ────────────────────────────────────────────────────────────────────────\n\n/** Exact signature of `@testing-library/react-native`'s `render`. */\nexport type Render = typeof testingLibraryRender\n\n/** Exact signature of `@testing-library/react-native`'s `renderHook`. */\nexport type RenderHook = typeof testingLibraryRenderHook\n\n/**\n * Exact return type of `@testing-library/react-native`'s `render` — we\n * re-export the upstream type so tests get every query (`getByTestId`,\n * `queryByText`, `rerender`, `unmount`, `debug`, `UNSAFE_root`, …) with\n * its real signature.\n */\nexport type TestingLibraryRenderAPI = ReturnType<typeof testingLibraryRender>\n\n/** Options accepted by {@link renderWithCss}. */\nexport interface RenderWithCssOptions {\n /** Theme CSS fed to rnwind's parser. Defaults to {@link DEFAULT_THEME_CSS}. */\n themeCss?: string\n /**\n * Active scheme the runtime resolver uses. Defaults to `'light'`.\n * Flip to `'dark'` / `'brand'` / ... to exercise scheme-variant atoms.\n */\n scheme?: string\n /**\n * Safe-area insets to inject. When provided, the component tree is\n * rendered under a `<RnwindProvider insets={...}>` so `*-safe` atoms\n * resolve to real numbers instead of the `0` fallback. Partial is\n * accepted — missing sides default to zero.\n */\n insets?: Partial<Insets>\n /**\n * Haptic dispatcher forwarded onto the implicit `<RnwindProvider>`.\n * Tests pass a spy callback to assert which haptic atoms fired.\n */\n onHaptics?: OnHaptics\n /**\n * `react-native` namespace bindings forwarded into the rewritten\n * module. Defaults to importing the test process's `react-native`\n * (typically a mocked stub). Override to inject custom host elements.\n */\n reactNative?: Record<string, unknown>\n}\n\n/**\n * Result of a {@link renderWithCss} call. Every key from\n * `@testing-library/react-native`'s `render` return value is spread onto\n * this object — `getByTestId`, `queryByText`, `rerender`, `unmount`,\n * `debug`, etc. — so tests use the testing-library API directly.\n *\n * Two rnwind-specific handles are added:\n * - `transformedSource` — the exact code the rnwind Metro transformer\n * emitted for your input. Log it to confirm the rewrite.\n * - `cleanup` — tears down the ephemeral rnwind state + chunk cache dir.\n * Call from `afterEach` so successive tests don't share atoms.\n */\nexport type RenderWithCssResult = TestingLibraryRenderAPI & {\n /** The post-transformer source text. Same code Metro would ship. */\n transformedSource: string\n /** Tear down the ephemeral rnwind state + cache dir. */\n cleanup: () => void\n}\n\n/** Options accepted by {@link renderHookWithCss}. */\nexport interface RenderHookWithCssOptions<Props> extends RenderHookOptions<Props> {\n /** Theme CSS fed to rnwind's parser. Defaults to the built-in minimal theme. */\n themeCss?: string\n /**\n * Active scheme the runtime resolver uses. Defaults to `'light'`. When\n * set, the hook is wrapped in `<RnwindProvider scheme={scheme}>` so\n * `useScheme()` / `useCss()` observe the override.\n */\n scheme?: string\n /**\n * Class names to pre-register into the runtime atom registry before\n * the hook runs. rnwind synthesizes a throwaway source file mentioning\n * these classes, feeds it through the real Metro transformer, and\n * evaluates the generated `style.js` bundle — so the hook sees\n * exactly the atoms the production bundle would register.\n */\n classNames?: readonly string[]\n}\n\n/**\n * Result of a {@link renderHookWithCss} call. Mirrors\n * `@testing-library/react-native`'s `renderHook` return type — `result`,\n * `rerender`, `unmount` — and adds a `cleanup` that tears down the\n * ephemeral rnwind state.\n */\nexport type RenderHookWithCssResult<Result, Props> = ReturnType<typeof testingLibraryRenderHook<Result, Props>> & {\n /** Tear down the ephemeral rnwind state + cache dir. */\n cleanup: () => void\n}\n\n// ────────────────────────────────────────────────────────────────────────\n// Public exports — functions\n// ────────────────────────────────────────────────────────────────────────\n\n/**\n * Feed a source string through rnwind's Metro transformer, evaluate\n * the generated `style.js` bundle, evaluate the rewritten module, and\n * render the default-exported component with\n * `@testing-library/react-native`.\n *\n * The rendered RN element's `style` prop is the EXACT array your\n * production bundle would attach — same transformer, same runtime\n * resolver, same atoms. Assert on `flatten(node.props.style)` to verify\n * the resolved values.\n * @example\n * ```tsx\n * const handle = await renderWithCss(\n * `import { View } from 'react-native'\n * export default () => <View className=\"bg-primary p-4\" testID=\"box\" />`,\n * { themeCss: `@import 'tailwindcss'; @theme { --color-primary: #6366f1; }` },\n * )\n * const flat = flatten(handle.getByTestId('box').props.style)\n * expect(flat.backgroundColor).toBe('#6366f1')\n * ```\n * @param source User source (one file) to transform + render.\n * @param options Optional theme override, scheme, and `react-native` bindings.\n * @returns Render queries + diagnostic handles.\n */\nexport async function renderWithCss(source: string, options: RenderWithCssOptions = {}): Promise<RenderWithCssResult> {\n const reactNative = options.reactNative ?? resolveReactNative()\n const {\n projectRoot: _projectRoot,\n transformedSource,\n cleanup,\n } = await bootstrapRnwindRuntime({\n themeCss: options.themeCss,\n source,\n })\n const Component = evaluateRewrittenModule(transformedSource, reactNative)\n const rendered = TESTING_LIBRARY.render(buildRootElement(Component, options.scheme, options.insets, options.onHaptics))\n return Object.assign({}, rendered, { transformedSource, cleanup })\n}\n\n/**\n * Render-side counterpart to {@link renderWithCss} for testing hooks in\n * isolation. Pre-registers atoms for the supplied `classNames`, wraps\n * the hook in an optional `<RnwindProvider>`, and forwards to\n * `@testing-library/react-native`'s `renderHook`.\n * @example\n * ```ts\n * const { result, cleanup } = await renderHookWithCss(\n * () => useCss('bg-primary'),\n * {\n * themeCss: `@import 'tailwindcss'; @theme { --color-primary: #6366f1; }`,\n * classNames: ['bg-primary'],\n * },\n * )\n * expect(flatten(result.current).backgroundColor).toBe('#6366f1')\n * cleanup()\n * ```\n * @param callback Hook body — same shape as `renderHook(callback)`.\n * @param options Theme, scheme, classNames to pre-register, plus\n * everything `renderHook` itself accepts (`initialProps`, `wrapper`).\n * @returns `renderHook`'s return value plus a `cleanup` function.\n */\nexport async function renderHookWithCss<Result, Props = unknown>(\n callback: (props: Props) => Result,\n options: RenderHookWithCssOptions<Props> = {},\n): Promise<RenderHookWithCssResult<Result, Props>> {\n const { cleanup } = await bootstrapRnwindRuntime({\n themeCss: options.themeCss,\n source: options.classNames && options.classNames.length > 0 ? sourceFromClassNames(options.classNames) : undefined,\n })\n const wrapper = composeHookWrapper(options.scheme, options.wrapper as RenderHookWithCssOptions<Props>['wrapper'])\n const { themeCss: _themeCss, scheme: _scheme, classNames: _classNames, ...hookOptions } = options\n const rendered = TESTING_LIBRARY.renderHook<Result, Props>(callback, {\n ...(hookOptions as RenderHookOptions<Props>),\n wrapper,\n })\n return Object.assign({}, rendered, { cleanup })\n}\n\n/**\n * Flatten a React Native style array (or single style object) into one\n * merged record. RN flattens left-to-right (later wins), so the returned\n * record is what the native view manager actually applies.\n * @param styles Style array, single object, or null/undefined.\n * @returns Flat style record.\n */\nexport function flatten(styles: unknown): Record<string, unknown> {\n if (styles == null) return {}\n if (Array.isArray(styles)) return Object.assign({}, ...styles.map((entry) => flatten(entry)))\n if (typeof styles === 'object') return styles as Record<string, unknown>\n return {}\n}\n"],"names":["generateImport","metroTransform"],"mappings":";;;;;;;;;;;;;AAsBA;AACA;AACA;AAEA;;;;AAIG;AACH,MAAM,iBAAiB,GAAG,CAAA;;;;;;;CAOzB;AAED;AACA,MAAM,kBAAkB,GAAG,EAAE,MAAM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,SAAU,OAAO,CAAC,MAAe,EAAA;IACrC,IAAI,MAAM,IAAI,IAAI;AAAE,QAAA,OAAO,EAAE;AAC7B,IAAA,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QAAE,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;IAC7F,IAAI,OAAO,MAAM,KAAK,QAAQ;AAAE,QAAA,OAAO,MAAiC;AACxE,IAAA,OAAO,EAAE;AACX;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../../../../src/testing/index.ts"],"sourcesContent":["/* eslint-disable sonarjs/no-unused-vars */\nimport type { File as BabelFile } from '@babel/types'\nimport { parse } from '@babel/parser'\nimport generateImport from '@babel/generator'\nimport { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'\nimport { createRequire } from 'node:module'\nimport { tmpdir } from 'node:os'\nimport path from 'node:path'\nimport * as React from 'react'\nimport type {\n render as testingLibraryRender,\n renderHook as testingLibraryRenderHook,\n RenderHookOptions,\n} from '@testing-library/react-native'\nimport { configureRnwindState, resetRnwindState, transform as metroTransform } from '../metro'\nimport {\n __resetLookupCssState,\n registerAtoms,\n registerBreakpoints,\n registerSchemeLoader,\n} from '../runtime/lookup-css'\nimport { __resetResolveState, registerGradients, registerHaptics, registerMolecules } from '../runtime/resolve'\nimport * as rnwindRuntime from '../runtime'\nimport type { Insets } from '../runtime/components/rnwind-provider'\nimport { RnwindProvider } from '../runtime/components/rnwind-provider'\nimport type { OnHaptics } from '../core/parser/haptics'\nimport type { Scheme } from '../runtime/types'\n\n// ────────────────────────────────────────────────────────────────────────\n// Private constants\n// ────────────────────────────────────────────────────────────────────────\n\n/**\n * Minimal theme every `renderWithCss` call defaults to. Ships the two\n * conventional schemes so atoms carrying `dark:` / `light:` prefixes\n * resolve without the caller having to craft a theme CSS string.\n */\nconst DEFAULT_THEME_CSS = `@import 'tailwindcss';\n@layer theme {\n :root {\n @variant light { --color-bg: #ffffff; --color-fg: #0a0a0a; }\n @variant dark { --color-bg: #0a0a0a; --color-fg: #ffffff; }\n }\n}\n`\n\n/** `StyleSheet.create` stub the evaluated bundle calls — identity at test time. */\nconst BUNDLE_STYLE_SHEET = { create: <T>(styles: T): T => styles, hairlineWidth: 1 }\n\n// Synthesize a require rooted at the consumer's cwd so optional peer\n// lookups (`@testing-library/react-native`, `esbuild`) resolve from THEIR\n// node_modules, not rnwind's. A workspace-local rnwind install resolves\n// through a different node_modules chain than the test cwd — we want the\n// test's chain.\nconst localRequire: NodeRequire = createRequire(path.join(process.cwd(), 'package.json'))\n\nconst generate: typeof generateImport = (generateImport as { default?: typeof generateImport }).default ?? generateImport\n\n/**\n * Pre-loaded `@testing-library/react-native`. Resolved once at module\n * init (not lazily per-call) because the testing library registers\n * `beforeAll`/`afterEach` cleanup hooks on import — and Bun's test\n * runner refuses to register lifecycle hooks from inside a running\n * test body, which is where the first `renderWithCss` call lands.\n */\nconst TESTING_LIBRARY: {\n render: typeof testingLibraryRender\n renderHook: typeof testingLibraryRenderHook\n} = (() => {\n try {\n return localRequire('@testing-library/react-native') as {\n render: typeof testingLibraryRender\n renderHook: typeof testingLibraryRenderHook\n }\n } catch (error) {\n throw new Error(\n 'rnwind/testing: cannot load `@testing-library/react-native`. Add it to your dev dependencies. Underlying error: ' +\n (error instanceof Error ? error.message : String(error)),\n )\n }\n})()\n\n// ────────────────────────────────────────────────────────────────────────\n// Private helpers\n// ────────────────────────────────────────────────────────────────────────\n\n/**\n * Scheme string typed to match what `<RnwindProvider>` accepts. Users\n * who declare their own scheme names via `rnwind-types.d.ts` get them\n * through `Scheme`; everyone else gets `'light' | 'dark' | string`.\n */\ntype SchemeName = Scheme\n\n/**\n * Default lookup for the test process's `react-native` module. Users\n * who run with a mocked `react-native` (via Bun's `mock.module` or\n * Jest's `jest.mock`) get the mock here automatically.\n * @returns React Native namespace bindings.\n */\nfunction resolveReactNative(): Record<string, unknown> {\n return localRequire('react-native') as Record<string, unknown>\n}\n\n/**\n * Compile JSX + TypeScript source to plain JS. Prefers Bun's built-in\n * transpiler (fast, zero-dep); falls back to `esbuild` if running under\n * Node (Jest users). Throws with an install hint if neither is available.\n * @param source Source text to compile.\n * @returns JavaScript suitable for `new Function(...)` evaluation.\n */\nfunction compileToJs(source: string): string {\n const globalBun = (\n globalThis as { Bun?: { Transpiler: new (options: unknown) => { transformSync: (text: string) => string } } }\n ).Bun\n if (globalBun?.Transpiler) {\n return new globalBun.Transpiler({\n loader: 'tsx',\n tsconfig: JSON.stringify({ compilerOptions: { jsx: 'react', target: 'esnext' } }),\n }).transformSync(source)\n }\n try {\n const esbuild = localRequire('esbuild') as {\n transformSync: (text: string, options: { loader: string; jsx: string; target?: string }) => { code: string }\n }\n return esbuild.transformSync(source, { loader: 'tsx', jsx: 'transform', target: 'esnext' }).code\n } catch {\n throw new Error(\n 'rnwind/testing: cannot compile JSX. Run tests under Bun (which has a built-in transpiler) ' +\n 'or install `esbuild` as a dev dependency for Node / Jest setups.',\n )\n }\n}\n\n/**\n * Evaluate one generated registry file (`common.style.js`,\n * `<variant>.style.js`, or `schemes.js`) so its `registerAtoms` /\n * `registerBreakpoints` / `registerGradients` / `registerHaptics` /\n * `registerSchemeLoader` calls land in the process-global registries the\n * runtime resolver reads. Imports (`'rnwind'`, `'react-native'`, relative\n * `./common.style`), `require(...)` loaders, and `export {...}` are\n * stripped — every scheme file is evaluated directly, so lazy loaders are\n * inert.\n * @param filePath Absolute path to the generated file.\n */\nfunction evaluateGeneratedFile(filePath: string): void {\n if (!existsSync(filePath)) return\n const body = readFileSync(filePath, 'utf8')\n .replaceAll(/import\\s+\\{[^}]*\\}\\s+from\\s+['\"][^'\"]+['\"];?\\s*\\n?/g, '')\n .replaceAll(/import\\s+['\"][^'\"]+['\"];?\\s*\\n?/g, '')\n .replaceAll(/export\\s+\\{[^}]*\\}\\s*;?\\s*\\n?/g, '')\n // Generated body is produced by rnwind itself — not user-controlled.\n // `require` is neutralised: each variant file is evaluated directly,\n // so the manifest's lazy `require('./x.style')` loaders are no-ops.\n // eslint-disable-next-line sonarjs/code-eval\n new Function(\n 'StyleSheet',\n 'registerAtoms',\n 'registerMolecules',\n 'registerBreakpoints',\n 'registerGradients',\n 'registerHaptics',\n 'registerSchemeLoader',\n 'require',\n body,\n )(\n BUNDLE_STYLE_SHEET,\n registerAtoms,\n registerMolecules,\n registerBreakpoints,\n registerGradients,\n registerHaptics,\n registerSchemeLoader,\n () => {},\n )\n}\n\n/**\n * Sort comparator that floats `common.style.js` to the front, rest alpha.\n * @param a\n * @param b\n */\nfunction commonFirst(a: string, b: string): number {\n if (a === 'common.style.js') return -1\n if (b === 'common.style.js') return 1\n return a.localeCompare(b)\n}\n\n/**\n * Evaluate every generated registry the transform wrote (`common` +\n * every variant scheme + the manifest) so the runtime resolver sees the\n * full atom / molecule / gradient / haptic registries — same state a\n * production bundle would hold once all scheme files load.\n * @param cacheDir The rnwind cache dir holding the generated files.\n */\nfunction evaluateGeneratedRegistries(cacheDir: string): void {\n if (!existsSync(cacheDir)) return\n const files = readdirSync(cacheDir).filter((name) => name.endsWith('.style.js'))\n // common first so variants layer their diffs on top of it.\n for (const name of files.toSorted(commonFirst)) {\n evaluateGeneratedFile(path.join(cacheDir, name))\n }\n evaluateGeneratedFile(path.join(cacheDir, 'schemes.js'))\n}\n\n/**\n * Move every `const {…} = __rnwind;` / `const {…} = __reactNative;`\n * binding line to the top of the source, preserving order, so they\n * initialise before the transformer's `const View = _rnwWrap(_rnw0)`\n * wrap declarations reference them. Mirrors ESM import hoisting.\n * @param source Source with imports already converted to const-destructures.\n * @returns Source with binding consts hoisted to the front.\n */\nfunction hoistBindingConsts(source: string): string {\n const hoisted: string[] = []\n const rest: string[] = []\n for (const line of source.split('\\n')) {\n if (/=\\s*__(?:rnwind|reactNative);\\s*$/.test(line)) hoisted.push(line)\n else rest.push(line)\n }\n return [...hoisted, ...rest].join('\\n')\n}\n\n/**\n * Evaluate the transformer's rewritten source as a standalone module:\n * strip synthetic generated imports, forward `import ... from 'rnwind'`\n * (incl. the injected `wrap as _rnwWrap`) and `'react-native'` to local\n * bindings, compile JSX via the compiler, and capture the default export.\n * Aliased named imports (`{ View as _rnw0 }`) are converted to valid\n * destructuring (`{ View: _rnw0 }`). The import-derived `const`s are then\n * hoisted above the body: the transformer injects `const View =\n * _rnwWrap(_rnw0)` ahead of the (real-ESM-hoisted) `import … as _rnw0`,\n * so without re-hoisting the binding the eval would hit a TDZ on `_rnw0`.\n * @param transformedSource Post-transformer source.\n * @param reactNative `react-native` namespace bindings to forward.\n * @returns The default-exported component.\n */\nfunction evaluateRewrittenModule(\n transformedSource: string,\n reactNative: Record<string, unknown>,\n): React.ComponentType<Record<string, unknown>> {\n const prepared = transformedSource\n .replaceAll(/import\\s+[\"']rnwind\\/__generated\\/[^\"']+[\"'];?\\s*\\n?/g, '')\n .replaceAll(/import\\s+\\{([^}]+)\\}\\s+from\\s+[\"']rnwind[\"'];?/g, (_m, spec: string) => `const {${spec.replaceAll(/\\bas\\b/g, ':')}} = __rnwind;`)\n .replaceAll(/import\\s+\\{([^}]+)\\}\\s+from\\s+[\"']react-native[\"'];?/g, (_m, spec: string) => `const {${spec.replaceAll(/\\bas\\b/g, ':')}} = __reactNative;`)\n .replace(/export\\s+default\\s+/, 'module.exports.default = ')\n\n const compiled = compileToJs(hoistBindingConsts(prepared))\n const moduleObject: { exports: { default?: React.ComponentType<Record<string, unknown>> } } = { exports: {} }\n // Compiled source originates from rnwind's own transformer + the JSX compiler.\n // eslint-disable-next-line sonarjs/code-eval\n new Function('React', '__rnwind', '__reactNative', 'module', compiled)(React, rnwindRuntime, reactNative, moduleObject)\n if (!moduleObject.exports.default) {\n throw new Error('rnwind/testing: evaluated module did not export a default component.')\n }\n return moduleObject.exports.default\n}\n\n/**\n * Build the root element `@testing-library/react-native`'s `render`\n * receives. When a scheme is specified, wrap the component in a live\n * `<RnwindProvider>` so hooks like `useScheme()` and `useCss()` return\n * the requested scheme. Otherwise render bare (context falls back to the\n * default `'light'`).\n * @param Component Component to render.\n * @param scheme Optional active scheme override.\n * @param insets\n * @param onHaptics\n * @returns The React element to hand to `render`.\n */\nfunction buildRootElement(\n Component: React.ComponentType<Record<string, unknown>>,\n scheme: string | undefined,\n insets: Partial<Insets> | undefined,\n onHaptics: OnHaptics | undefined,\n): React.ReactElement {\n const child = React.createElement(Component)\n if (scheme === undefined && !insets && !onHaptics) return child\n const providerProps: { scheme: SchemeName; insets?: Partial<Insets>; onHaptics?: OnHaptics } = {\n scheme: (scheme ?? 'light') as SchemeName,\n }\n if (insets) providerProps.insets = insets\n if (onHaptics) providerProps.onHaptics = onHaptics\n return React.createElement(RnwindProvider, providerProps, child)\n}\n\n/**\n * Compose the `wrapper` option `renderHook` accepts. When a scheme is\n * set, wrap the hook's execution context in `<RnwindProvider>` so hooks\n * that read `useScheme()` see the requested scheme. If the user also\n * supplies their own wrapper, nest it inside the provider so both apply.\n * @param scheme Optional active scheme override.\n * @param userWrapper Optional user-supplied wrapper component.\n * @returns A wrapper component suitable for `renderHook`'s `wrapper` option.\n */\nfunction composeHookWrapper(\n scheme: string | undefined,\n userWrapper: React.ComponentType<{ children?: React.ReactNode }> | undefined,\n): React.ComponentType<{ children?: React.ReactNode }> | undefined {\n if (scheme === undefined && !userWrapper) return undefined\n return function RnwindTestWrapper({ children }: { children?: React.ReactNode }): React.ReactElement {\n const inner = userWrapper ? React.createElement(userWrapper, null, children) : (children as React.ReactElement)\n if (scheme === undefined) return inner\n return React.createElement(RnwindProvider, { scheme: scheme as SchemeName }, inner)\n }\n}\n\n/**\n * Spin up an ephemeral rnwind project and register the atoms produced\n * by `source` (when provided) into the runtime. Returns the project\n * state and the post-transform source so callers can render the\n * rewritten module or just rely on the registered atoms.\n *\n * Shared between `renderWithCss` (needs the rewritten component) and\n * `renderHookWithCss` (only needs the atoms in the registry).\n * @param options Options controlling the theme and pre-registered source.\n * @param options.themeCss Theme CSS override.\n * @param options.source Source whose Tailwind candidates should be registered.\n * @returns Paths plus the transformed source and a `cleanup` closure.\n */\nasync function bootstrapRnwindRuntime(options: { themeCss?: string; source?: string }): Promise<{\n projectRoot: string\n cacheDir: string\n transformedSource: string\n cleanup: () => void\n}> {\n const projectRoot = mkdtempSync(path.join(tmpdir(), 'rnwind-test-'))\n const cacheDir = path.join(projectRoot, '.rnwind-cache')\n const cssPath = path.join(projectRoot, 'theme.css')\n writeFileSync(cssPath, options.themeCss ?? DEFAULT_THEME_CSS)\n configureRnwindState(cssPath, cacheDir)\n\n let transformedSource = ''\n if (options.source !== undefined) {\n const filename = path.join(projectRoot, 'App.tsx')\n writeFileSync(filename, options.source)\n const ast: BabelFile = parse(options.source, { sourceType: 'module', plugins: ['typescript', 'jsx'] })\n const result = await metroTransform({ filename, src: options.source, options: { projectRoot }, ast })\n transformedSource = generate(result.ast).code\n evaluateGeneratedRegistries(cacheDir)\n }\n\n const cleanup = (): void => {\n __resetLookupCssState()\n __resetResolveState()\n resetRnwindState()\n if (existsSync(projectRoot)) rmSync(projectRoot, { recursive: true, force: true })\n }\n\n return { projectRoot, cacheDir, transformedSource, cleanup }\n}\n\n/**\n * Synthesize a throwaway source file that mentions each className in a\n * JSX `className=\"...\"` attribute so the Metro transformer's scanner\n * picks them up and records their resolved styles into the union\n * `style.js`. Used by `renderHookWithCss` to pre-register atoms the\n * hook under test will look up.\n * @param classNames Class names to include.\n * @returns Source string suitable for `bootstrapRnwindRuntime`.\n */\nfunction sourceFromClassNames(classNames: readonly string[]): string {\n const joined = classNames.join(' ').replaceAll('\"', String.raw`\\\"`)\n return `const V: any = () => null\\nexport default () => <V className=\"${joined}\" />\\n`\n}\n\n// ────────────────────────────────────────────────────────────────────────\n// Public exports — types\n// ────────────────────────────────────────────────────────────────────────\n\n/** Exact signature of `@testing-library/react-native`'s `render`. */\nexport type Render = typeof testingLibraryRender\n\n/** Exact signature of `@testing-library/react-native`'s `renderHook`. */\nexport type RenderHook = typeof testingLibraryRenderHook\n\n/**\n * Exact return type of `@testing-library/react-native`'s `render` — we\n * re-export the upstream type so tests get every query (`getByTestId`,\n * `queryByText`, `rerender`, `unmount`, `debug`, `UNSAFE_root`, …) with\n * its real signature.\n */\nexport type TestingLibraryRenderAPI = ReturnType<typeof testingLibraryRender>\n\n/** Options accepted by {@link renderWithCss}. */\nexport interface RenderWithCssOptions {\n /** Theme CSS fed to rnwind's parser. Defaults to {@link DEFAULT_THEME_CSS}. */\n themeCss?: string\n /**\n * Active scheme the runtime resolver uses. Defaults to `'light'`.\n * Flip to `'dark'` / `'brand'` / ... to exercise scheme-variant atoms.\n */\n scheme?: string\n /**\n * Safe-area insets to inject. When provided, the component tree is\n * rendered under a `<RnwindProvider insets={...}>` so `*-safe` atoms\n * resolve to real numbers instead of the `0` fallback. Partial is\n * accepted — missing sides default to zero.\n */\n insets?: Partial<Insets>\n /**\n * Haptic dispatcher forwarded onto the implicit `<RnwindProvider>`.\n * Tests pass a spy callback to assert which haptic atoms fired.\n */\n onHaptics?: OnHaptics\n /**\n * `react-native` namespace bindings forwarded into the rewritten\n * module. Defaults to importing the test process's `react-native`\n * (typically a mocked stub). Override to inject custom host elements.\n */\n reactNative?: Record<string, unknown>\n}\n\n/**\n * Result of a {@link renderWithCss} call. Every key from\n * `@testing-library/react-native`'s `render` return value is spread onto\n * this object — `getByTestId`, `queryByText`, `rerender`, `unmount`,\n * `debug`, etc. — so tests use the testing-library API directly.\n *\n * Two rnwind-specific handles are added:\n * - `transformedSource` — the exact code the rnwind Metro transformer\n * emitted for your input. Log it to confirm the rewrite.\n * - `cleanup` — tears down the ephemeral rnwind state + chunk cache dir.\n * Call from `afterEach` so successive tests don't share atoms.\n */\nexport type RenderWithCssResult = TestingLibraryRenderAPI & {\n /** The post-transformer source text. Same code Metro would ship. */\n transformedSource: string\n /** Tear down the ephemeral rnwind state + cache dir. */\n cleanup: () => void\n}\n\n/** Options accepted by {@link renderHookWithCss}. */\nexport interface RenderHookWithCssOptions<Props> extends RenderHookOptions<Props> {\n /** Theme CSS fed to rnwind's parser. Defaults to the built-in minimal theme. */\n themeCss?: string\n /**\n * Active scheme the runtime resolver uses. Defaults to `'light'`. When\n * set, the hook is wrapped in `<RnwindProvider scheme={scheme}>` so\n * `useScheme()` / `useCss()` observe the override.\n */\n scheme?: string\n /**\n * Class names to pre-register into the runtime atom registry before\n * the hook runs. rnwind synthesizes a throwaway source file mentioning\n * these classes, feeds it through the real Metro transformer, and\n * evaluates the generated `style.js` bundle — so the hook sees\n * exactly the atoms the production bundle would register.\n */\n classNames?: readonly string[]\n}\n\n/**\n * Result of a {@link renderHookWithCss} call. Mirrors\n * `@testing-library/react-native`'s `renderHook` return type — `result`,\n * `rerender`, `unmount` — and adds a `cleanup` that tears down the\n * ephemeral rnwind state.\n */\nexport type RenderHookWithCssResult<Result, Props> = ReturnType<typeof testingLibraryRenderHook<Result, Props>> & {\n /** Tear down the ephemeral rnwind state + cache dir. */\n cleanup: () => void\n}\n\n// ────────────────────────────────────────────────────────────────────────\n// Public exports — functions\n// ────────────────────────────────────────────────────────────────────────\n\n/**\n * Feed a source string through rnwind's Metro transformer, evaluate\n * the generated `style.js` bundle, evaluate the rewritten module, and\n * render the default-exported component with\n * `@testing-library/react-native`.\n *\n * The rendered RN element's `style` prop is the EXACT array your\n * production bundle would attach — same transformer, same runtime\n * resolver, same atoms. Assert on `flatten(node.props.style)` to verify\n * the resolved values.\n * @example\n * ```tsx\n * const handle = await renderWithCss(\n * `import { View } from 'react-native'\n * export default () => <View className=\"bg-primary p-4\" testID=\"box\" />`,\n * { themeCss: `@import 'tailwindcss'; @theme { --color-primary: #6366f1; }` },\n * )\n * const flat = flatten(handle.getByTestId('box').props.style)\n * expect(flat.backgroundColor).toBe('#6366f1')\n * ```\n * @param source User source (one file) to transform + render.\n * @param options Optional theme override, scheme, and `react-native` bindings.\n * @returns Render queries + diagnostic handles.\n */\nexport async function renderWithCss(source: string, options: RenderWithCssOptions = {}): Promise<RenderWithCssResult> {\n const reactNative = options.reactNative ?? resolveReactNative()\n const {\n projectRoot: _projectRoot,\n transformedSource,\n cleanup,\n } = await bootstrapRnwindRuntime({\n themeCss: options.themeCss,\n source,\n })\n const Component = evaluateRewrittenModule(transformedSource, reactNative)\n const rendered = TESTING_LIBRARY.render(buildRootElement(Component, options.scheme, options.insets, options.onHaptics))\n return Object.assign({}, rendered, { transformedSource, cleanup })\n}\n\n/**\n * Render-side counterpart to {@link renderWithCss} for testing hooks in\n * isolation. Pre-registers atoms for the supplied `classNames`, wraps\n * the hook in an optional `<RnwindProvider>`, and forwards to\n * `@testing-library/react-native`'s `renderHook`.\n * @example\n * ```ts\n * const { result, cleanup } = await renderHookWithCss(\n * () => useCss('bg-primary'),\n * {\n * themeCss: `@import 'tailwindcss'; @theme { --color-primary: #6366f1; }`,\n * classNames: ['bg-primary'],\n * },\n * )\n * expect(flatten(result.current).backgroundColor).toBe('#6366f1')\n * cleanup()\n * ```\n * @param callback Hook body — same shape as `renderHook(callback)`.\n * @param options Theme, scheme, classNames to pre-register, plus\n * everything `renderHook` itself accepts (`initialProps`, `wrapper`).\n * @returns `renderHook`'s return value plus a `cleanup` function.\n */\nexport async function renderHookWithCss<Result, Props = unknown>(\n callback: (props: Props) => Result,\n options: RenderHookWithCssOptions<Props> = {},\n): Promise<RenderHookWithCssResult<Result, Props>> {\n const { cleanup } = await bootstrapRnwindRuntime({\n themeCss: options.themeCss,\n source: options.classNames && options.classNames.length > 0 ? sourceFromClassNames(options.classNames) : undefined,\n })\n const wrapper = composeHookWrapper(options.scheme, options.wrapper as RenderHookWithCssOptions<Props>['wrapper'])\n const { themeCss: _themeCss, scheme: _scheme, classNames: _classNames, ...hookOptions } = options\n const rendered = TESTING_LIBRARY.renderHook<Result, Props>(callback, {\n ...(hookOptions as RenderHookOptions<Props>),\n wrapper,\n })\n return Object.assign({}, rendered, { cleanup })\n}\n\n/**\n * Flatten a React Native style array (or single style object) into one\n * merged record. RN flattens left-to-right (later wins), so the returned\n * record is what the native view manager actually applies.\n * @param styles Style array, single object, or null/undefined.\n * @returns Flat style record.\n */\nexport function flatten(styles: unknown): Record<string, unknown> {\n if (styles == null) return {}\n if (Array.isArray(styles)) return Object.assign({}, ...styles.map((entry) => flatten(entry)))\n if (typeof styles === 'object') return styles as Record<string, unknown>\n return {}\n}\n"],"names":["generateImport","rnwindRuntime","metroTransform"],"mappings":";;;;;;;;;;;;;;AA4BA;AACA;AACA;AAEA;;;;AAIG;AACH,MAAM,iBAAiB,GAAG,CAAA;;;;;;;CAOzB;AAED;AACA,MAAM,kBAAkB,GAAG,EAAE,MAAM,EAAE,CAAI,MAAS,KAAQ,MAAM,EAAE,aAAa,EAAE,CAAC,EAAE;AAEpF;AACA;AACA;AACA;AACA;AACA,MAAM,YAAY,GAAgB,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,cAAc,CAAC,CAAC;AAEzF,MAAM,QAAQ,GAA2BA,UAAsD,CAAC,OAAO,IAAIA,UAAc;AAEzH;;;;;;AAMG;AACH,MAAM,eAAe,GAGjB,CAAC,MAAK;AACR,IAAA,IAAI;AACF,QAAA,OAAO,YAAY,CAAC,+BAA+B,CAGlD;IACH;IAAE,OAAO,KAAK,EAAE;QACd,MAAM,IAAI,KAAK,CACb,kHAAkH;AAChH,aAAC,KAAK,YAAY,KAAK,GAAG,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAC3D;IACH;AACF,CAAC,GAAG;AAaJ;;;;;AAKG;AACH,SAAS,kBAAkB,GAAA;AACzB,IAAA,OAAO,YAAY,CAAC,cAAc,CAA4B;AAChE;AAEA;;;;;;AAMG;AACH,SAAS,WAAW,CAAC,MAAc,EAAA;AACjC,IAAA,MAAM,SAAS,GACb,UACD,CAAC,GAAG;AACL,IAAA,IAAI,SAAS,EAAE,UAAU,EAAE;AACzB,QAAA,OAAO,IAAI,SAAS,CAAC,UAAU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AO,EAAE,CAAC;AACjD;AAEA;;;;;;AAMG;AACG,SAAU,OAAO,CAAC,MAAe,EAAA;IACrC,IAAI,MAAM,IAAI,IAAI;AAAE,QAAA,OAAO,EAAE;AAC7B,IAAA,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QAAE,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;IAC7F,IAAI,OAAO,MAAM,KAAK,QAAQ;AAAE,QAAA,OAAO,MAAiC;AACxE,IAAA,OAAO,EAAE;AACX;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rnwind",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Tailwind for React Native",
5
5
  "author": "https://github.com/sagltd",
6
6
  "license": "MIT",
@@ -56,6 +56,7 @@
56
56
  ],
57
57
  "scripts": {
58
58
  "build": "rollup -c rollup.config.mjs",
59
+ "bench": "bun run bench/resolve.bench.ts",
59
60
  "test": "bun test ./__tests__",
60
61
  "test:coverage": "bun test ./__tests__ --coverage",
61
62
  "typecheck": "tsc --noEmit",
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Normalize a className for molecule keying: trim, collapse runs of
3
+ * whitespace, and drop exact-duplicate tokens — but PRESERVE ORDER.
4
+ * Tailwind is last-wins for conflicting utilities (`p-4 p-2` ≠ `p-2 p-4`),
5
+ * so sorting would corrupt the merge. Build-time (molecule keys) and
6
+ * runtime (lookup) call the identical function so their keys always match.
7
+ * @param className Raw className string.
8
+ * @returns Normalized, order-preserving className.
9
+ */
10
+ export function normalizeClassName(className: string): string {
11
+ const seen = new Set<string>()
12
+ const out: string[] = []
13
+ for (const token of className.trim().split(/\s+/)) {
14
+ if (token.length === 0 || seen.has(token)) continue
15
+ seen.add(token)
16
+ out.push(token)
17
+ }
18
+ return out.join(' ')
19
+ }
@@ -1,4 +1,5 @@
1
1
  import type { KeyframeBlock, RNStyle, SchemedStyle } from '../parser'
2
+ import { normalizeClassName } from '../normalize-classname'
2
3
 
3
4
  /** Match atom names like `border-hairline`, `h-hairline`, `border-t-hairline`, etc. */
4
5
  const HAIRLINE_ATOM = /-hairline$/
@@ -179,61 +180,96 @@ function prepareAtomValue(atomName: string, style: RNStyle, keyframes: ReadonlyM
179
180
  }
180
181
 
181
182
  /**
182
- * Per-file value deduplicator interns each unique serialized atom
183
- * value once and emits `const _s<N> = <value>` at module scope. Scheme
184
- * entries reference the const instead of inlining the literal.
185
- *
186
- * Wins: (1) smaller bundle bytes for themes with many atoms sharing a
187
- * style shape; (2) stable `===` inside one scheme so two atoms
188
- * resolving to the same value yield the same object reference.
183
+ * Decide which serialized atom values get hoisted to a shared `const`.
184
+ * A value is hoisted ONLY when ≥2 atoms share it then one
185
+ * `const _s<N> = <value>` saves the repeated bytes AND gives those atoms
186
+ * one shared object (reference identity). A value used once is inlined
187
+ * directly at its atom (`"-m-2": {"margin":-8}`) hoisting a singleton
188
+ * would only add bytes. First-seen order keeps the const indices stable
189
+ * across workers.
190
+ * @param entries `[atomName, serializedValue]` pairs (atom-sorted).
191
+ * @returns `{ constFor }` value→const-name map + `decls` source lines.
189
192
  */
190
- class ValueDeduper {
191
- private readonly byText = new Map<string, string>()
192
- private readonly decls: string[] = []
193
-
194
- intern(serialized: string): string {
195
- const existing = this.byText.get(serialized)
196
- if (existing) return existing
197
- const name = `_s${this.decls.length}`
198
- this.decls.push(`const ${name} = ${serialized}`)
199
- this.byText.set(serialized, name)
200
- return name
193
+ function planValueConsts(entries: readonly (readonly [string, string])[]): {
194
+ constFor: ReadonlyMap<string, string>
195
+ decls: readonly string[]
196
+ } {
197
+ const counts = new Map<string, number>()
198
+ for (const [, value] of entries) counts.set(value, (counts.get(value) ?? 0) + 1)
199
+ const constFor = new Map<string, string>()
200
+ const decls: string[] = []
201
+ for (const [value, count] of counts) {
202
+ if (count < 2) continue
203
+ const name = `_s${decls.length}`
204
+ constFor.set(value, name)
205
+ decls.push(`const ${name} = ${value}`)
201
206
  }
207
+ return { constFor, decls }
208
+ }
202
209
 
203
- get declarations(): readonly string[] {
204
- return this.decls
205
- }
210
+ /**
211
+ * Serialize a scheme's molecule map into a `registerMolecules(...)` object
212
+ * literal, sorted by className for byte-deterministic output.
213
+ * @param molecules normalized className → pre-merged style object.
214
+ * @returns Object-literal source (`null` when empty).
215
+ */
216
+ function serializeMolecules(molecules: Record<string, RNStyle> | undefined): string | null {
217
+ if (!molecules) return null
218
+ const keys = Object.keys(molecules).toSorted((a, b) => a.localeCompare(b))
219
+ if (keys.length === 0) return null
220
+ const body = keys.map((cn) => ` ${JSON.stringify(cn)}: ${JSON.stringify(molecules[cn])},`)
221
+ return ['{', ...body, '}'].join('\n')
206
222
  }
207
223
 
208
224
  /**
209
225
  * Render one scheme file's source. `entries` is the list of atoms this
210
226
  * scheme contributes — for `common` every atom's canonical value; for
211
227
  * a variant only atoms whose value differs from canonical. Hairline
212
- * atoms in this file trigger the `StyleSheet` import.
228
+ * atoms in this file trigger the `StyleSheet` import. Pre-merged
229
+ * molecules (when present) are registered alongside the atoms so the
230
+ * runtime resolver's molecule-first path is populated.
213
231
  * @param schemeName Registry key (`'common'` or the variant name).
214
232
  * @param entries `[atomName, serializedValue]` pairs to emit.
233
+ * @param molecules Pre-merged className → style map for this scheme.
215
234
  * @returns JS source text.
216
235
  */
217
- function renderSchemeFile(schemeName: string, entries: readonly (readonly [string, string])[]): string {
236
+ function renderSchemeFile(
237
+ schemeName: string,
238
+ entries: readonly (readonly [string, string])[],
239
+ molecules?: Record<string, RNStyle>,
240
+ ): string {
218
241
  const needsStyleSheet = entries.some(([atom]) => isHairlineAtom(atom))
219
- const deduper = new ValueDeduper()
220
- const recordLines: string[] = []
221
- for (const [atom, value] of entries) {
222
- const ref = deduper.intern(value)
223
- recordLines.push(` ${JSON.stringify(atom)}: ${ref},`)
224
- }
242
+ const { constFor, decls } = planValueConsts(entries)
243
+ const recordLines = entries.map(([atom, value]) => ` ${JSON.stringify(atom)}: ${constFor.get(value) ?? value},`)
244
+ const moleculeLiteral = serializeMolecules(molecules)
225
245
 
246
+ const imports = ['registerAtoms']
247
+ if (moleculeLiteral) imports.push('registerMolecules')
226
248
  const lines: string[] = []
227
249
  if (needsStyleSheet) lines.push(`import { StyleSheet } from 'react-native'`)
228
- lines.push(`import { registerAtoms } from 'rnwind'`, ``)
229
- if (deduper.declarations.length > 0) {
230
- for (const decl of deduper.declarations) lines.push(decl)
250
+ lines.push(`import { ${imports.join(', ')} } from 'rnwind'`, ``)
251
+ if (decls.length > 0) {
252
+ for (const decl of decls) lines.push(decl)
231
253
  lines.push(``)
232
254
  }
233
255
  lines.push(`registerAtoms(${JSON.stringify(schemeName)}, {`, ...recordLines, `})`, ``)
256
+ if (moleculeLiteral) lines.push(`registerMolecules(${JSON.stringify(schemeName)}, ${moleculeLiteral})`, ``)
234
257
  return lines.join('\n')
235
258
  }
236
259
 
260
+ /**
261
+ * Serialize a feature map (atom name → JSON-able value: gradient info or
262
+ * haptic request) into a stable JS object literal for the manifest.
263
+ * Sorted by key so the output is byte-deterministic across workers.
264
+ * @param map Atom name → feature value.
265
+ * @returns Object-literal source.
266
+ */
267
+ function serializeFeatureMap(map: ReadonlyMap<string, unknown>): string {
268
+ const entries = [...map.entries()].toSorted((a, b) => a[0].localeCompare(b[0]))
269
+ const body = entries.map(([key, value]) => `${JSON.stringify(key)}: ${JSON.stringify(value)}`).join(', ')
270
+ return `{ ${body} }`
271
+ }
272
+
237
273
  /**
238
274
  * Render the JS-object literal for the responsive-breakpoint table the
239
275
  * runtime registers at manifest-load time. Sorted by ascending px
@@ -250,35 +286,215 @@ function serializeBreakpoints(breakpoints: ReadonlyMap<string, number>): string
250
286
  }
251
287
 
252
288
  /**
253
- * Render the manifest module. Eager-imports `common.style.js` (every
254
- * rewritten source file pulls this via a transitive side-effect
255
- * import), registers the responsive-breakpoint table once, and lazy-
256
- * requires every variant scheme's file through an inline require —
257
- * first call in `ensureSchemeLoaded(name)` triggers the scheme
258
- * module's evaluation; Metro's module cache makes subsequent calls
259
- * no-ops.
289
+ * Render the manifest module. EAGER-imports `common.style.js` AND every
290
+ * variant scheme file so every scheme's atoms register the moment the
291
+ * manifest evaluates no lazy `require`. Lazy loading raced the cold
292
+ * start: `RnwindProvider` calls `loadScheme(scheme)` on its first render,
293
+ * but on a cold boot the manifest (hence `registerSchemeLoader`) may not
294
+ * have evaluated yet, so that call no-ops and the active variant's atoms
295
+ * never load — scheme-dependent styles fall back to `common` (the light
296
+ * default) until a reload. Eager imports remove the race entirely; the
297
+ * variant files are small diffs, so the upfront cost is negligible.
298
+ * `ensureSchemeLoaded` stays exported as a no-op for API compatibility.
260
299
  * @param variants Variant scheme names (no `base`, no `common`).
261
300
  * @param breakpoints Responsive breakpoint name → px-threshold map.
301
+ * @param gradients Atom → gradient info for `registerGradients`.
302
+ * @param haptics Atom → haptic request for `registerHaptics`.
262
303
  * @returns JS source text.
263
304
  */
264
- function renderManifest(variants: readonly string[], breakpoints: ReadonlyMap<string, number>): string {
265
- const lines: string[] = [
266
- `import { registerSchemeLoader, registerBreakpoints } from 'rnwind'`,
267
- `import './common.style'`,
305
+ function renderManifest(
306
+ variants: readonly string[],
307
+ breakpoints: ReadonlyMap<string, number>,
308
+ gradients: ReadonlyMap<string, unknown>,
309
+ haptics: ReadonlyMap<string, unknown>,
310
+ ): string {
311
+ const imports = ['registerSchemeLoader', 'registerBreakpoints']
312
+ if (gradients.size > 0) imports.push('registerGradients')
313
+ if (haptics.size > 0) imports.push('registerHaptics')
314
+ const lines: string[] = [`import { ${imports.join(', ')} } from 'rnwind'`, `import './common.style'`]
315
+ for (const variant of variants) lines.push(`import ${JSON.stringify(`./${variant}.style`)}`)
316
+ lines.push(``, `registerBreakpoints(${serializeBreakpoints(breakpoints)})`)
317
+ if (gradients.size > 0) lines.push(`registerGradients(${serializeFeatureMap(gradients)})`)
318
+ if (haptics.size > 0) lines.push(`registerHaptics(${serializeFeatureMap(haptics)})`)
319
+ lines.push(
320
+ ``,
321
+ `function ensureSchemeLoaded(_name) {}`,
268
322
  ``,
269
- `registerBreakpoints(${serializeBreakpoints(breakpoints)})`,
323
+ `registerSchemeLoader(ensureSchemeLoaded)`,
270
324
  ``,
271
- ]
272
- if (variants.length === 0) {
273
- lines.push(`function ensureSchemeLoaded(_name) {}`, ``, `registerSchemeLoader(ensureSchemeLoaded)`, ``, `export { ensureSchemeLoaded }`, ``)
274
- return lines.join('\n')
325
+ `export { ensureSchemeLoaded }`,
326
+ ``,
327
+ )
328
+ return lines.join('\n')
329
+ }
330
+
331
+ /**
332
+ * Whether a resolved style carries a nested safe-area marker — molecules
333
+ * can't pre-bake these because the inset value is per-render.
334
+ * @param style Raw resolved RN style (pre-envelope).
335
+ * @returns True when any value is a `{__safe: ...}` marker.
336
+ */
337
+ function hasSafeMarker(style: RNStyle): boolean {
338
+ for (const key of Object.keys(style)) {
339
+ const value = style[key]
340
+ if (typeof value !== 'object' || !value) continue
341
+ if ('__safe' in value) return true
275
342
  }
276
- lines.push(`const LOADERS = {`)
343
+ return false
344
+ }
345
+
346
+ /**
347
+ * Whether a resolved style has font-scale-sensitive props. Molecules
348
+ * can't pre-bake these because `fontSize`/`lineHeight` scale per-render
349
+ * with `useWindowDimensions().fontScale`.
350
+ * @param style Resolved RN style.
351
+ * @returns True when `fontSize` or `lineHeight` is present.
352
+ */
353
+ function hasFontScaleProperty(style: RNStyle): boolean {
354
+ return 'fontSize' in style || 'lineHeight' in style
355
+ }
356
+
357
+ /**
358
+ * Whether a token is a feature-only utility (gradient stop/direction,
359
+ * haptic, or text-truncate) that contributes NO RN `style` — the runtime
360
+ * resolver folds these in via `attachFeatures`, so they don't disqualify
361
+ * a molecule, they just merge nothing.
362
+ * @param token Atom name.
363
+ * @param gradients Gradient feature map.
364
+ * @param haptics Haptic feature map.
365
+ * @returns True when the token is a non-style feature.
366
+ */
367
+ function isFeatureToken(token: string, gradients: ReadonlyMap<string, unknown>, haptics: ReadonlyMap<string, unknown>): boolean {
368
+ if (gradients.has(token) || haptics.has(token)) return true
369
+ return token === 'truncate' || token === 'text-ellipsis' || token === 'text-clip' || token.startsWith('line-clamp-')
370
+ }
371
+
372
+ /**
373
+ * Resolve one atom's value under a scheme: the scheme's own non-empty
374
+ * bucket, falling back to canonical. `common` always reads canonical.
375
+ * @param schemed Parser-produced per-scheme bucket.
376
+ * @param scheme Scheme key (`'common'` or a variant name).
377
+ * @returns The atom's RN style for that scheme, or undefined.
378
+ */
379
+ function schemeValueOf(schemed: SchemedStyle, scheme: string): RNStyle | undefined {
380
+ if (scheme === COMMON_SCHEME) return canonicalValue(schemed)
381
+ const own = (schemed as Readonly<Record<string, RNStyle>>)[scheme]
382
+ return isNonEmptyStyle(own) ? own : canonicalValue(schemed)
383
+ }
384
+
385
+ /**
386
+ * Pre-merge a normalized className's atoms into ONE RN style object for a
387
+ * scheme, or null when the className is NOT molecule-eligible. A
388
+ * className is eligible only when every token is context-independent:
389
+ * - no variant prefix (`active:` / `focus:` / `md:` / `dark:` — anything
390
+ * with a `:`), so scheme/state/breakpoint gating never applies,
391
+ * - no `*-hairline`, `*-safe`, or font-scale (`fontSize`/`lineHeight`)
392
+ * atom, whose value is resolved per-render.
393
+ * Feature-only tokens (gradient / haptic / truncate) are skipped, not
394
+ * disqualifying — the runtime folds them in via `attachFeatures`. Unknown
395
+ * tokens disqualify so the atom path still surfaces the dev warning.
396
+ * @param tokens Normalized className tokens (order preserved).
397
+ * @param scheme Scheme key to resolve each atom under.
398
+ * @param resolved Per-atom schemed styles.
399
+ * @param keyframes Keyframes to inline into `animationName`.
400
+ * @param gradients Gradient feature map.
401
+ * @param haptics Haptic feature map.
402
+ * @returns Merged style object, or null when not eligible.
403
+ */
404
+ function mergeMolecule(
405
+ tokens: readonly string[],
406
+ scheme: string,
407
+ resolved: ReadonlyMap<string, SchemedStyle>,
408
+ keyframes: ReadonlyMap<string, KeyframeBlock>,
409
+ gradients: ReadonlyMap<string, unknown>,
410
+ haptics: ReadonlyMap<string, unknown>,
411
+ ): RNStyle | null {
412
+ const merged: RNStyle = {}
413
+ for (const token of tokens) {
414
+ if (token.includes(':')) return null
415
+ if (isFeatureToken(token, gradients, haptics)) continue
416
+ if (isHairlineAtom(token)) return null
417
+ const schemed = resolved.get(token)
418
+ if (!schemed) return null
419
+ const raw = schemeValueOf(schemed, scheme)
420
+ if (!raw) continue
421
+ if (hasSafeMarker(raw) || hasFontScaleProperty(raw)) return null
422
+ Object.assign(merged, inlineAnimationName(raw, keyframes))
423
+ }
424
+ return merged
425
+ }
426
+
427
+ /**
428
+ * Emit each variant's molecule for one className — but only when the
429
+ * variant's merge DIFFERS from common (runtime falls back to common).
430
+ * @param normalized Normalized className key.
431
+ * @param tokens Normalized className tokens.
432
+ * @param commonText Serialized common-scheme merge for the diff check.
433
+ * @param variants Variant scheme names.
434
+ * @param variantMaps Mutable per-variant molecule collectors.
435
+ * @param resolved Per-atom schemed styles.
436
+ * @param keyframes Keyframes to inline.
437
+ * @param gradients Gradient feature map.
438
+ * @param haptics Haptic feature map.
439
+ */
440
+ function addVariantMolecules(
441
+ normalized: string,
442
+ tokens: readonly string[],
443
+ commonText: string,
444
+ variants: readonly string[],
445
+ variantMaps: Record<string, Record<string, RNStyle>>,
446
+ resolved: ReadonlyMap<string, SchemedStyle>,
447
+ keyframes: ReadonlyMap<string, KeyframeBlock>,
448
+ gradients: ReadonlyMap<string, unknown>,
449
+ haptics: ReadonlyMap<string, unknown>,
450
+ ): void {
277
451
  for (const variant of variants) {
278
- lines.push(` ${JSON.stringify(variant)}: () => require(${JSON.stringify(`./${variant}.style`)}),`)
452
+ const variantMerged = mergeMolecule(tokens, variant, resolved, keyframes, gradients, haptics)
453
+ if (variantMerged === null) continue
454
+ if (JSON.stringify(variantMerged) !== commonText) variantMaps[variant][normalized] = variantMerged
279
455
  }
280
- lines.push(`}`, ``, `function ensureSchemeLoaded(name) {`, ` const loader = LOADERS[name]`, ` if (loader) loader()`, `}`, ``, `registerSchemeLoader(ensureSchemeLoaded)`, ``, `export { ensureSchemeLoaded }`, ``)
281
- return lines.join('\n')
456
+ }
457
+
458
+ /**
459
+ * Build per-scheme molecules for every literal className the project
460
+ * uses. Each eligible className gets a pre-merged style object under
461
+ * `common`; a variant only carries an entry when its merge DIFFERS from
462
+ * common (runtime falls back `molecules[scheme] ?? molecules.common`).
463
+ * @param literals Distinct literal className strings (raw).
464
+ * @param resolved Per-atom schemed styles.
465
+ * @param keyframes Keyframes to inline.
466
+ * @param variants Variant scheme names.
467
+ * @param gradients Gradient feature map.
468
+ * @param haptics Haptic feature map.
469
+ * @returns scheme → (normalized className → merged style).
470
+ */
471
+ function buildMolecules(
472
+ literals: readonly string[],
473
+ resolved: ReadonlyMap<string, SchemedStyle>,
474
+ keyframes: ReadonlyMap<string, KeyframeBlock>,
475
+ variants: readonly string[],
476
+ gradients: ReadonlyMap<string, unknown>,
477
+ haptics: ReadonlyMap<string, unknown>,
478
+ ): Record<string, Record<string, RNStyle>> {
479
+ const common: Record<string, RNStyle> = {}
480
+ const variantMaps: Record<string, Record<string, RNStyle>> = {}
481
+ for (const variant of variants) variantMaps[variant] = {}
482
+
483
+ for (const literal of literals) {
484
+ const normalized = normalizeClassName(literal)
485
+ if (normalized.length === 0) continue
486
+ const tokens = normalized.split(' ')
487
+ const commonMerged = mergeMolecule(tokens, COMMON_SCHEME, resolved, keyframes, gradients, haptics)
488
+ if (commonMerged === null) continue
489
+ common[normalized] = commonMerged
490
+ addVariantMolecules(normalized, tokens, JSON.stringify(commonMerged), variants, variantMaps, resolved, keyframes, gradients, haptics)
491
+ }
492
+
493
+ const out: Record<string, Record<string, RNStyle>> = { [COMMON_SCHEME]: common }
494
+ for (const variant of variants) {
495
+ if (Object.keys(variantMaps[variant]).length > 0) out[variant] = variantMaps[variant]
496
+ }
497
+ return out
282
498
  }
283
499
 
284
500
  /** Output of one build pass — one source per scheme plus the manifest. */
@@ -458,6 +674,11 @@ const EMPTY_BREAKPOINTS: ReadonlyMap<string, number> = new Map()
458
674
  * manifest emits `registerBreakpoints({...})` so the runtime can gate
459
675
  * `md:*` / `lg:*` atoms on `windowWidth`. Optional — empty when the
460
676
  * theme declares no breakpoints (legacy/test callers).
677
+ * @param gradients Gradient feature map (atom → role/colour) for the manifest + molecule eligibility.
678
+ * @param haptics Haptic feature map (atom → request) for the manifest + molecule eligibility.
679
+ * @param literals Distinct literal className strings — pre-merged into
680
+ * per-scheme molecules so the runtime resolver's O(1) molecule-first
681
+ * path is populated. Empty for legacy/test callers (atom path only).
461
682
  * @returns Per-scheme sources, manifest source, variant list.
462
683
  */
463
684
  export function buildSchemeSources(
@@ -466,6 +687,9 @@ export function buildSchemeSources(
466
687
  keyframes: ReadonlyMap<string, KeyframeBlock>,
467
688
  cache?: AtomSerializedCache,
468
689
  breakpoints: ReadonlyMap<string, number> = EMPTY_BREAKPOINTS,
690
+ gradients: ReadonlyMap<string, unknown> = EMPTY_FEATURE_MAP,
691
+ haptics: ReadonlyMap<string, unknown> = EMPTY_FEATURE_MAP,
692
+ literals: readonly string[] = EMPTY_LITERALS,
469
693
  ): BuildSchemeSourcesOutput {
470
694
  const variants = collectVariantSchemes(resolved)
471
695
  const commonEntries: (readonly [string, string])[] = []
@@ -481,20 +705,27 @@ export function buildSchemeSources(
481
705
  misses += collectAtomEntries(atom, schemed, canonical, variants, keyframes, commonEntries, variantEntries, cache)
482
706
  }
483
707
 
708
+ const molecules = buildMolecules(literals, resolved, keyframes, variants, gradients, haptics)
484
709
  const schemeSources: Record<string, string> = {
485
- [COMMON_SCHEME]: renderSchemeFile(COMMON_SCHEME, commonEntries),
710
+ [COMMON_SCHEME]: renderSchemeFile(COMMON_SCHEME, commonEntries, molecules[COMMON_SCHEME]),
486
711
  }
487
712
  for (const variant of variants) {
488
- schemeSources[variant] = renderSchemeFile(variant, variantEntries[variant])
713
+ schemeSources[variant] = renderSchemeFile(variant, variantEntries[variant], molecules[variant])
489
714
  }
490
715
 
491
716
  return {
492
717
  schemeSources,
493
- manifestSource: renderManifest(variants, breakpoints),
718
+ manifestSource: renderManifest(variants, breakpoints, gradients, haptics),
494
719
  variants,
495
720
  serializedMisses: misses,
496
721
  }
497
722
  }
498
723
 
724
+ /** Shared empty feature map default. */
725
+ const EMPTY_FEATURE_MAP: ReadonlyMap<string, unknown> = new Map()
726
+
727
+ /** Shared empty literal-list default (atom-only callers). */
728
+ const EMPTY_LITERALS: readonly string[] = []
729
+
499
730
  /** Registry key the runtime uses for the always-loaded fallback. */
500
731
  export const COMMON_SCHEME_NAME: string = COMMON_SCHEME