rnwind 0.0.11 → 0.0.12
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.
- package/lib/cjs/core/normalize-classname.cjs +3 -1
- package/lib/cjs/core/normalize-classname.cjs.map +1 -1
- package/lib/cjs/core/parser/border-dispatcher.cjs +20 -10
- package/lib/cjs/core/parser/border-dispatcher.cjs.map +1 -1
- package/lib/cjs/core/parser/color-properties-dispatcher.cjs +7 -5
- package/lib/cjs/core/parser/color-properties-dispatcher.cjs.map +1 -1
- package/lib/cjs/core/parser/color.cjs +194 -10
- package/lib/cjs/core/parser/color.cjs.map +1 -1
- package/lib/cjs/core/parser/color.d.ts +18 -3
- package/lib/cjs/core/parser/declaration.cjs +62 -4
- package/lib/cjs/core/parser/declaration.cjs.map +1 -1
- package/lib/cjs/core/parser/layout-dispatcher.cjs +32 -2
- package/lib/cjs/core/parser/layout-dispatcher.cjs.map +1 -1
- package/lib/cjs/core/parser/shorthand.cjs +10 -3
- package/lib/cjs/core/parser/shorthand.cjs.map +1 -1
- package/lib/cjs/core/parser/tokens.cjs +9 -0
- package/lib/cjs/core/parser/tokens.cjs.map +1 -1
- package/lib/cjs/core/parser/tw-parser.cjs +6 -0
- package/lib/cjs/core/parser/tw-parser.cjs.map +1 -1
- package/lib/cjs/core/parser/typography-dispatcher.cjs +15 -8
- package/lib/cjs/core/parser/typography-dispatcher.cjs.map +1 -1
- package/lib/cjs/core/style-builder/union-builder.cjs +81 -2
- package/lib/cjs/core/style-builder/union-builder.cjs.map +1 -1
- package/lib/cjs/core/style-builder/union-builder.d.ts +28 -0
- package/lib/cjs/metro/state.cjs +74 -13
- package/lib/cjs/metro/state.cjs.map +1 -1
- package/lib/cjs/metro/state.d.ts +18 -0
- package/lib/cjs/metro/transformer.cjs +10 -4
- package/lib/cjs/metro/transformer.cjs.map +1 -1
- package/lib/cjs/metro/with-config.cjs +57 -0
- package/lib/cjs/metro/with-config.cjs.map +1 -1
- package/lib/cjs/metro/with-config.d.ts +12 -0
- package/lib/cjs/metro/wrap-imports.cjs +36 -1
- package/lib/cjs/metro/wrap-imports.cjs.map +1 -1
- package/lib/cjs/runtime/hooks/use-scheme.cjs +14 -7
- package/lib/cjs/runtime/hooks/use-scheme.cjs.map +1 -1
- package/lib/cjs/runtime/resolve.cjs +6 -2
- package/lib/cjs/runtime/resolve.cjs.map +1 -1
- package/lib/cjs/runtime/resolve.d.ts +5 -1
- package/lib/esm/core/normalize-classname.mjs +3 -1
- package/lib/esm/core/normalize-classname.mjs.map +1 -1
- package/lib/esm/core/parser/border-dispatcher.mjs +21 -11
- package/lib/esm/core/parser/border-dispatcher.mjs.map +1 -1
- package/lib/esm/core/parser/color-properties-dispatcher.mjs +8 -6
- package/lib/esm/core/parser/color-properties-dispatcher.mjs.map +1 -1
- package/lib/esm/core/parser/color.d.ts +18 -3
- package/lib/esm/core/parser/color.mjs +195 -12
- package/lib/esm/core/parser/color.mjs.map +1 -1
- package/lib/esm/core/parser/declaration.mjs +63 -5
- package/lib/esm/core/parser/declaration.mjs.map +1 -1
- package/lib/esm/core/parser/layout-dispatcher.mjs +32 -2
- package/lib/esm/core/parser/layout-dispatcher.mjs.map +1 -1
- package/lib/esm/core/parser/shorthand.mjs +11 -4
- package/lib/esm/core/parser/shorthand.mjs.map +1 -1
- package/lib/esm/core/parser/tokens.mjs +10 -1
- package/lib/esm/core/parser/tokens.mjs.map +1 -1
- package/lib/esm/core/parser/tw-parser.mjs +6 -0
- package/lib/esm/core/parser/tw-parser.mjs.map +1 -1
- package/lib/esm/core/parser/typography-dispatcher.mjs +15 -8
- package/lib/esm/core/parser/typography-dispatcher.mjs.map +1 -1
- package/lib/esm/core/style-builder/union-builder.d.ts +28 -0
- package/lib/esm/core/style-builder/union-builder.mjs +82 -3
- package/lib/esm/core/style-builder/union-builder.mjs.map +1 -1
- package/lib/esm/metro/state.d.ts +18 -0
- package/lib/esm/metro/state.mjs +75 -14
- package/lib/esm/metro/state.mjs.map +1 -1
- package/lib/esm/metro/transformer.mjs +10 -4
- package/lib/esm/metro/transformer.mjs.map +1 -1
- package/lib/esm/metro/with-config.d.ts +12 -0
- package/lib/esm/metro/with-config.mjs +58 -2
- package/lib/esm/metro/with-config.mjs.map +1 -1
- package/lib/esm/metro/wrap-imports.mjs +36 -1
- package/lib/esm/metro/wrap-imports.mjs.map +1 -1
- package/lib/esm/runtime/hooks/use-scheme.mjs +14 -7
- package/lib/esm/runtime/hooks/use-scheme.mjs.map +1 -1
- package/lib/esm/runtime/resolve.d.ts +5 -1
- package/lib/esm/runtime/resolve.mjs +6 -2
- package/lib/esm/runtime/resolve.mjs.map +1 -1
- package/package.json +1 -1
- package/src/core/normalize-classname.ts +4 -1
- package/src/core/parser/border-dispatcher.ts +22 -11
- package/src/core/parser/color-properties-dispatcher.ts +7 -5
- package/src/core/parser/color.ts +182 -11
- package/src/core/parser/declaration.ts +61 -5
- package/src/core/parser/layout-dispatcher.ts +34 -2
- package/src/core/parser/shorthand.ts +9 -3
- package/src/core/parser/tokens.ts +10 -1
- package/src/core/parser/tw-parser.ts +5 -0
- package/src/core/parser/typography-dispatcher.ts +15 -6
- package/src/core/style-builder/union-builder.ts +83 -3
- package/src/metro/state.ts +117 -12
- package/src/metro/transformer.ts +9 -4
- package/src/metro/with-config.ts +59 -1
- package/src/metro/wrap-imports.ts +36 -1
- package/src/runtime/hooks/use-scheme.ts +13 -6
- package/src/runtime/resolve.ts +6 -2
package/lib/cjs/metro/state.d.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { UnionBuilder } from '../core/style-builder';
|
|
2
2
|
import { TailwindParser } from '../core/parser';
|
|
3
3
|
import { buildWrapModules } from './wrap-imports';
|
|
4
|
+
/** Test-only — count of actual disk reads of the theme CSS (memo misses). */
|
|
5
|
+
export declare function __getThemeReadCount(): number;
|
|
6
|
+
/** Test-only — clear the theme memo so the next read hits disk. */
|
|
7
|
+
export declare function __resetThemeMemo(): void;
|
|
8
|
+
/**
|
|
9
|
+
* Test-only override for the memoised library fingerprint. Production reads
|
|
10
|
+
* the value once from disk; tests use this to simulate a library upgrade
|
|
11
|
+
* (fingerprint rotation) without rebuilding the lib. Passing `undefined`
|
|
12
|
+
* clears the override so the next call re-derives from disk.
|
|
13
|
+
* @param value Forced fingerprint, or omit/`undefined` to clear the override.
|
|
14
|
+
*/
|
|
15
|
+
export declare function __setLibraryFingerprintForTest(value?: string): void;
|
|
4
16
|
/**
|
|
5
17
|
* Worker-local state. Lazy-initialised on first access so files that
|
|
6
18
|
* bypass the transform don't pay for construction.
|
|
@@ -10,6 +22,12 @@ export interface RnwindState {
|
|
|
10
22
|
builder: UnionBuilder;
|
|
11
23
|
themeCss: string;
|
|
12
24
|
themeHash: string;
|
|
25
|
+
/**
|
|
26
|
+
* Library fingerprint captured when this state was built. Folded into the
|
|
27
|
+
* rebuild guard so an in-place rnwind upgrade (unchanged CSS, rotated
|
|
28
|
+
* fingerprint) drops the stale builder + on-disk scheme format.
|
|
29
|
+
*/
|
|
30
|
+
libraryFingerprint: string;
|
|
13
31
|
projectRoot: string;
|
|
14
32
|
}
|
|
15
33
|
/**
|
|
@@ -179,12 +179,18 @@ async function rewriteSource(args) {
|
|
|
179
179
|
// Wrap host imports ONLY when className flows through a component — written
|
|
180
180
|
// (`hasClassName`) or forwarded (`{...rest}`). A `useCss`/`cva`-only file
|
|
181
181
|
// resolves manually, so its imports (e.g. skia drawing primitives) are
|
|
182
|
-
// left untouched.
|
|
183
|
-
|
|
182
|
+
// left untouched. Mutates the AST in place; both branches below regenerate.
|
|
183
|
+
if (hasClassName || hasSpread)
|
|
184
|
+
wrapImports.rewriteWrapImports(ast, state.getWrapModules());
|
|
184
185
|
if (!hasAtoms) {
|
|
185
|
-
// Wrap-only forwarder — no classes to record/register
|
|
186
|
+
// Wrap-only forwarder — no classes to record/register, but className
|
|
187
|
+
// still flows through it (`hasClassName || hasSpread`). It MUST hold a
|
|
188
|
+
// dep-graph edge to the theme CSS so a theme edit re-transforms it and
|
|
189
|
+
// its forwarded className resolves against the new scheme — otherwise it
|
|
190
|
+
// renders stale. Inject the theme-signature import even with no atoms.
|
|
186
191
|
state$1.builder.dropFile(args.filename);
|
|
187
|
-
|
|
192
|
+
injectThemeSignatureImport(ast);
|
|
193
|
+
return generateModule(ast).code;
|
|
188
194
|
}
|
|
189
195
|
warnUnknownClasses(args.src, parsed.candidates, parsed.atoms, args.filename, [parsed.gradientAtoms, parsed.hapticAtoms]);
|
|
190
196
|
const literals = collectClassNameLiterals(ast);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformer.cjs","sources":["../../../../src/metro/transformer.ts"],"sourcesContent":["import type { File } from '@babel/types'\nimport * as t from '@babel/types'\nimport { parse } from '@babel/parser'\nimport generate from '@babel/generator'\nimport { createHash } from 'node:crypto'\nimport { realpathSync } from 'node:fs'\nimport { getRnwindCacheKey, getRnwindState, getWrapModules, onThemeChange } from './state'\nimport { rewriteWrapImports } from './wrap-imports'\nimport { STYLE_SPECIFIERS, THEME_SIGNATURE_MODULE } from './resolver'\nimport { filterUnknownClassCandidates } from './warn-unknown-classes'\n\n/** The shape of the upstream module we delegate parsing/babel work to. */\ninterface UpstreamTransformer {\n transform: (args: BabelTransformerArgs) => Promise<BabelTransformerResult> | BabelTransformerResult\n}\n\n/** Env var that points at the upstream `babelTransformerPath` we override. */\nconst UPSTREAM_ENV = 'RNWIND_UPSTREAM_TRANSFORMER'\n\n/** Cached upstream module — required once, reused across every transform call. */\nlet cachedUpstream: UpstreamTransformer | null = null\n\nconst generateModule = (generate as unknown as { default?: typeof generate }).default ?? generate\n\n/**\n * Parse user source with the broad plugin set (Flow + JSX + TypeScript\n * + class properties). Permissive on purpose so we don't reject any\n * file the upstream could have handled. Returns `null` when parse\n * fails — caller falls back to the raw source string.\n * @param source Source text.\n * @returns Parsed AST, or null on parse failure.\n */\nfunction parseUserSource(source: string): File | null {\n try {\n return parse(source, {\n sourceType: 'unambiguous',\n allowReturnOutsideFunction: true,\n allowImportExportEverywhere: true,\n plugins: ['typescript', 'jsx'],\n }) as unknown as File\n } catch {\n try {\n return parse(source, {\n sourceType: 'unambiguous',\n allowReturnOutsideFunction: true,\n allowImportExportEverywhere: true,\n plugins: ['flow', 'jsx'],\n }) as unknown as File\n } catch {\n return null\n }\n }\n}\n\n/**\n * Print Tailwind-shaped candidates oxide picked up but the parser\n * could NOT compile — typo, missing custom utility, or class not in\n * the user's theme. Filtering by candidates that ALSO appear inside a\n * `className=\"…\"` literal eliminates false positives from imports,\n * comments, and JSX prop values.\n * @param source Original source text — searched for className literals.\n * @param candidates Every candidate oxide surfaced from the source.\n * @param atoms Successfully resolved atoms (keys are class names).\n * @param filename Source path, prefixed onto the warning.\n * @param features Feature-atom maps (gradient / haptic) — their names are\n * known classes even though they carry no RN style, so they're excluded\n * from the unknown-class warning.\n */\nfunction warnUnknownClasses(\n source: string,\n candidates: readonly string[],\n atoms: ReadonlyMap<string, unknown>,\n filename: string,\n features: ReadonlyArray<ReadonlyMap<string, unknown>> = [],\n): void {\n // Feature atoms (gradient / haptic) resolve to no RN style, so they're\n // absent from `atoms` — but they're NOT unknown. Fold their names into\n // the known set so `active:haptic-rigid` etc. don't warn at build time.\n const known = new Set(atoms.keys())\n for (const map of features) for (const name of map.keys()) known.add(name)\n const unknown = filterUnknownClassCandidates(source, candidates, known)\n if (unknown.length === 0) return\n // eslint-disable-next-line no-console\n console.warn(`rnwind: unknown class${unknown.length > 1 ? 'es' : ''} in ${filename}: ${unknown.join(', ')}`)\n}\n\n/**\n * Extract the bare extension for oxide / internal switches.\n * @param filename Absolute path.\n * @returns Extension without the leading dot (`tsx` / `ts` / `js` / `jsx`).\n */\nfunction extensionOf(filename: string): string {\n const index = filename.lastIndexOf('.')\n if (index === -1) return 'tsx'\n return filename.slice(index + 1)\n}\n\n/**\n * Read the project root Metro hands us per-transform. Falls back to\n * `process.cwd()` only when the upstream harness doesn't set it (unit\n * tests, standalone). Metro's production pipeline always sets it.\n * @param args Metro transformer args.\n * @returns Absolute project root.\n */\nfunction projectRootOf(args: BabelTransformerArgs): string {\n const fromOptions = args.options?.projectRoot\n if (typeof fromOptions === 'string' && fromOptions.length > 0) return fromOptions\n return process.cwd()\n}\n\n/**\n * Whether a `.css` filename is the user's theme entry (the file\n * `withRnwindConfig` pointed us at via `RNWIND_CSS_ENTRY_FILE`).\n * Only the theme CSS should trigger a scheme rebuild — unrelated CSS\n * files in the project stay invisible to rnwind.\n * @param filename Absolute CSS path.\n * @returns Whether the file is the configured theme entry.\n */\nfunction isThemeCssEntry(filename: string): boolean {\n const cssEntry = process.env.RNWIND_CSS_ENTRY_FILE\n return typeof cssEntry === 'string' && cssEntry.length > 0 && cssEntry === filename\n}\n\n/**\n * Wrap host imports + compile any className literals, then regenerate\n * source. Two paths:\n * - **className present**: oxide-scan the file, record its atoms into\n * the union, and inject the generated-style + theme-signature\n * side-effect imports so the runtime registries populate.\n * - **import-only** (a `{...rest}` forwarder or a leaf with no literal\n * `className=`): just wrap the host imports so a forwarded className\n * still resolves at render — no oxide scan, no injected imports.\n *\n * On parse failure, fall back to the original source — a transient parse\n * error shouldn't crash Metro for a file the upstream might handle fine.\n * @param args Metro args; `src` is the original source text.\n * @returns Rewritten source text.\n */\nasync function rewriteSource(args: BabelTransformerArgs): Promise<string> {\n // SCAN FIRST — exactly like Tailwind: oxide extracts class candidates from\n // the WHOLE file content (className, cva/clsx, plain strings, anywhere),\n // and the compiler is the only filter. No babel parse needed to scan, so\n // the common \"file with no classes\" case stays cheap and the source is\n // returned untouched. This is what makes adding a class ANYWHERE register\n // on hot-reload, not just inside a `className=` or a known helper call.\n const state = getRnwindState(projectRootOf(args))\n const extension = extensionOf(args.filename)\n const parsed = await state.parser.parseAtoms({ content: args.src, extension })\n const hasAtoms = parsed.atoms.size > 0\n\n const hasClassName = /classname=/i.test(args.src)\n const hasSpread = /\\{\\s*\\.\\.\\./.test(args.src)\n\n // Nothing for rnwind to do: no Tailwind class compiled AND no host\n // wrapping needed. Drop any stale contribution and emit the file as-is.\n if (!hasAtoms && !hasClassName && !hasSpread) {\n state.builder.dropFile(args.filename)\n return args.src\n }\n\n const ast = parseUserSource(args.src)\n if (!ast) {\n // Can't parse to wrap/inject, but we DID scan — still record the atoms so\n // they register (a malformed-but-recoverable file shouldn't lose styles).\n if (hasAtoms) {\n await state.builder.recordFile(args.filename, parsed.atoms, parsed.keyframes, [])\n await state.builder.writeSchemes()\n } else {\n state.builder.dropFile(args.filename)\n }\n return args.src\n }\n\n // Wrap host imports ONLY when className flows through a component — written\n // (`hasClassName`) or forwarded (`{...rest}`). A `useCss`/`cva`-only file\n // resolves manually, so its imports (e.g. skia drawing primitives) are\n // left untouched.\n const wrapped = hasClassName || hasSpread ? rewriteWrapImports(ast, getWrapModules()) : false\n\n if (!hasAtoms) {\n // Wrap-only forwarder — no classes to record/register.\n state.builder.dropFile(args.filename)\n return wrapped ? generateModule(ast).code : args.src\n }\n\n warnUnknownClasses(args.src, parsed.candidates, parsed.atoms, args.filename, [parsed.gradientAtoms, parsed.hapticAtoms])\n const literals = collectClassNameLiterals(ast)\n const { changed } = await state.builder.recordFile(args.filename, parsed.atoms, parsed.keyframes, literals)\n if (changed) await state.builder.writeSchemes()\n\n injectSideEffectImports(ast, STYLE_SPECIFIERS)\n injectThemeSignatureImport(ast)\n return generateModule(ast).code\n}\n\n/**\n * Whether a JSX attribute names a className-style prop (`className` or\n * any `<prefix>ClassName`).\n * @param node JSX attribute node.\n * @returns True when the attribute is a className prop.\n */\nfunction isClassNameAttribute(node: t.JSXAttribute): boolean {\n if (!t.isJSXIdentifier(node.name)) return false\n const {name} = node.name\n return name === 'className' || name.endsWith('ClassName')\n}\n\n/**\n * Pull static string literals out of a className expression. Handles a\n * bare string, a no-substitution template, and the branches of a\n * ternary / `&&` (so `cond ? 'a' : 'b'` and `flag && 'x'` both register\n * their literals). Dynamic interpolations are skipped — they resolve via\n * the runtime atom path.\n * @param expr Expression inside a `className={...}` container.\n * @param out Accumulator for discovered literals.\n */\nfunction collectLiteralsFromExpression(expr: t.Expression | t.JSXEmptyExpression | null | undefined, out: string[]): void {\n if (!expr) return\n if (t.isStringLiteral(expr)) {\n out.push(expr.value)\n return\n }\n if (t.isTemplateLiteral(expr) && expr.expressions.length === 0 && expr.quasis.length === 1) {\n const cooked = expr.quasis[0]?.value.cooked\n if (typeof cooked === 'string') out.push(cooked)\n return\n }\n if (t.isConditionalExpression(expr)) {\n collectLiteralsFromExpression(expr.consequent, out)\n collectLiteralsFromExpression(expr.alternate, out)\n return\n }\n if (t.isLogicalExpression(expr)) {\n collectLiteralsFromExpression(expr.right as t.Expression, out)\n }\n}\n\n/** AST node keys the literal walk skips — position / comment metadata. */\nconst SKIP_WALK_KEYS = new Set(['type', 'loc', 'start', 'end', 'range', 'leadingComments', 'trailingComments', 'innerComments'])\n\n/**\n * Collect the static literals from one className JSX attribute into the\n * dedup accumulator.\n * @param attribute The (already className-matched) JSX attribute.\n * @param seen Dedup set of literals already collected.\n * @param out Ordered accumulator.\n */\nfunction collectAttributeLiterals(attribute: t.JSXAttribute, seen: Set<string>, out: string[]): void {\n const { value } = attribute\n const found: string[] = []\n if (t.isStringLiteral(value)) found.push(value.value)\n else if (t.isJSXExpressionContainer(value)) collectLiteralsFromExpression(value.expression, found)\n for (const literal of found) {\n if (seen.has(literal)) continue\n seen.add(literal)\n out.push(literal)\n }\n}\n\n/**\n * Walk the AST for every `className=` / `<prefix>ClassName=` literal so\n * the builder can pre-merge each into a per-scheme molecule. A generic\n * node walk (no scope build) keeps it cheap; only JSX attribute nodes do\n * any work.\n * @param ast Parsed Babel file.\n * @returns Distinct literal className strings, in first-seen order.\n */\nfunction collectClassNameLiterals(ast: File): readonly string[] {\n const out: string[] = []\n const seen = new Set<string>()\n const visit = (node: unknown): void => {\n if (!node || typeof node !== 'object') return\n if (Array.isArray(node)) {\n for (const child of node) visit(child)\n return\n }\n const typed = node as { type?: string; [key: string]: unknown }\n if (typeof typed.type !== 'string') return\n if (typed.type === 'JSXAttribute' && isClassNameAttribute(node as t.JSXAttribute)) {\n collectAttributeLiterals(node as t.JSXAttribute, seen, out)\n }\n for (const key in typed) {\n if (SKIP_WALK_KEYS.has(key)) continue\n visit(typed[key])\n }\n }\n visit(ast.program)\n return out\n}\n\n/**\n * Prepend side-effect imports (`import '<spec>'`) so the generated\n * per-scheme style + manifest modules load — registering this file's\n * atoms / molecules / features into the runtime registries the wrapper's\n * `resolve` reads.\n * @param ast Babel File AST to mutate in place.\n * @param specifiers Module specifiers to side-effect-import.\n */\nfunction injectSideEffectImports(ast: File, specifiers: readonly string[]): void {\n for (const specifier of specifiers) {\n ast.program.body.unshift(t.importDeclaration([], t.stringLiteral(specifier)))\n }\n}\n\n/**\n * Prepend `import 'rnwind/__generated/theme-signature'` to every\n * rnwind-transformed file. The resolver maps that specifier to the\n * user's theme CSS so Metro's dependency graph carries a real edge\n * from this JS file to the CSS. When the user edits `global.css`,\n * the CSS module's SHA1 changes, and Metro invalidates every JS file\n * holding this import — forcing them to re-transform with the new\n * theme. The `.css` branch in {@link transform} returns an empty\n * `export {}` module so the runtime cost is one extra `require()`.\n * @param ast Babel File AST to mutate in place.\n */\nfunction injectThemeSignatureImport(ast: File): void {\n const declaration = t.importDeclaration([], t.stringLiteral(THEME_SIGNATURE_MODULE))\n ast.program.body.unshift(declaration)\n}\n\n/**\n * Read the upstream transformer's `getCacheKey()` so our cache-key\n * contribution composes with — rather than replaces — whatever the\n * host framework wants to mix in.\n * @returns Upstream cache key, or `null` when no upstream exposes one.\n */\nfunction loadUpstreamCacheKey(): string | null {\n const upstream = loadUpstream() as (UpstreamTransformer & { getCacheKey?: () => string }) | null\n if (!upstream) return null\n try {\n return typeof upstream.getCacheKey === 'function' ? upstream.getCacheKey() : null\n } catch {\n return null\n }\n}\n\n/**\n * Invoke the upstream `babelTransformerPath` Metro originally had\n * configured. The path is read from `RNWIND_UPSTREAM_TRANSFORMER`,\n * which `withRnwindConfig` sets at Metro startup. When the env var is\n * unset (unit tests, standalone use), fall back to a typescript+jsx\n * parse.\n * @param args Metro's per-file args.\n * @returns Upstream transform result containing the post-babel AST.\n */\nasync function runUpstream(args: BabelTransformerArgs): Promise<BabelTransformerResult> {\n if (args.ast && !process.env[UPSTREAM_ENV]) return { ast: args.ast }\n const upstream = loadUpstream()\n if (upstream) return await Promise.resolve(upstream.transform(args))\n if (args.ast) return { ast: args.ast }\n return { ast: parseSource(args.src) }\n}\n\n/**\n * Lazily require the upstream transformer module. Cached after first\n * load so per-file overhead is one cache lookup.\n * @returns Upstream module, or null when env is unset.\n */\nfunction loadUpstream(): UpstreamTransformer | null {\n if (cachedUpstream) return cachedUpstream\n const upstreamPath = process.env[UPSTREAM_ENV]\n if (!upstreamPath || upstreamPath.length === 0) return null\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires\n const required = require(upstreamPath) as UpstreamTransformer | { default?: UpstreamTransformer }\n const upstream = (required as { default?: UpstreamTransformer }).default ?? (required as UpstreamTransformer)\n if (typeof upstream.transform !== 'function') return null\n cachedUpstream = upstream\n return upstream\n } catch (error) {\n // eslint-disable-next-line no-console\n if (process.env.RNWIND_DEBUG) console.error('rnwind: failed to load upstream transformer:', error)\n return null\n }\n}\n\n/**\n * Cheap guard — the file has to look JS/TS, live outside `node_modules`,\n * and mention `className=` before we spend AST cycles on it.\n *\n * Symlink awareness: monorepo workspaces (yarn / pnpm / bun workspaces)\n * symlink each package into the consumer's `node_modules/<name>`, so a\n * file from `packages/ui/src/Foo.tsx` ends up reaching the transformer\n * as `<root>/node_modules/ui/src/Foo.tsx`. The naïve `/node_modules/`\n * check would skip every workspace UI file. We `realpath` the filename\n * once and only bail when the resolved real path is ALSO under\n * node_modules — true third-party installs.\n * @param args Metro args.\n * @returns Whether the file might need the rnwind pass.\n */\nfunction isRewriteCandidate(args: BabelTransformerArgs): boolean {\n if (!/\\.(?:tsx|ts|jsx|js)$/i.test(args.filename)) return false\n // EVERY user source file is scanned — exactly like Tailwind walks its\n // whole content set. The compiler is the only filter (see rewriteSource);\n // no className/helper pre-filter, because that \"filter magic\" missed\n // classes living in cva/clsx/plain-string files and broke their hot-reload.\n // (Cheap when the file has no classes: oxide finds nothing, no babel parse,\n // source returned untouched.)\n if (!args.filename.includes('/node_modules/')) return true\n // node_modules in path → could be a workspace symlink; resolve it.\n try {\n return !realpathSync(args.filename).includes('/node_modules/')\n } catch {\n // realpath failed (broken symlink, missing file). Fall back to skipping.\n return false\n }\n}\n\n/**\n * Fallback parse when no upstream is configured AND Metro didn't hand\n * us an AST. Used by unit tests and standalone setups.\n * @param source Source text.\n * @returns Parsed Babel File.\n */\nfunction parseSource(source: string): File {\n return parse(source, { sourceType: 'module', plugins: ['typescript', 'jsx'] }) as unknown as File\n}\n\n/** Metro's babel transformer signature. */\nexport interface BabelTransformerArgs {\n filename: string\n src: string\n options: { projectRoot?: string; [key: string]: unknown }\n ast?: File\n plugins?: readonly unknown[]\n}\n\n/** Return shape Metro expects from a babel transformer. */\nexport interface BabelTransformerResult {\n ast: File\n metadata?: unknown\n}\n\n/**\n * rnwind's Metro babel transformer. Two phases per source file:\n *\n * 1. **Pre-process the source string before handing it to the upstream\n * babel pipeline.** babel-preset-expo / React's JSX transform run\n * inside the upstream and convert `<View className=\"...\"/>` into\n * `React.createElement(View, {className})`. If we walked the AST\n * AFTER the upstream, there'd be no JSX attributes left to\n * rewrite. So we parse, run our pass, regenerate code, and feed\n * THAT to the upstream as `src`.\n * 2. **Delegate to the upstream `babelTransformerPath`** (Expo's\n * default handles Flow stripping, expo-router macros, etc.).\n *\n * Skip both phases when the file isn't a JS/TS source under user\n * code, or doesn't mention `className=` — hand straight to upstream.\n * @param args Metro's per-file args.\n * @returns Mutated AST + metadata.\n */\nexport async function transform(args: BabelTransformerArgs): Promise<BabelTransformerResult> {\n // Short-circuit `.css` inputs: the theme CSS is pulled into the dep\n // graph as a sentinel (see `THEME_SIGNATURE_MODULE` in resolver.ts)\n // so Metro watches it and invalidates importers on edit, but the\n // file's CSS syntax can't go through a JS babel transformer.\n //\n // When the CSS being transformed IS the user's theme entry, we\n // piggyback on Metro's own file-watcher: Metro calls us here on\n // every CSS save; we trigger `onThemeChange` to rebuild parser +\n // rewrite scheme files with the new values. Metro's dep graph then\n // HMRs the regenerated `common.style.js` to the running app.\n //\n // Emitting the CSS content hash in the fake JS output is what makes\n // Metro propagate invalidation to downstream importers — constant\n // `export {}` bytes would never look changed and Metro would skip\n // the chain.\n if (args.filename.endsWith('.css')) {\n if (isThemeCssEntry(args.filename)) {\n try {\n await onThemeChange(projectRootOf(args))\n } catch {\n // CSS edit happened outside a configured project (e.g. tests).\n }\n }\n const themeHash = createHash('sha256').update(args.src).digest('hex').slice(0, 16)\n const stub = `export const __rnwindThemeHash = ${JSON.stringify(themeHash)};\\n`\n return { ast: parse(stub, { sourceType: 'module' }) as unknown as File }\n }\n if (!isRewriteCandidate(args)) {\n if (/\\.(?:tsx|ts|jsx|js)$/i.test(args.filename) && !args.filename.includes('/node_modules/')) {\n try {\n getRnwindState(projectRootOf(args)).builder.dropFile(args.filename)\n } catch {\n // State not configured (e.g. test). Nothing to drop.\n }\n }\n return runUpstream(args)\n }\n\n const rewrittenSource = await rewriteSource(args)\n return runUpstream({ ...args, src: rewrittenSource, ast: undefined })\n}\n\n/**\n * Metro's babel-transformer contract: a `getCacheKey()` export is\n * sampled per-file and mixed into the transform cache key. Returning\n * a string that includes the theme CSS content hash invalidates every\n * cached transform on every CSS edit — so the bundle rebuilds with\n * the new theme automatically on the next request.\n * @returns Cache-key segment that includes rnwind's current theme hash.\n */\nexport function getCacheKey(): string {\n const upstreamKey = loadUpstreamCacheKey()\n const ownKey = getRnwindCacheKey()\n return upstreamKey ? `${upstreamKey}|${ownKey}` : ownKey\n}\n\n/** Test-only — drop the cached upstream so a new env var picks up next call. */\nexport function __resetUpstreamCache(): void {\n cachedUpstream = null\n}\n"],"names":["parse","filterUnknownClassCandidates","state","getRnwindState","rewriteWrapImports","getWrapModules","STYLE_SP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AAM,aAAa,CAAC,IAAI,CAAC;AACjD,IAAA,OAAO,WAAW,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC;AACvE;AAEA;;;;;;;AAOG;SACa,WAAW,GAAA;AACzB,IAAA,MAAM,WAAW,GAAG,oBAAoB,EAAE;AAC1C,IAAA,MAAM,MAAM,GAAGS,uBAAiB,EAAE;AAClC,IAAA,OAAO,WAAW,GAAG,CAAA,EAAG,WAAW,CAAA,CAAA,EAAI,MAAM,CAAA,CAAE,GAAG,MAAM;AAC1D;AAEA;SACgB,oBAAoB,GAAA;IAClC,cAAc,GAAG,IAAI;AACvB;;;;;;"}
|
|
1
|
+
{"version":3,"file":"transformer.cjs","sources":["../../../../src/metro/transformer.ts"],"sourcesContent":["import type { File } from '@babel/types'\nimport * as t from '@babel/types'\nimport { parse } from '@babel/parser'\nimport generate from '@babel/generator'\nimport { createHash } from 'node:crypto'\nimport { realpathSync } from 'node:fs'\nimport { getRnwindCacheKey, getRnwindState, getWrapModules, onThemeChange } from './state'\nimport { rewriteWrapImports } from './wrap-imports'\nimport { STYLE_SPECIFIERS, THEME_SIGNATURE_MODULE } from './resolver'\nimport { filterUnknownClassCandidates } from './warn-unknown-classes'\n\n/** The shape of the upstream module we delegate parsing/babel work to. */\ninterface UpstreamTransformer {\n transform: (args: BabelTransformerArgs) => Promise<BabelTransformerResult> | BabelTransformerResult\n}\n\n/** Env var that points at the upstream `babelTransformerPath` we override. */\nconst UPSTREAM_ENV = 'RNWIND_UPSTREAM_TRANSFORMER'\n\n/** Cached upstream module — required once, reused across every transform call. */\nlet cachedUpstream: UpstreamTransformer | null = null\n\nconst generateModule = (generate as unknown as { default?: typeof generate }).default ?? generate\n\n/**\n * Parse user source with the broad plugin set (Flow + JSX + TypeScript\n * + class properties). Permissive on purpose so we don't reject any\n * file the upstream could have handled. Returns `null` when parse\n * fails — caller falls back to the raw source string.\n * @param source Source text.\n * @returns Parsed AST, or null on parse failure.\n */\nfunction parseUserSource(source: string): File | null {\n try {\n return parse(source, {\n sourceType: 'unambiguous',\n allowReturnOutsideFunction: true,\n allowImportExportEverywhere: true,\n plugins: ['typescript', 'jsx'],\n }) as unknown as File\n } catch {\n try {\n return parse(source, {\n sourceType: 'unambiguous',\n allowReturnOutsideFunction: true,\n allowImportExportEverywhere: true,\n plugins: ['flow', 'jsx'],\n }) as unknown as File\n } catch {\n return null\n }\n }\n}\n\n/**\n * Print Tailwind-shaped candidates oxide picked up but the parser\n * could NOT compile — typo, missing custom utility, or class not in\n * the user's theme. Filtering by candidates that ALSO appear inside a\n * `className=\"…\"` literal eliminates false positives from imports,\n * comments, and JSX prop values.\n * @param source Original source text — searched for className literals.\n * @param candidates Every candidate oxide surfaced from the source.\n * @param atoms Successfully resolved atoms (keys are class names).\n * @param filename Source path, prefixed onto the warning.\n * @param features Feature-atom maps (gradient / haptic) — their names are\n * known classes even though they carry no RN style, so they're excluded\n * from the unknown-class warning.\n */\nfunction warnUnknownClasses(\n source: string,\n candidates: readonly string[],\n atoms: ReadonlyMap<string, unknown>,\n filename: string,\n features: ReadonlyArray<ReadonlyMap<string, unknown>> = [],\n): void {\n // Feature atoms (gradient / haptic) resolve to no RN style, so they're\n // absent from `atoms` — but they're NOT unknown. Fold their names into\n // the known set so `active:haptic-rigid` etc. don't warn at build time.\n const known = new Set(atoms.keys())\n for (const map of features) for (const name of map.keys()) known.add(name)\n const unknown = filterUnknownClassCandidates(source, candidates, known)\n if (unknown.length === 0) return\n // eslint-disable-next-line no-console\n console.warn(`rnwind: unknown class${unknown.length > 1 ? 'es' : ''} in ${filename}: ${unknown.join(', ')}`)\n}\n\n/**\n * Extract the bare extension for oxide / internal switches.\n * @param filename Absolute path.\n * @returns Extension without the leading dot (`tsx` / `ts` / `js` / `jsx`).\n */\nfunction extensionOf(filename: string): string {\n const index = filename.lastIndexOf('.')\n if (index === -1) return 'tsx'\n return filename.slice(index + 1)\n}\n\n/**\n * Read the project root Metro hands us per-transform. Falls back to\n * `process.cwd()` only when the upstream harness doesn't set it (unit\n * tests, standalone). Metro's production pipeline always sets it.\n * @param args Metro transformer args.\n * @returns Absolute project root.\n */\nfunction projectRootOf(args: BabelTransformerArgs): string {\n const fromOptions = args.options?.projectRoot\n if (typeof fromOptions === 'string' && fromOptions.length > 0) return fromOptions\n return process.cwd()\n}\n\n/**\n * Whether a `.css` filename is the user's theme entry (the file\n * `withRnwindConfig` pointed us at via `RNWIND_CSS_ENTRY_FILE`).\n * Only the theme CSS should trigger a scheme rebuild — unrelated CSS\n * files in the project stay invisible to rnwind.\n * @param filename Absolute CSS path.\n * @returns Whether the file is the configured theme entry.\n */\nfunction isThemeCssEntry(filename: string): boolean {\n const cssEntry = process.env.RNWIND_CSS_ENTRY_FILE\n return typeof cssEntry === 'string' && cssEntry.length > 0 && cssEntry === filename\n}\n\n/**\n * Wrap host imports + compile any className literals, then regenerate\n * source. Two paths:\n * - **className present**: oxide-scan the file, record its atoms into\n * the union, and inject the generated-style + theme-signature\n * side-effect imports so the runtime registries populate.\n * - **import-only** (a `{...rest}` forwarder or a leaf with no literal\n * `className=`): just wrap the host imports so a forwarded className\n * still resolves at render — no oxide scan, no injected imports.\n *\n * On parse failure, fall back to the original source — a transient parse\n * error shouldn't crash Metro for a file the upstream might handle fine.\n * @param args Metro args; `src` is the original source text.\n * @returns Rewritten source text.\n */\nasync function rewriteSource(args: BabelTransformerArgs): Promise<string> {\n // SCAN FIRST — exactly like Tailwind: oxide extracts class candidates from\n // the WHOLE file content (className, cva/clsx, plain strings, anywhere),\n // and the compiler is the only filter. No babel parse needed to scan, so\n // the common \"file with no classes\" case stays cheap and the source is\n // returned untouched. This is what makes adding a class ANYWHERE register\n // on hot-reload, not just inside a `className=` or a known helper call.\n const state = getRnwindState(projectRootOf(args))\n const extension = extensionOf(args.filename)\n const parsed = await state.parser.parseAtoms({ content: args.src, extension })\n const hasAtoms = parsed.atoms.size > 0\n\n const hasClassName = /classname=/i.test(args.src)\n const hasSpread = /\\{\\s*\\.\\.\\./.test(args.src)\n\n // Nothing for rnwind to do: no Tailwind class compiled AND no host\n // wrapping needed. Drop any stale contribution and emit the file as-is.\n if (!hasAtoms && !hasClassName && !hasSpread) {\n state.builder.dropFile(args.filename)\n return args.src\n }\n\n const ast = parseUserSource(args.src)\n if (!ast) {\n // Can't parse to wrap/inject, but we DID scan — still record the atoms so\n // they register (a malformed-but-recoverable file shouldn't lose styles).\n if (hasAtoms) {\n await state.builder.recordFile(args.filename, parsed.atoms, parsed.keyframes, [])\n await state.builder.writeSchemes()\n } else {\n state.builder.dropFile(args.filename)\n }\n return args.src\n }\n\n // Wrap host imports ONLY when className flows through a component — written\n // (`hasClassName`) or forwarded (`{...rest}`). A `useCss`/`cva`-only file\n // resolves manually, so its imports (e.g. skia drawing primitives) are\n // left untouched. Mutates the AST in place; both branches below regenerate.\n if (hasClassName || hasSpread) rewriteWrapImports(ast, getWrapModules())\n\n if (!hasAtoms) {\n // Wrap-only forwarder — no classes to record/register, but className\n // still flows through it (`hasClassName || hasSpread`). It MUST hold a\n // dep-graph edge to the theme CSS so a theme edit re-transforms it and\n // its forwarded className resolves against the new scheme — otherwise it\n // renders stale. Inject the theme-signature import even with no atoms.\n state.builder.dropFile(args.filename)\n injectThemeSignatureImport(ast)\n return generateModule(ast).code\n }\n\n warnUnknownClasses(args.src, parsed.candidates, parsed.atoms, args.filename, [parsed.gradientAtoms, parsed.hapticAtoms])\n const literals = collectClassNameLiterals(ast)\n const { changed } = await state.builder.recordFile(args.filename, parsed.atoms, parsed.keyframes, literals)\n if (changed) await state.builder.writeSchemes()\n\n injectSideEffectImports(ast, STYLE_SPECIFIERS)\n injectThemeSignatureImport(ast)\n return generateModule(ast).code\n}\n\n/**\n * Whether a JSX attribute names a className-style prop (`className` or\n * any `<prefix>ClassName`).\n * @param node JSX attribute node.\n * @returns True when the attribute is a className prop.\n */\nfunction isClassNameAttribute(node: t.JSXAttribute): boolean {\n if (!t.isJSXIdentifier(node.name)) return false\n const {name} = node.name\n return name === 'className' || name.endsWith('ClassName')\n}\n\n/**\n * Pull static string literals out of a className expression. Handles a\n * bare string, a no-substitution template, and the branches of a\n * ternary / `&&` (so `cond ? 'a' : 'b'` and `flag && 'x'` both register\n * their literals). Dynamic interpolations are skipped — they resolve via\n * the runtime atom path.\n * @param expr Expression inside a `className={...}` container.\n * @param out Accumulator for discovered literals.\n */\nfunction collectLiteralsFromExpression(expr: t.Expression | t.JSXEmptyExpression | null | undefined, out: string[]): void {\n if (!expr) return\n if (t.isStringLiteral(expr)) {\n out.push(expr.value)\n return\n }\n if (t.isTemplateLiteral(expr) && expr.expressions.length === 0 && expr.quasis.length === 1) {\n const cooked = expr.quasis[0]?.value.cooked\n if (typeof cooked === 'string') out.push(cooked)\n return\n }\n if (t.isConditionalExpression(expr)) {\n collectLiteralsFromExpression(expr.consequent, out)\n collectLiteralsFromExpression(expr.alternate, out)\n return\n }\n if (t.isLogicalExpression(expr)) {\n collectLiteralsFromExpression(expr.right as t.Expression, out)\n }\n}\n\n/** AST node keys the literal walk skips — position / comment metadata. */\nconst SKIP_WALK_KEYS = new Set(['type', 'loc', 'start', 'end', 'range', 'leadingComments', 'trailingComments', 'innerComments'])\n\n/**\n * Collect the static literals from one className JSX attribute into the\n * dedup accumulator.\n * @param attribute The (already className-matched) JSX attribute.\n * @param seen Dedup set of literals already collected.\n * @param out Ordered accumulator.\n */\nfunction collectAttributeLiterals(attribute: t.JSXAttribute, seen: Set<string>, out: string[]): void {\n const { value } = attribute\n const found: string[] = []\n if (t.isStringLiteral(value)) found.push(value.value)\n else if (t.isJSXExpressionContainer(value)) collectLiteralsFromExpression(value.expression, found)\n for (const literal of found) {\n if (seen.has(literal)) continue\n seen.add(literal)\n out.push(literal)\n }\n}\n\n/**\n * Walk the AST for every `className=` / `<prefix>ClassName=` literal so\n * the builder can pre-merge each into a per-scheme molecule. A generic\n * node walk (no scope build) keeps it cheap; only JSX attribute nodes do\n * any work.\n * @param ast Parsed Babel file.\n * @returns Distinct literal className strings, in first-seen order.\n */\nfunction collectClassNameLiterals(ast: File): readonly string[] {\n const out: string[] = []\n const seen = new Set<string>()\n const visit = (node: unknown): void => {\n if (!node || typeof node !== 'object') return\n if (Array.isArray(node)) {\n for (const child of node) visit(child)\n return\n }\n const typed = node as { type?: string; [key: string]: unknown }\n if (typeof typed.type !== 'string') return\n if (typed.type === 'JSXAttribute' && isClassNameAttribute(node as t.JSXAttribute)) {\n collectAttributeLiterals(node as t.JSXAttribute, seen, out)\n }\n for (const key in typed) {\n if (SKIP_WALK_KEYS.has(key)) continue\n visit(typed[key])\n }\n }\n visit(ast.program)\n return out\n}\n\n/**\n * Prepend side-effect imports (`import '<spec>'`) so the generated\n * per-scheme style + manifest modules load — registering this file's\n * atoms / molecules / features into the runtime registries the wrapper's\n * `resolve` reads.\n * @param ast Babel File AST to mutate in place.\n * @param specifiers Module specifiers to side-effect-import.\n */\nfunction injectSideEffectImports(ast: File, specifiers: readonly string[]): void {\n for (const specifier of specifiers) {\n ast.program.body.unshift(t.importDeclaration([], t.stringLiteral(specifier)))\n }\n}\n\n/**\n * Prepend `import 'rnwind/__generated/theme-signature'` to every\n * rnwind-transformed file. The resolver maps that specifier to the\n * user's theme CSS so Metro's dependency graph carries a real edge\n * from this JS file to the CSS. When the user edits `global.css`,\n * the CSS module's SHA1 changes, and Metro invalidates every JS file\n * holding this import — forcing them to re-transform with the new\n * theme. The `.css` branch in {@link transform} returns an empty\n * `export {}` module so the runtime cost is one extra `require()`.\n * @param ast Babel File AST to mutate in place.\n */\nfunction injectThemeSignatureImport(ast: File): void {\n const declaration = t.importDeclaration([], t.stringLiteral(THEME_SIGNATURE_MODULE))\n ast.program.body.unshift(declaration)\n}\n\n/**\n * Read the upstream transformer's `getCacheKey()` so our cache-key\n * contribution composes with — rather than replaces — whatever the\n * host framework wants to mix in.\n * @returns Upstream cache key, or `null` when no upstream exposes one.\n */\nfunction loadUpstreamCacheKey(): string | null {\n const upstream = loadUpstream() as (UpstreamTransformer & { getCacheKey?: () => string }) | null\n if (!upstream) return null\n try {\n return typeof upstream.getCacheKey === 'function' ? upstream.getCacheKey() : null\n } catch {\n return null\n }\n}\n\n/**\n * Invoke the upstream `babelTransformerPath` Metro originally had\n * configured. The path is read from `RNWIND_UPSTREAM_TRANSFORMER`,\n * which `withRnwindConfig` sets at Metro startup. When the env var is\n * unset (unit tests, standalone use), fall back to a typescript+jsx\n * parse.\n * @param args Metro's per-file args.\n * @returns Upstream transform result containing the post-babel AST.\n */\nasync function runUpstream(args: BabelTransformerArgs): Promise<BabelTransformerResult> {\n if (args.ast && !process.env[UPSTREAM_ENV]) return { ast: args.ast }\n const upstream = loadUpstream()\n if (upstream) return await Promise.resolve(upstream.transform(args))\n if (args.ast) return { ast: args.ast }\n return { ast: parseSource(args.src) }\n}\n\n/**\n * Lazily require the upstream transformer module. Cached after first\n * load so per-file overhead is one cache lookup.\n * @returns Upstream module, or null when env is unset.\n */\nfunction loadUpstream(): UpstreamTransformer | null {\n if (cachedUpstream) return cachedUpstream\n const upstreamPath = process.env[UPSTREAM_ENV]\n if (!upstreamPath || upstreamPath.length === 0) return null\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires\n const required = require(upstreamPath) as UpstreamTransformer | { default?: UpstreamTransformer }\n const upstream = (required as { default?: UpstreamTransformer }).default ?? (required as UpstreamTransformer)\n if (typeof upstream.transform !== 'function') return null\n cachedUpstream = upstream\n return upstream\n } catch (error) {\n // eslint-disable-next-line no-console\n if (process.env.RNWIND_DEBUG) console.error('rnwind: failed to load upstream transformer:', error)\n return null\n }\n}\n\n/**\n * Cheap guard — the file has to look JS/TS, live outside `node_modules`,\n * and mention `className=` before we spend AST cycles on it.\n *\n * Symlink awareness: monorepo workspaces (yarn / pnpm / bun workspaces)\n * symlink each package into the consumer's `node_modules/<name>`, so a\n * file from `packages/ui/src/Foo.tsx` ends up reaching the transformer\n * as `<root>/node_modules/ui/src/Foo.tsx`. The naïve `/node_modules/`\n * check would skip every workspace UI file. We `realpath` the filename\n * once and only bail when the resolved real path is ALSO under\n * node_modules — true third-party installs.\n * @param args Metro args.\n * @returns Whether the file might need the rnwind pass.\n */\nfunction isRewriteCandidate(args: BabelTransformerArgs): boolean {\n if (!/\\.(?:tsx|ts|jsx|js)$/i.test(args.filename)) return false\n // EVERY user source file is scanned — exactly like Tailwind walks its\n // whole content set. The compiler is the only filter (see rewriteSource);\n // no className/helper pre-filter, because that \"filter magic\" missed\n // classes living in cva/clsx/plain-string files and broke their hot-reload.\n // (Cheap when the file has no classes: oxide finds nothing, no babel parse,\n // source returned untouched.)\n if (!args.filename.includes('/node_modules/')) return true\n // node_modules in path → could be a workspace symlink; resolve it.\n try {\n return !realpathSync(args.filename).includes('/node_modules/')\n } catch {\n // realpath failed (broken symlink, missing file). Fall back to skipping.\n return false\n }\n}\n\n/**\n * Fallback parse when no upstream is configured AND Metro didn't hand\n * us an AST. Used by unit tests and standalone setups.\n * @param source Source text.\n * @returns Parsed Babel File.\n */\nfunction parseSource(source: string): File {\n return parse(source, { sourceType: 'module', plugins: ['typescript', 'jsx'] }) as unknown as File\n}\n\n/** Metro's babel transformer signature. */\nexport interface BabelTransformerArgs {\n filename: string\n src: string\n options: { projectRoot?: string; [key: string]: unknown }\n ast?: File\n plugins?: readonly unknown[]\n}\n\n/** Return shape Metro expects from a babel transformer. */\nexport interface BabelTransformerResult {\n ast: File\n metadata?: unknown\n}\n\n/**\n * rnwind's Metro babel transformer. Two phases per source file:\n *\n * 1. **Pre-process the source string before handing it to the upstream\n * babel pipeline.** babel-preset-expo / React's JSX transform run\n * inside the upstream and convert `<View className=\"...\"/>` into\n * `React.createElement(View, {className})`. If we walked the AST\n * AFTER the upstream, there'd be no JSX attributes left to\n * rewrite. So we parse, run our pass, regenerate code, and feed\n * THAT to the upstream as `src`.\n * 2. **Delegate to the upstream `babelTransformerPath`** (Expo's\n * default handles Flow stripping, expo-router macros, etc.).\n *\n * Skip both phases when the file isn't a JS/TS source under user\n * code, or doesn't mention `className=` — hand straight to upstream.\n * @param args Metro's per-file args.\n * @returns Mutated AST + metadata.\n */\nexport async function transform(args: BabelTransformerArgs): Promise<BabelTransformerResult> {\n // Short-circuit `.css` inputs: the theme CSS is pulled into the dep\n // graph as a sentinel (see `THEME_SIGNATURE_MODULE` in resolver.ts)\n // so Metro watches it and invalidates importers on edit, but the\n // file's CSS syntax can't go through a JS babel transformer.\n //\n // When the CSS being transformed IS the user's theme entry, we\n // piggyback on Metro's own file-watcher: Metro calls us here on\n // every CSS save; we trigger `onThemeChange` to rebuild parser +\n // rewrite scheme files with the new values. Metro's dep graph then\n // HMRs the regenerated `common.style.js` to the running app.\n //\n // Emitting the CSS content hash in the fake JS output is what makes\n // Metro propagate invalidation to downstream importers — constant\n // `export {}` bytes would never look changed and Metro would skip\n // the chain.\n if (args.filename.endsWith('.css')) {\n if (isThemeCssEntry(args.filename)) {\n try {\n await onThemeChange(projectRootOf(args))\n } catch {\n // CSS edit happened outside a configured project (e.g. tests).\n }\n }\n const themeHash = createHash('sha256').update(args.src).digest('hex').slice(0, 16)\n const stub = `export const __rnwindThemeHash = ${JSON.stringify(themeHash)};\\n`\n return { ast: parse(stub, { sourceType: 'module' }) as unknown as File }\n }\n if (!isRewriteCandidate(args)) {\n if (/\\.(?:tsx|ts|jsx|js)$/i.test(args.filename) && !args.filename.includes('/node_modules/')) {\n try {\n getRnwindState(projectRootOf(args)).builder.dropFile(args.filename)\n } catch {\n // State not configured (e.g. test). Nothing to drop.\n }\n }\n return runUpstream(args)\n }\n\n const rewrittenSource = await rewriteSource(args)\n return runUpstream({ ...args, src: rewrittenSource, ast: undefined })\n}\n\n/**\n * Metro's babel-transformer contract: a `getCacheKey()` export is\n * sampled per-file and mixed into the transform cache key. Returning\n * a string that includes the theme CSS content hash invalidates every\n * cached transform on every CSS edit — so the bundle rebuilds with\n * the new theme automatically on the next request.\n * @returns Cache-key segment that includes rnwind's current theme hash.\n */\nexport function getCacheKey(): string {\n const upstreamKey = loadUpstreamCacheKey()\n const ownKey = getRnwindCacheKey()\n return upstreamKey ? `${upstreamKey}|${ownKey}` : ownKey\n}\n\n/** Test-only — drop the cached upstream so a new env var picks up next call. */\nexport function __resetUpstreamCache(): void {\n cachedUpstream = null\n}\n"],"names":["parse","filterUnknownClassCandidates","state","getRnwindState","rewriteWrapImports","getWrapModules","STYLE_SPECIFIERS","t","THEME_SIGNATURE_MODULE","realpathSync","onThemeChange","createHash","getRnwindCacheKey"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBA;AACA,MAAM,YAAY,GAAG,6BAA6B;AAElD;AACA,IAAI,cAAc,GAA+B,IAAI;AAErD,MAAM,cAAc,GAAI,QAAqD,CAAC,OAAO,IAAI,QAAQ;AAEjG;;;;;;;AAOG;AACH,SAAS,eAAe,CAAC,MAAc,EAAA;AACrC,IAAA,IAAI;QACF,OAAOA,YAAK,CAAC,MAAM,EAAE;AACnB,YAAA,UAAU,EAAE,aAAa;AACzB,YAAA,0BAA0B,EAAE,IAAI;AAChC,YAAA,2BAA2B,EAAE,IAAI;AACjC,YAAA,OAAO,EAAE,CAAC,YAAY,EAAE,KAAK,CAAC;AAC/B,SAAA,CAAoB;IACvB;AAAE,IAAA,MAAM;AACN,QAAA,IAAI;YACF,OAAOA,YAAK,CAAC,MAAM,EAAE;AACnB,gBAAA,UAAU,EAAE,aAAa;AACzB,gBAAA,0BAA0B,EAAE,IAAI;AAChC,gBAAA,2BAA2B,EAAE,IAAI;AACjC,gBAAA,OAAO,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC;AACzB,aAAA,CAAoB;QACvB;AAAE,QAAA,MAAM;AACN,YAAA,OAAO,IAAI;QACb;IACF;AACF;AAEA;;;;;;;;;;;;;AAaG;AACH,SAAS,kBAAkB,CACzB,MAAc,EACd,UAA6B,EAC7B,KAAmC,EACnC,QAAgB,EAChB,QAAA,GAAwD,EAAE,EAAA;;;;IAK1D,MAAM,KAAK,GAA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gBAAAG,oBAAc,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC;YACrE;AAAE,YAAA,MAAM;;YAER;QACF;AACA,QAAA,OAAO,WAAW,CAAC,IAAI,CAAC;IAC1B;AAEA,IAAA,MAAM,eAAe,GAAG,MAAM,aAAa,CAAC,IAAI,CAAC;AACjD,IAAA,OAAO,WAAW,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC;AACvE;AAEA;;;;;;;AAOG;SACa,WAAW,GAAA;AACzB,IAAA,MAAM,WAAW,GAAG,oBAAoB,EAAE;AAC1C,IAAA,MAAM,MAAM,GAAGS,uBAAiB,EAAE;AAClC,IAAA,OAAO,WAAW,GAAG,CAAA,EAAG,WAAW,CAAA,CAAA,EAAI,MAAM,CAAA,CAAE,GAAG,MAAM;AAC1D;AAEA;SACgB,oBAAoB,GAAA;IAClC,cAAc,GAAG,IAAI;AACvB;;;;;;"}
|
|
@@ -82,6 +82,58 @@ function resolveCacheDir(projectRoot, override) {
|
|
|
82
82
|
return path.resolve(projectRoot, DEFAULT_CACHE_DIR);
|
|
83
83
|
return path.isAbsolute(override) ? override : path.resolve(projectRoot, override);
|
|
84
84
|
}
|
|
85
|
+
/** Cache dirs already wiped this process — guards the reset so it fires once per dir, not on every worker (re)configure. */
|
|
86
|
+
const wipedCacheDirs = new Set();
|
|
87
|
+
/**
|
|
88
|
+
* CLI flags that mean "reset the cache". `metro.config.js` (→ this function)
|
|
89
|
+
* is evaluated BEFORE Metro/Expo merge the flag onto the config object
|
|
90
|
+
* (`overrideConfigWithArguments` / Expo's `instantiateMetro` both set
|
|
91
|
+
* `resetCache` AFTER loading the config), so `config.resetCache` is still its
|
|
92
|
+
* `false` default here. The reliable signal at config-eval time is the CLI
|
|
93
|
+
* argv of the same process: `react-native start --reset-cache`, Metro
|
|
94
|
+
* `--reset-cache`, and Expo `start -c` / `--clear`.
|
|
95
|
+
*/
|
|
96
|
+
const RESET_CACHE_ARGS = new Set(['--reset-cache', '--resetCache', '--clear', '-c']);
|
|
97
|
+
/**
|
|
98
|
+
* Whether the dev asked for a cache reset — true if the (post-merge) config
|
|
99
|
+
* flag is set OR the CLI argv carries a reset flag. Wiping on a false positive
|
|
100
|
+
* is harmless (the cache just regenerates), so argv detection errs toward
|
|
101
|
+
* honoring the request.
|
|
102
|
+
* @param resetCache Metro's `config.resetCache` (usually still false at config-eval time).
|
|
103
|
+
* @param argv Process argv (injectable for tests).
|
|
104
|
+
* @returns True when the cache dir should be wiped.
|
|
105
|
+
*/
|
|
106
|
+
function isResetCacheRequested(resetCache, argv = process.argv) {
|
|
107
|
+
if (resetCache === true)
|
|
108
|
+
return true;
|
|
109
|
+
return argv.some((argument) => RESET_CACHE_ARGS.has(argument));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* On a Metro `--reset-cache` run (Expo `start -c` / `--clear`) the dev
|
|
113
|
+
* explicitly asked for a clean slate, so rnwind's generated cache dir
|
|
114
|
+
* (`*.style.js` scheme chunks + `schemes.js` manifest) is stale and must be
|
|
115
|
+
* wiped before `ensureFilesExist` regenerates it — otherwise old chunks
|
|
116
|
+
* survive a reset and Metro's haste-map indexes dead bytes. Only ever removes
|
|
117
|
+
* the resolved cache dir rnwind owns, never the project root, and never throws
|
|
118
|
+
* when the dir is already absent. Idempotent per dir within one process.
|
|
119
|
+
* @param cacheDir Absolute path to rnwind's cache dir.
|
|
120
|
+
* @param resetCache Metro's resolved `config.resetCache` flag.
|
|
121
|
+
* @param projectRoot Absolute project root — guards against ever wiping it.
|
|
122
|
+
*/
|
|
123
|
+
function resetCacheDirIfRequested(cacheDir, resetCache, projectRoot) {
|
|
124
|
+
if (!isResetCacheRequested(resetCache))
|
|
125
|
+
return;
|
|
126
|
+
if (wipedCacheDirs.has(cacheDir))
|
|
127
|
+
return;
|
|
128
|
+
// Hard safety: never rm the project root (or an ancestor of it) even if a
|
|
129
|
+
// mis-configured cacheDir resolves there.
|
|
130
|
+
const resolvedCache = path.resolve(cacheDir);
|
|
131
|
+
const resolvedRoot = path.resolve(projectRoot);
|
|
132
|
+
if (resolvedCache === resolvedRoot || resolvedRoot.startsWith(resolvedCache + path.sep))
|
|
133
|
+
return;
|
|
134
|
+
wipedCacheDirs.add(cacheDir);
|
|
135
|
+
node_fs.rmSync(resolvedCache, { recursive: true, force: true });
|
|
136
|
+
}
|
|
85
137
|
/**
|
|
86
138
|
* Read the theme CSS and extract `@variant <name>` blocks for the .d.ts
|
|
87
139
|
* generator. Forces construction of `getRnwindState`, then reads
|
|
@@ -127,6 +179,10 @@ function withRnwindConfig(metroConfig, options) {
|
|
|
127
179
|
const projectRoot = options.projectRoot ?? metroConfig.projectRoot ?? process.cwd();
|
|
128
180
|
const cacheDir = resolveCacheDir(projectRoot, options.cacheDir);
|
|
129
181
|
const cssEntry = path.isAbsolute(options.cssEntryFile) ? options.cssEntryFile : path.resolve(projectRoot, options.cssEntryFile);
|
|
182
|
+
// Honor Metro's `--reset-cache`: wipe rnwind's stale generated dir BEFORE we
|
|
183
|
+
// recreate it + before `ensureFilesExist` regenerates and the haste-map
|
|
184
|
+
// indexes it. Synchronous so the dir is clean by the time this returns.
|
|
185
|
+
resetCacheDirIfRequested(cacheDir, metroConfig.resetCache, projectRoot);
|
|
130
186
|
node_fs.mkdirSync(cacheDir, { recursive: true });
|
|
131
187
|
const watchFolders = (metroConfig.watchFolders ?? []).filter((p) => typeof p === 'string' && p.length > 0);
|
|
132
188
|
state.configureRnwindState(cssEntry, cacheDir, watchFolders, options.wrapModules);
|
|
@@ -172,5 +228,6 @@ function withRnwindConfig(metroConfig, options) {
|
|
|
172
228
|
return metroConfig;
|
|
173
229
|
}
|
|
174
230
|
|
|
231
|
+
exports.isResetCacheRequested = isResetCacheRequested;
|
|
175
232
|
exports.withRnwindConfig = withRnwindConfig;
|
|
176
233
|
//# sourceMappingURL=with-config.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-config.cjs","sources":["../../../../src/metro/with-config.ts"],"sourcesContent":["import { existsSync, mkdirSync, watch as watchFile } from 'node:fs'\nimport path from 'node:path'\nimport { writeDtsFile } from './dts'\nimport { createRnwindResolver, type ResolveRequestFn } from './resolver'\nimport { configureRnwindState, getRnwindState, onThemeChange } from './state'\n\n/** Default cache directory at the project root. Visible for debugging. */\nconst DEFAULT_CACHE_DIR = '.rnwind'\n\n/**\n * Active CSS watcher — replaced (and the prior one closed) when\n * `withRnwindConfig` is called again (Metro restart, repeated init).\n * Only one watcher per process; no stacking.\n */\nlet activeCssWatcher: { cssPath: string; close: () => void } | null = null\n\n/**\n * Watch the theme CSS for edits. On change, rebuild state against the fresh\n * CSS and rewrite the per-scheme files (`onThemeChange` → full rescan →\n * `writeSchemes`). That's the entire HMR signal: every transformed source\n * imports `rnwind/__generated/schemes`, which eager-imports\n * `common.style.js`; the rewrite changes its bytes, Metro's content-SHA1\n * dedup notices, and every importer is invalidated + re-bundled with the new\n * style values. The rewritten JSX references atoms by NAME (theme-independent),\n * so no source-file re-transform / mtime nudge is needed — the dep graph\n * carries the signal.\n * @param cssPath Absolute path to the theme CSS to watch.\n * @param projectRoot Metro's project root (for `getRnwindState`).\n */\nfunction watchThemeCss(cssPath: string, projectRoot: string): void {\n if (activeCssWatcher?.cssPath === cssPath) return\n activeCssWatcher?.close()\n if (!existsSync(cssPath)) return\n let pending = false\n const watcher = watchFile(cssPath, { persistent: false }, () => {\n // Debounce: editors often emit 2-3 change events per save (atomic\n // rename dance, tmp files). Coalesce to ONE rebuild per microtask.\n if (pending) return\n pending = true\n queueMicrotask(async () => {\n pending = false\n try {\n await onThemeChange(projectRoot)\n } catch {\n // Invalidation is best-effort — never crash the dev server.\n }\n })\n })\n activeCssWatcher = { cssPath, close: () => watcher.close() }\n}\n\n/**\n * Where the rnwind babel transformer lives — resolved relative to this\n * module so the path works from both the `src/` tree (tests) and the\n * built `lib/` output. Tries a few extensions because `require.resolve`\n * doesn't auto-find `.cjs` / `.mjs` from a bare specifier.\n * @returns Absolute path to the rnwind transformer module.\n */\nfunction transformerPath(): string {\n for (const candidate of ['./transformer.cjs', './transformer.mjs', './transformer.js', './transformer.ts', './transformer']) {\n try {\n return require.resolve(candidate)\n } catch {\n // try the next extension\n }\n }\n throw new Error('rnwind: could not resolve the metro transformer path')\n}\n\n/**\n * Resolve the effective cache directory, honoring a user override and\n * falling back to `<projectRoot>/.rnwind`.\n * @param projectRoot Anchor for relative paths.\n * @param override User-supplied option.\n * @returns Absolute cache directory.\n */\nfunction resolveCacheDir(projectRoot: string, override: string | undefined): string {\n if (!override || override.length === 0) return path.resolve(projectRoot, DEFAULT_CACHE_DIR)\n return path.isAbsolute(override) ? override : path.resolve(projectRoot, override)\n}\n\n/**\n * Read the theme CSS and extract `@variant <name>` blocks for the .d.ts\n * generator. Forces construction of `getRnwindState`, then reads\n * `parser.declaredSchemes` (populated synchronously at construction).\n * @param cssEntry Absolute path to theme CSS.\n * @param projectRoot\n * @returns Scheme names (empty when the theme has no variants; `'base'` is filtered).\n */\nfunction discoverSchemes(cssEntry: string, projectRoot: string): readonly string[] {\n if (!existsSync(cssEntry)) return []\n try {\n const { parser } = getRnwindState(projectRoot)\n return parser.declaredSchemes.filter((name) => name !== 'base')\n } catch {\n return []\n }\n}\n\n/** User-facing options for `withRnwindConfig`. */\nexport interface RnwindMetroOptions {\n /** Path to the theme CSS (absolute or relative to `projectRoot`). Required. */\n cssEntryFile: string\n /** Where rnwind writes the `.d.ts` file. Set `false` to disable. Defaults to `<projectRoot>/rnwind-types.d.ts`. */\n dtsFile?: string | false\n /** Optional project-root override — defaults to `metroConfig.projectRoot` then `process.cwd()`. */\n projectRoot?: string\n /** Cache directory. Absolute, or relative to `projectRoot`. Default: `.rnwind` at project root. */\n cacheDir?: string\n /**\n * Extra module specifiers whose component exports rnwind should\n * auto-wrap at import sites — `import { View } from 'react-native'`\n * becomes `const View = wrap(_rnw0)` so `<View className=\"…\">` resolves\n * styles at runtime. Merged with the built-in defaults: `react-native`,\n * `react-native-reanimated`, `react-native-svg`,\n * `react-native-gesture-handler`, `react-native-safe-area-context`,\n * `expo-linear-gradient`, `expo-image`, and more.\n *\n * A module NOT in this list keeps its raw imports — the importing\n * component receives `className` as a plain prop and can resolve it\n * via `useCss` / `wrap` itself. Use this to opt your design-system /\n * UI primitive packages into the auto-wrap path.\n */\n wrapModules?: readonly string[]\n}\n\n/** Shape we mutate on Metro's config. Loose so we don't pin Metro's internal types. */\nexport interface MetroConfigLike {\n projectRoot?: string\n watchFolders?: string[]\n transformer?: {\n babelTransformerPath?: string\n [key: string]: unknown\n }\n resolver?: {\n resolveRequest?: ResolveRequestFn | null\n [key: string]: unknown\n }\n [key: string]: unknown\n}\n\n/**\n * Wrap a Metro config with rnwind's pipeline:\n * - Install the rnwind babel transformer.\n * - Chain a `resolveRequest` hook that serves\n * `rnwind/__generated/schemes` from `<cacheDir>/schemes.js`.\n * - Write the `.d.ts` so TypeScript accepts `className=` on RN components.\n * - Publish the theme CSS path + cache dir via env so Metro workers\n * can rebuild their local state.\n * - Ensure the cache dir is a watched folder Metro's haste-map indexes.\n *\n * Theme-edit hot reload happens implicitly: every transformed file\n * imports `rnwind/__generated/schemes`, and that module eager-imports\n * `common.style.js`. When the theme changes, the per-scheme files\n * regenerate with new bytes; Metro's content SHA1 dedup detects the\n * change and invalidates every importer automatically.\n * `getCacheKey()` on the transformer covers the per-file transform\n * cache. No file watcher / source-padding hack needed — the dep graph\n * carries the signal.\n * @param metroConfig Config from `getDefaultConfig(__dirname)` or equivalent.\n * @param options rnwind options.\n * @returns The same config, mutated.\n */\nexport function withRnwindConfig<C extends MetroConfigLike>(metroConfig: C, options: RnwindMetroOptions): C {\n const projectRoot = options.projectRoot ?? metroConfig.projectRoot ?? process.cwd()\n const cacheDir = resolveCacheDir(projectRoot, options.cacheDir)\n const cssEntry = path.isAbsolute(options.cssEntryFile) ? options.cssEntryFile : path.resolve(projectRoot, options.cssEntryFile)\n\n mkdirSync(cacheDir, { recursive: true })\n const watchFolders = (metroConfig.watchFolders ?? []).filter((p) => typeof p === 'string' && p.length > 0)\n configureRnwindState(cssEntry, cacheDir, watchFolders, options.wrapModules)\n\n // Warm the state eagerly (in the Metro master process) so oxide's\n // Scanner walks every project source (and every monorepo\n // watch-folder) ONCE and the manifest + scheme files hold the\n // complete union before Metro's resolver tries to SHA1 them on the\n // first transform. Each worker lazy-repeats this scan on its first\n // transform to converge on identical state.\n try {\n void getRnwindState(projectRoot).builder.ensureFilesExist()\n } catch {\n // Any init error surfaces again at the first transform; don't crash Metro boot.\n }\n\n // Install transformer + resolver. Capture the existing\n // babelTransformerPath BEFORE we override it — our worker chains to\n // it (env-passed) so Flow / expo-router / babel-preset-expo etc. all\n // continue to run.\n const existingTransformerPath = metroConfig.transformer?.babelTransformerPath\n if (typeof existingTransformerPath === 'string' && existingTransformerPath.length > 0) {\n process.env.RNWIND_UPSTREAM_TRANSFORMER = existingTransformerPath\n }\n const upstream = metroConfig.resolver?.resolveRequest ?? null\n metroConfig.transformer = { ...metroConfig.transformer, babelTransformerPath: transformerPath() }\n metroConfig.resolver = { ...metroConfig.resolver, resolveRequest: createRnwindResolver(upstream) }\n\n // Metro's haste-map indexes `watchFolders` at startup. Adding the\n // cache dir guarantees scheme style files + manifest get SHA1'd\n // without a \"Failed to get the SHA-1\" race when the first transform\n // writes them.\n const existingWatch = metroConfig.watchFolders ?? []\n metroConfig.watchFolders = existingWatch.includes(cacheDir) ? existingWatch : [...existingWatch, cacheDir]\n\n if (options.dtsFile !== false) {\n const dtsPath = options.dtsFile ?? path.resolve(projectRoot, 'rnwind-types.d.ts')\n const schemes = discoverSchemes(cssEntry, projectRoot)\n writeDtsFile(dtsPath, schemes)\n }\n\n // Watch the theme CSS. On edit, we rewrite scheme files AND touch\n // mtime on every transformed source file so Metro invalidates them\n // and re-transforms — the only reliable way to propagate theme\n // changes to an already-running dev server.\n watchThemeCss(cssEntry, projectRoot)\n\n return metroConfig\n}\n"],"names":["existsSync","watchFile","onThemeChange","getRnwindState","mkdirSync","configureRnwindState","createRnwindResolver","writeDtsFile"],"mappings":";;;;;;;;AAMA;AACA,MAAM,iBAAiB,GAAG,SAAS;AAEnC;;;;AAIG;AACH,IAAI,gBAAgB,GAAkD,IAAI;AAE1E;;;;;;;;;;;;AAYG;AACH,SAAS,aAAa,CAAC,OAAe,EAAE,WAAmB,EAAA;AACzD,IAAA,IAAI,gBAAgB,EAAE,OAAO,KAAK,OAAO;QAAE;IAC3C,gBAAgB,EAAE,KAAK,EAAE;AACzB,IAAA,IAAI,CAACA,kBAAU,CAAC,OAAO,CAAC;QAAE;IAC1B,IAAI,OAAO,GAAG,KAAK;AACnB,IAAA,MAAM,OAAO,GAAGC,aAAS,CAAC,OAAO,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,EAAE,MAAK;;;AAG7D,QAAA,IAAI,OAAO;YAAE;QACb,OAAO,GAAG,IAAI;QACd,cAAc,CAAC,YAAW;YACxB,OAAO,GAAG,KAAK;AACf,YAAA,IAAI;AACF,gBAAA,MAAMC,mBAAa,CAAC,WAAW,CAAC;YAClC;AAAE,YAAA,MAAM;;YAER;AACF,QAAA,CAAC,CAAC;AACJ,IAAA,CAAC,CAAC;AACF,IAAA,gBAAgB,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC,KAAK,EAAE,EAAE;AAC9D;AAEA;;;;;;AAMG;AACH,SAAS,eAAe,GAAA;AACtB,IAAA,KAAK,MAAM,SAAS,IAAI,CAAC,mBAAmB,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,eAAe,CAAC,EAAE;AAC3H,QAAA,IAAI;AACF,YAAA,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;QACnC;AAAE,QAAA,MAAM;;QAER;IACF;AACA,IAAA,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC;AACzE;AA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MAAM,OAAO,GAAG,eAAe,CAAC,QAAQ,EAAE,WAAW,CAAC;AACtD,QAAAC,gBAAY,CAAC,OAAO,EAAE,OAAO,CAAC;IAChC;;;;;AAMA,IAAA,aAAa,CAAC,QAAQ,EAAE,WAAW,CAAC;AAEpC,IAAA,OAAO,WAAW;AACpB;;;;"}
|
|
1
|
+
{"version":3,"file":"with-config.cjs","sources":["../../../../src/metro/with-config.ts"],"sourcesContent":["import { existsSync, mkdirSync, rmSync, watch as watchFile } from 'node:fs'\nimport path from 'node:path'\nimport { writeDtsFile } from './dts'\nimport { createRnwindResolver, type ResolveRequestFn } from './resolver'\nimport { configureRnwindState, getRnwindState, onThemeChange } from './state'\n\n/** Default cache directory at the project root. Visible for debugging. */\nconst DEFAULT_CACHE_DIR = '.rnwind'\n\n/**\n * Active CSS watcher — replaced (and the prior one closed) when\n * `withRnwindConfig` is called again (Metro restart, repeated init).\n * Only one watcher per process; no stacking.\n */\nlet activeCssWatcher: { cssPath: string; close: () => void } | null = null\n\n/**\n * Watch the theme CSS for edits. On change, rebuild state against the fresh\n * CSS and rewrite the per-scheme files (`onThemeChange` → full rescan →\n * `writeSchemes`). That's the entire HMR signal: every transformed source\n * imports `rnwind/__generated/schemes`, which eager-imports\n * `common.style.js`; the rewrite changes its bytes, Metro's content-SHA1\n * dedup notices, and every importer is invalidated + re-bundled with the new\n * style values. The rewritten JSX references atoms by NAME (theme-independent),\n * so no source-file re-transform / mtime nudge is needed — the dep graph\n * carries the signal.\n * @param cssPath Absolute path to the theme CSS to watch.\n * @param projectRoot Metro's project root (for `getRnwindState`).\n */\nfunction watchThemeCss(cssPath: string, projectRoot: string): void {\n if (activeCssWatcher?.cssPath === cssPath) return\n activeCssWatcher?.close()\n if (!existsSync(cssPath)) return\n let pending = false\n const watcher = watchFile(cssPath, { persistent: false }, () => {\n // Debounce: editors often emit 2-3 change events per save (atomic\n // rename dance, tmp files). Coalesce to ONE rebuild per microtask.\n if (pending) return\n pending = true\n queueMicrotask(async () => {\n pending = false\n try {\n await onThemeChange(projectRoot)\n } catch {\n // Invalidation is best-effort — never crash the dev server.\n }\n })\n })\n activeCssWatcher = { cssPath, close: () => watcher.close() }\n}\n\n/**\n * Where the rnwind babel transformer lives — resolved relative to this\n * module so the path works from both the `src/` tree (tests) and the\n * built `lib/` output. Tries a few extensions because `require.resolve`\n * doesn't auto-find `.cjs` / `.mjs` from a bare specifier.\n * @returns Absolute path to the rnwind transformer module.\n */\nfunction transformerPath(): string {\n for (const candidate of ['./transformer.cjs', './transformer.mjs', './transformer.js', './transformer.ts', './transformer']) {\n try {\n return require.resolve(candidate)\n } catch {\n // try the next extension\n }\n }\n throw new Error('rnwind: could not resolve the metro transformer path')\n}\n\n/**\n * Resolve the effective cache directory, honoring a user override and\n * falling back to `<projectRoot>/.rnwind`.\n * @param projectRoot Anchor for relative paths.\n * @param override User-supplied option.\n * @returns Absolute cache directory.\n */\nfunction resolveCacheDir(projectRoot: string, override: string | undefined): string {\n if (!override || override.length === 0) return path.resolve(projectRoot, DEFAULT_CACHE_DIR)\n return path.isAbsolute(override) ? override : path.resolve(projectRoot, override)\n}\n\n/** Cache dirs already wiped this process — guards the reset so it fires once per dir, not on every worker (re)configure. */\nconst wipedCacheDirs = new Set<string>()\n\n/**\n * CLI flags that mean \"reset the cache\". `metro.config.js` (→ this function)\n * is evaluated BEFORE Metro/Expo merge the flag onto the config object\n * (`overrideConfigWithArguments` / Expo's `instantiateMetro` both set\n * `resetCache` AFTER loading the config), so `config.resetCache` is still its\n * `false` default here. The reliable signal at config-eval time is the CLI\n * argv of the same process: `react-native start --reset-cache`, Metro\n * `--reset-cache`, and Expo `start -c` / `--clear`.\n */\nconst RESET_CACHE_ARGS: ReadonlySet<string> = new Set(['--reset-cache', '--resetCache', '--clear', '-c'])\n\n/**\n * Whether the dev asked for a cache reset — true if the (post-merge) config\n * flag is set OR the CLI argv carries a reset flag. Wiping on a false positive\n * is harmless (the cache just regenerates), so argv detection errs toward\n * honoring the request.\n * @param resetCache Metro's `config.resetCache` (usually still false at config-eval time).\n * @param argv Process argv (injectable for tests).\n * @returns True when the cache dir should be wiped.\n */\nexport function isResetCacheRequested(resetCache: boolean | undefined, argv: readonly string[] = process.argv): boolean {\n if (resetCache === true) return true\n return argv.some((argument) => RESET_CACHE_ARGS.has(argument))\n}\n\n/**\n * On a Metro `--reset-cache` run (Expo `start -c` / `--clear`) the dev\n * explicitly asked for a clean slate, so rnwind's generated cache dir\n * (`*.style.js` scheme chunks + `schemes.js` manifest) is stale and must be\n * wiped before `ensureFilesExist` regenerates it — otherwise old chunks\n * survive a reset and Metro's haste-map indexes dead bytes. Only ever removes\n * the resolved cache dir rnwind owns, never the project root, and never throws\n * when the dir is already absent. Idempotent per dir within one process.\n * @param cacheDir Absolute path to rnwind's cache dir.\n * @param resetCache Metro's resolved `config.resetCache` flag.\n * @param projectRoot Absolute project root — guards against ever wiping it.\n */\nfunction resetCacheDirIfRequested(cacheDir: string, resetCache: boolean | undefined, projectRoot: string): void {\n if (!isResetCacheRequested(resetCache)) return\n if (wipedCacheDirs.has(cacheDir)) return\n // Hard safety: never rm the project root (or an ancestor of it) even if a\n // mis-configured cacheDir resolves there.\n const resolvedCache = path.resolve(cacheDir)\n const resolvedRoot = path.resolve(projectRoot)\n if (resolvedCache === resolvedRoot || resolvedRoot.startsWith(resolvedCache + path.sep)) return\n wipedCacheDirs.add(cacheDir)\n rmSync(resolvedCache, { recursive: true, force: true })\n}\n\n/**\n * Read the theme CSS and extract `@variant <name>` blocks for the .d.ts\n * generator. Forces construction of `getRnwindState`, then reads\n * `parser.declaredSchemes` (populated synchronously at construction).\n * @param cssEntry Absolute path to theme CSS.\n * @param projectRoot\n * @returns Scheme names (empty when the theme has no variants; `'base'` is filtered).\n */\nfunction discoverSchemes(cssEntry: string, projectRoot: string): readonly string[] {\n if (!existsSync(cssEntry)) return []\n try {\n const { parser } = getRnwindState(projectRoot)\n return parser.declaredSchemes.filter((name) => name !== 'base')\n } catch {\n return []\n }\n}\n\n/** User-facing options for `withRnwindConfig`. */\nexport interface RnwindMetroOptions {\n /** Path to the theme CSS (absolute or relative to `projectRoot`). Required. */\n cssEntryFile: string\n /** Where rnwind writes the `.d.ts` file. Set `false` to disable. Defaults to `<projectRoot>/rnwind-types.d.ts`. */\n dtsFile?: string | false\n /** Optional project-root override — defaults to `metroConfig.projectRoot` then `process.cwd()`. */\n projectRoot?: string\n /** Cache directory. Absolute, or relative to `projectRoot`. Default: `.rnwind` at project root. */\n cacheDir?: string\n /**\n * Extra module specifiers whose component exports rnwind should\n * auto-wrap at import sites — `import { View } from 'react-native'`\n * becomes `const View = wrap(_rnw0)` so `<View className=\"…\">` resolves\n * styles at runtime. Merged with the built-in defaults: `react-native`,\n * `react-native-reanimated`, `react-native-svg`,\n * `react-native-gesture-handler`, `react-native-safe-area-context`,\n * `expo-linear-gradient`, `expo-image`, and more.\n *\n * A module NOT in this list keeps its raw imports — the importing\n * component receives `className` as a plain prop and can resolve it\n * via `useCss` / `wrap` itself. Use this to opt your design-system /\n * UI primitive packages into the auto-wrap path.\n */\n wrapModules?: readonly string[]\n}\n\n/** Shape we mutate on Metro's config. Loose so we don't pin Metro's internal types. */\nexport interface MetroConfigLike {\n projectRoot?: string\n watchFolders?: string[]\n /** Set by Metro when the dev runs `--reset-cache` (Expo `start -c`). Wipes rnwind's cache dir. */\n resetCache?: boolean\n transformer?: {\n babelTransformerPath?: string\n [key: string]: unknown\n }\n resolver?: {\n resolveRequest?: ResolveRequestFn | null\n [key: string]: unknown\n }\n [key: string]: unknown\n}\n\n/**\n * Wrap a Metro config with rnwind's pipeline:\n * - Install the rnwind babel transformer.\n * - Chain a `resolveRequest` hook that serves\n * `rnwind/__generated/schemes` from `<cacheDir>/schemes.js`.\n * - Write the `.d.ts` so TypeScript accepts `className=` on RN components.\n * - Publish the theme CSS path + cache dir via env so Metro workers\n * can rebuild their local state.\n * - Ensure the cache dir is a watched folder Metro's haste-map indexes.\n *\n * Theme-edit hot reload happens implicitly: every transformed file\n * imports `rnwind/__generated/schemes`, and that module eager-imports\n * `common.style.js`. When the theme changes, the per-scheme files\n * regenerate with new bytes; Metro's content SHA1 dedup detects the\n * change and invalidates every importer automatically.\n * `getCacheKey()` on the transformer covers the per-file transform\n * cache. No file watcher / source-padding hack needed — the dep graph\n * carries the signal.\n * @param metroConfig Config from `getDefaultConfig(__dirname)` or equivalent.\n * @param options rnwind options.\n * @returns The same config, mutated.\n */\nexport function withRnwindConfig<C extends MetroConfigLike>(metroConfig: C, options: RnwindMetroOptions): C {\n const projectRoot = options.projectRoot ?? metroConfig.projectRoot ?? process.cwd()\n const cacheDir = resolveCacheDir(projectRoot, options.cacheDir)\n const cssEntry = path.isAbsolute(options.cssEntryFile) ? options.cssEntryFile : path.resolve(projectRoot, options.cssEntryFile)\n\n // Honor Metro's `--reset-cache`: wipe rnwind's stale generated dir BEFORE we\n // recreate it + before `ensureFilesExist` regenerates and the haste-map\n // indexes it. Synchronous so the dir is clean by the time this returns.\n resetCacheDirIfRequested(cacheDir, metroConfig.resetCache, projectRoot)\n mkdirSync(cacheDir, { recursive: true })\n const watchFolders = (metroConfig.watchFolders ?? []).filter((p) => typeof p === 'string' && p.length > 0)\n configureRnwindState(cssEntry, cacheDir, watchFolders, options.wrapModules)\n\n // Warm the state eagerly (in the Metro master process) so oxide's\n // Scanner walks every project source (and every monorepo\n // watch-folder) ONCE and the manifest + scheme files hold the\n // complete union before Metro's resolver tries to SHA1 them on the\n // first transform. Each worker lazy-repeats this scan on its first\n // transform to converge on identical state.\n try {\n void getRnwindState(projectRoot).builder.ensureFilesExist()\n } catch {\n // Any init error surfaces again at the first transform; don't crash Metro boot.\n }\n\n // Install transformer + resolver. Capture the existing\n // babelTransformerPath BEFORE we override it — our worker chains to\n // it (env-passed) so Flow / expo-router / babel-preset-expo etc. all\n // continue to run.\n const existingTransformerPath = metroConfig.transformer?.babelTransformerPath\n if (typeof existingTransformerPath === 'string' && existingTransformerPath.length > 0) {\n process.env.RNWIND_UPSTREAM_TRANSFORMER = existingTransformerPath\n }\n const upstream = metroConfig.resolver?.resolveRequest ?? null\n metroConfig.transformer = { ...metroConfig.transformer, babelTransformerPath: transformerPath() }\n metroConfig.resolver = { ...metroConfig.resolver, resolveRequest: createRnwindResolver(upstream) }\n\n // Metro's haste-map indexes `watchFolders` at startup. Adding the\n // cache dir guarantees scheme style files + manifest get SHA1'd\n // without a \"Failed to get the SHA-1\" race when the first transform\n // writes them.\n const existingWatch = metroConfig.watchFolders ?? []\n metroConfig.watchFolders = existingWatch.includes(cacheDir) ? existingWatch : [...existingWatch, cacheDir]\n\n if (options.dtsFile !== false) {\n const dtsPath = options.dtsFile ?? path.resolve(projectRoot, 'rnwind-types.d.ts')\n const schemes = discoverSchemes(cssEntry, projectRoot)\n writeDtsFile(dtsPath, schemes)\n }\n\n // Watch the theme CSS. On edit, we rewrite scheme files AND touch\n // mtime on every transformed source file so Metro invalidates them\n // and re-transforms — the only reliable way to propagate theme\n // changes to an already-running dev server.\n watchThemeCss(cssEntry, projectRoot)\n\n return metroConfig\n}\n"],"names":["existsSync","watchFile","onThemeChange","rmSync","getRnwindState","mkdirSync","configureRnwindState","createRnwindResolver","writeDtsFile"],"mappings":";;;;;;;;AAMA;AACA,MAAM,iBAAiB,GAAG,SAAS;AAEnC;;;;AAIG;AACH,IAAI,gBAAgB,GAAkD,IAAI;AAE1E;;;;;;;;;;;;AAYG;AACH,SAAS,aAAa,CAAC,OAAe,EAAE,WAAmB,EAAA;AACzD,IAAA,IAAI,gBAAgB,EAAE,OAAO,KAAK,OAAO;QAAE;IAC3C,gBAAgB,EAAE,KAAK,EAAE;AACzB,IAAA,IAAI,CAACA,kBAAU,CAAC,OAAO,CAAC;QAAE;IAC1B,IAAI,OAAO,GAAG,KAAK;AACnB,IAAA,MAAM,OAAO,GAAGC,aAAS,CAAC,OAAO,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,EAAE,MAAK;;;AAG7D,QAAA,IAAI,OAAO;YAAE;QACb,OAAO,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AAe,EAAE,EAAE;AACjG,IAAA,WAAW,CAAC,QAAQ,GAAG,EAAE,GAAG,WAAW,CAAC,QAAQ,EAAE,cAAc,EAAEG,6BAAoB,CAAC,QAAQ,CAAC,EAAE;;;;;AAMlG,IAAA,MAAM,aAAa,GAAG,WAAW,CAAC,YAAY,IAAI,EAAE;IACpD,WAAW,CAAC,YAAY,GAAG,aAAa,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,aAAa,GAAG,CAAC,GAAG,aAAa,EAAE,QAAQ,CAAC;AAE1G,IAAA,IAAI,OAAO,CAAC,OAAO,KAAK,KAAK,EAAE;AAC7B,QAAA,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,mBAAmB,CAAC;QACjF,MAAM,OAAO,GAAG,eAAe,CAAC,QAAQ,EAAE,WAAW,CAAC;AACtD,QAAAC,gBAAY,CAAC,OAAO,EAAE,OAAO,CAAC;IAChC;;;;;AAMA,IAAA,aAAa,CAAC,QAAQ,EAAE,WAAW,CAAC;AAEpC,IAAA,OAAO,WAAW;AACpB;;;;;"}
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { type ResolveRequestFn } from './resolver';
|
|
2
|
+
/**
|
|
3
|
+
* Whether the dev asked for a cache reset — true if the (post-merge) config
|
|
4
|
+
* flag is set OR the CLI argv carries a reset flag. Wiping on a false positive
|
|
5
|
+
* is harmless (the cache just regenerates), so argv detection errs toward
|
|
6
|
+
* honoring the request.
|
|
7
|
+
* @param resetCache Metro's `config.resetCache` (usually still false at config-eval time).
|
|
8
|
+
* @param argv Process argv (injectable for tests).
|
|
9
|
+
* @returns True when the cache dir should be wiped.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isResetCacheRequested(resetCache: boolean | undefined, argv?: readonly string[]): boolean;
|
|
2
12
|
/** User-facing options for `withRnwindConfig`. */
|
|
3
13
|
export interface RnwindMetroOptions {
|
|
4
14
|
/** Path to the theme CSS (absolute or relative to `projectRoot`). Required. */
|
|
@@ -29,6 +39,8 @@ export interface RnwindMetroOptions {
|
|
|
29
39
|
export interface MetroConfigLike {
|
|
30
40
|
projectRoot?: string;
|
|
31
41
|
watchFolders?: string[];
|
|
42
|
+
/** Set by Metro when the dev runs `--reset-cache` (Expo `start -c`). Wipes rnwind's cache dir. */
|
|
43
|
+
resetCache?: boolean;
|
|
32
44
|
transformer?: {
|
|
33
45
|
babelTransformerPath?: string;
|
|
34
46
|
[key: string]: unknown;
|
|
@@ -100,7 +100,24 @@ const NON_COMPONENT_EXPORTS = new Set([
|
|
|
100
100
|
'Easing',
|
|
101
101
|
'ReduceMotion',
|
|
102
102
|
'KeyframeRegistry',
|
|
103
|
+
// gesture-handler enums (PascalCase, but value objects accessed as
|
|
104
|
+
// `PointerType.TOUCH` / `MouseButton.LEFT`) — wrapping breaks the access.
|
|
105
|
+
'PointerType',
|
|
106
|
+
'MouseButton',
|
|
107
|
+
'HoverEffect',
|
|
103
108
|
]);
|
|
109
|
+
/**
|
|
110
|
+
* react-native-reanimated's NAMED exports are hooks, worklets, animation
|
|
111
|
+
* builders (`FadeIn`, `ZoomIn`, `Keyframe`, `Layout`…), easing helpers and
|
|
112
|
+
* enums (`SensorType`…) — NOT className-styleable components. Its only
|
|
113
|
+
* styleable surface is the DEFAULT `Animated` namespace, wrapped separately
|
|
114
|
+
* via {@link NAMESPACE_DEFAULT_MODULES}. Under the `'all'` policy the
|
|
115
|
+
* PascalCase heuristic wrongly wrapped builders/enums into `wrap()`-ed
|
|
116
|
+
* functions, so `FadeIn.duration()` / `new Keyframe()` / `SensorType.X` threw
|
|
117
|
+
* "is not a function" / "is not a constructor" / read `undefined`. An empty
|
|
118
|
+
* allow-list wraps none of them while leaving the default namespace intact.
|
|
119
|
+
*/
|
|
120
|
+
const REANIMATED_NAMED_COMPONENTS = new Set();
|
|
104
121
|
/**
|
|
105
122
|
* Default module → wrap policy. react-native is allow-listed (mixed
|
|
106
123
|
* exports); the rest are component-only packages → `'all'`. Only modules
|
|
@@ -109,7 +126,7 @@ const NON_COMPONENT_EXPORTS = new Set([
|
|
|
109
126
|
*/
|
|
110
127
|
const DEFAULT_WRAP_MODULES = new Map([
|
|
111
128
|
['react-native', REACT_NATIVE_COMPONENTS],
|
|
112
|
-
['react-native-reanimated',
|
|
129
|
+
['react-native-reanimated', REANIMATED_NAMED_COMPONENTS],
|
|
113
130
|
['react-native-svg', 'all'],
|
|
114
131
|
['react-native-gesture-handler', 'all'],
|
|
115
132
|
['react-native-safe-area-context', 'all'],
|
|
@@ -165,6 +182,18 @@ function buildWrapModules(extra) {
|
|
|
165
182
|
function importedNameOf(specifier) {
|
|
166
183
|
return t__namespace.isIdentifier(specifier.imported) ? specifier.imported.name : specifier.imported.value;
|
|
167
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Whether an import kind is type-only (`import type …` / `import { type … }`,
|
|
187
|
+
* and the Flow `typeof` variants). Type-only bindings carry no runtime value —
|
|
188
|
+
* preset-typescript strips them from the bundle — so wrapping one emits a
|
|
189
|
+
* `const X = wrap(_rnwN)` referencing a binding that no longer exists at
|
|
190
|
+
* runtime → Hermes "Property '_rnwN' doesn't exist". They must never be wrapped.
|
|
191
|
+
* @param kind The `importKind` of a declaration or specifier.
|
|
192
|
+
* @returns True when the import is type-only.
|
|
193
|
+
*/
|
|
194
|
+
function isTypeOnly(kind) {
|
|
195
|
+
return kind === 'type' || kind === 'typeof';
|
|
196
|
+
}
|
|
168
197
|
/**
|
|
169
198
|
* Build `const Local = <wrapper>(alias)` and rebind the specifier's local
|
|
170
199
|
* to `alias` in place.
|
|
@@ -198,8 +227,14 @@ function wrapSpecifiers(node, policy, counter) {
|
|
|
198
227
|
const moduleName = node.source.value;
|
|
199
228
|
let next = counter;
|
|
200
229
|
let usesNamespace = false;
|
|
230
|
+
// `import type { … }` — whole declaration is type-only; nothing to wrap.
|
|
231
|
+
if (isTypeOnly(node.importKind))
|
|
232
|
+
return { decls, counter: next, usesNamespace };
|
|
201
233
|
for (const specifier of node.specifiers) {
|
|
202
234
|
if (t__namespace.isImportSpecifier(specifier)) {
|
|
235
|
+
// `import { type X }` — inline type-only specifier; skip it.
|
|
236
|
+
if (isTypeOnly(specifier.importKind))
|
|
237
|
+
continue;
|
|
203
238
|
if (!shouldWrap(policy, importedNameOf(specifier)))
|
|
204
239
|
continue;
|
|
205
240
|
decls.push(makeWrapDecl(specifier, `_rnw${next}`, WRAP_LOCAL));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wrap-imports.cjs","sources":["../../../../src/metro/wrap-imports.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport type { File } from '@babel/types'\n\n/**\n * Build-time import rewrite. For every `import { View } from\n * 'react-native'` (and the other configured modules) it aliases the\n * original export and binds a `wrap()`-ed component in its place:\n *\n * ```\n * import { View, StyleSheet } from 'react-native'\n * ⇩\n * import { View as _rnw0, StyleSheet } from 'react-native'\n * import { wrap as _rnwWrap } from 'rnwind'\n * const View = _rnwWrap(_rnw0)\n * ```\n *\n * Now `<View className=\"…\">` resolves className → style at render via the\n * wrapper — no matter how className arrived (literal, `{...rest}` spread,\n * forwarded through custom layers). Non-component exports (`StyleSheet`)\n * are left untouched.\n */\n\n/** Local binding the injected `wrap` import is aliased to. */\nconst WRAP_LOCAL = '_rnwWrap'\n/** Local binding the injected `wrapNamespace` import is aliased to. */\nconst WRAP_NS_LOCAL = '_rnwWrapNs'\n/** Module the wrapper is imported from. */\nconst RUNTIME_MODULE = 'rnwind'\n\n/**\n * Wrap-modules whose DEFAULT export is a component NAMESPACE accessed via\n * member expressions (`Animated.View`), not a single component. Their\n * default import is bound through `wrapNamespace` (a Proxy that wraps each\n * accessed component member) instead of `wrap`. Every other default import\n * is treated as a plain component.\n */\nconst NAMESPACE_DEFAULT_MODULES: ReadonlySet<string> = new Set(['react-native-reanimated'])\n\n/**\n * react-native mixes styleable components with utilities (`StyleSheet`,\n * `Platform`, …). Only these named exports are wrapped; everything else\n * passes through. Other ecosystem modules export components only and use\n * the `'all'` policy instead.\n */\nconst REACT_NATIVE_COMPONENTS: ReadonlySet<string> = new Set([\n 'View',\n 'Text',\n 'TextInput',\n 'Pressable',\n 'ScrollView',\n 'Image',\n 'ImageBackground',\n 'FlatList',\n 'SectionList',\n 'VirtualizedList',\n 'KeyboardAvoidingView',\n 'SafeAreaView',\n 'Modal',\n 'Switch',\n 'RefreshControl',\n 'ActivityIndicator',\n 'TouchableOpacity',\n 'TouchableHighlight',\n 'TouchableWithoutFeedback',\n 'TouchableNativeFeedback',\n 'Button',\n 'StatusBar',\n])\n\n/**\n * Named exports that LOOK like components (PascalCase) under an `'all'`\n * policy but aren't — React contexts, gesture-handler enums/namespaces,\n * etc. Wrapping these would turn `Gesture.Pan()` / `State.ACTIVE` /\n * `<XContext.Provider>` into a `wrap()`-ed component and break them.\n * Names ending in `Context` are excluded separately.\n */\nconst NON_COMPONENT_EXPORTS: ReadonlySet<string> = new Set([\n 'Gesture',\n 'GestureObjects',\n 'State',\n 'Directions',\n 'Extrapolation',\n 'Extrapolate',\n 'Easing',\n 'ReduceMotion',\n 'KeyframeRegistry',\n])\n\n/** Per-module policy: an explicit allow-list, or `'all'` named exports. */\nexport type WrapPolicy = 'all' | ReadonlySet<string>\n\n/**\n * Default module → wrap policy. react-native is allow-listed (mixed\n * exports); the rest are component-only packages → `'all'`. Only modules\n * the project has installed are ever hit (you can't import from a missing\n * package), so listing optional peers is free.\n */\nexport const DEFAULT_WRAP_MODULES: ReadonlyMap<string, WrapPolicy> = new Map<string, WrapPolicy>([\n ['react-native', REACT_NATIVE_COMPONENTS],\n ['react-native-reanimated', 'all'],\n ['react-native-svg', 'all'],\n ['react-native-gesture-handler', 'all'],\n ['react-native-safe-area-context', 'all'],\n ['expo-linear-gradient', 'all'],\n ['expo-image', 'all'],\n ['expo-blur', 'all'],\n ['expo-symbols', 'all'],\n ['@shopify/flash-list', 'all'],\n ['@shopify/react-native-skia', 'all'],\n ['lottie-react-native', 'all'],\n])\n\n/**\n * Whether a named import from a wrap-module should be wrapped.\n *\n * Explicit allow-lists (react-native) match by exact name. The `'all'`\n * policy wraps only component-style names — PascalCase, not a React\n * context (`*Context`), and not a known non-component export. This is\n * what stops `useSafeAreaInsets` (a hook) from being wrapped into a\n * component and crashing when called.\n * @param policy The module's wrap policy.\n * @param importedName The exported name being imported.\n * @returns True when the name is a component to wrap.\n */\nfunction shouldWrap(policy: WrapPolicy, importedName: string): boolean {\n if (policy !== 'all') return policy.has(importedName)\n if (!/^[A-Z]/.test(importedName)) return false\n if (importedName.endsWith('Context')) return false\n return !NON_COMPONENT_EXPORTS.has(importedName)\n}\n\n/**\n * Merge user-supplied wrap modules onto the defaults — a bare module name\n * adopts the `'all'` policy.\n * @param extra User module specifiers (or undefined).\n * @returns Effective module → policy map.\n */\nexport function buildWrapModules(extra?: readonly string[]): ReadonlyMap<string, WrapPolicy> {\n if (!extra || extra.length === 0) return DEFAULT_WRAP_MODULES\n const merged = new Map<string, WrapPolicy>(DEFAULT_WRAP_MODULES)\n for (const moduleName of extra) if (!merged.has(moduleName)) merged.set(moduleName, 'all')\n return merged\n}\n\n/**\n * The `imported` name of an import specifier (`import { a as b }` → `'a'`).\n * @param specifier Named import specifier.\n * @returns The exported name.\n */\nfunction importedNameOf(specifier: t.ImportSpecifier): string {\n return t.isIdentifier(specifier.imported) ? specifier.imported.name : specifier.imported.value\n}\n\n/**\n * Build `const Local = <wrapper>(alias)` and rebind the specifier's local\n * to `alias` in place.\n * @param specifier The import specifier to rebind.\n * @param alias The `_rnwN` alias to bind the original import to.\n * @param wrapper The runtime helper local (`_rnwWrap` / `_rnwWrapNs`).\n * @returns The wrap declaration.\n */\nfunction makeWrapDecl(\n specifier: t.ImportSpecifier | t.ImportDefaultSpecifier | t.ImportNamespaceSpecifier,\n alias: string,\n wrapper: string,\n): t.VariableDeclaration {\n const { name: localName } = specifier.local\n specifier.local = t.identifier(alias)\n return t.variableDeclaration('const', [\n t.variableDeclarator(t.identifier(localName), t.callExpression(t.identifier(wrapper), [t.identifier(alias)])),\n ])\n}\n\n/**\n * Rewrite one import declaration's wrappable specifiers, aliasing each to\n * `_rnw<N>` in place:\n * - named (`{ View }`) → `const View = wrap(_rnwN)` (per policy),\n * - namespace (`* as Animated`) → `const Animated = wrapNamespace(_rnwN)`,\n * - default → `wrapNamespace` for {@link NAMESPACE_DEFAULT_MODULES}\n * (reanimated's `Animated`), else `wrap` (a plain default component).\n * @param node Import declaration to inspect.\n * @param policy The module's wrap policy.\n * @param counter Next alias index (caller-threaded for uniqueness).\n * @returns New wrap declarations, advanced counter, and whether any\n * binding used `wrapNamespace`.\n */\nfunction wrapSpecifiers(\n node: t.ImportDeclaration,\n policy: WrapPolicy,\n counter: number,\n): { decls: t.VariableDeclaration[]; counter: number; usesNamespace: boolean } {\n const decls: t.VariableDeclaration[] = []\n const moduleName = node.source.value\n let next = counter\n let usesNamespace = false\n for (const specifier of node.specifiers) {\n if (t.isImportSpecifier(specifier)) {\n if (!shouldWrap(policy, importedNameOf(specifier))) continue\n decls.push(makeWrapDecl(specifier, `_rnw${next}`, WRAP_LOCAL))\n next += 1\n continue\n }\n const isNamespace = t.isImportNamespaceSpecifier(specifier) || NAMESPACE_DEFAULT_MODULES.has(moduleName)\n const wrapper = isNamespace ? WRAP_NS_LOCAL : WRAP_LOCAL\n decls.push(makeWrapDecl(specifier, `_rnw${next}`, wrapper))\n next += 1\n if (isNamespace) usesNamespace = true\n }\n return { decls, counter: next, usesNamespace }\n}\n\n/**\n * Insert the `wrap` import at the top and the `const X = wrap(_rnwN)`\n * declarations AFTER every import. The consts reference the aliased\n * binding `_rnwN`, and in Metro's real transform (CommonJS interop + the\n * reanimated worklets plugin) a const placed above its source import\n * evaluates before the binding initialises → `ReferenceError: _rnw0\n * doesn't exist`. ESM-only hoisting would mask this; the bundle does not.\n * @param ast Parsed Babel file (mutated).\n * @param wrapDecls The wrap declarations to place.\n * @param usesNamespace Whether any binding used `wrapNamespace`.\n */\nfunction placeWrapDecls(ast: File, wrapDecls: readonly t.VariableDeclaration[], usesNamespace: boolean): void {\n const specifiers = [t.importSpecifier(t.identifier(WRAP_LOCAL), t.identifier('wrap'))]\n if (usesNamespace) specifiers.push(t.importSpecifier(t.identifier(WRAP_NS_LOCAL), t.identifier('wrapNamespace')))\n ast.program.body.unshift(t.importDeclaration(specifiers, t.stringLiteral(RUNTIME_MODULE)))\n let afterImports = 0\n for (let index = 0; index < ast.program.body.length; index += 1) {\n if (t.isImportDeclaration(ast.program.body[index])) afterImports = index + 1\n }\n ast.program.body.splice(afterImports, 0, ...wrapDecls)\n}\n\n/**\n * Rewrite component imports from the configured wrap-modules into\n * `wrap()`-ed bindings, in place. Injects the `wrap` import once when any\n * binding was rewritten.\n * @param ast Parsed Babel file (mutated).\n * @param modules Effective module → policy map.\n * @returns True when at least one import was wrapped.\n */\nexport function rewriteWrapImports(ast: File, modules: ReadonlyMap<string, WrapPolicy>): boolean {\n const wrapDecls: t.VariableDeclaration[] = []\n let counter = 0\n let usesNamespace = false\n\n for (const node of ast.program.body) {\n if (!t.isImportDeclaration(node)) continue\n const policy = modules.get(node.source.value)\n if (!policy) continue\n const { decls, counter: nextCounter, usesNamespace: ns } = wrapSpecifiers(node, policy, counter)\n counter = nextCounter\n usesNamespace = usesNamespace || ns\n wrapDecls.push(...decls)\n }\n\n if (wrapDecls.length === 0) return false\n placeWrapDecls(ast, wrapDecls, usesNamespace)\n return true\n}\n"],"names":["t"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAGA;;;;;;;;;;;;;;;;;AAiBG;AAEH;AACA,MAAM,UAAU,GAAG,UAAU;AAC7B;AACA,MAAM,aAAa,GAAG,YAAY;AAClC;AACA,MAAM,cAAc,GAAG,QAAQ;AAE/B;;;;;;AAMG;AACH,MAAM,yBAAyB,GAAwB,IAAI,GAAG,CAAC,CAAC,yBAAyB,CAAC,CAAC;AAE3F;;;;;AAKG;AACH,MAAM,uBAAuB,GAAwB,IAAI,GAAG,CAAC;IAC3D,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,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,EAAE,EAAE,GAAG,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC;QAChG,OAAO,GAAG,WAAW;AACrB,QAAA,aAAa,GAAG,aAAa,IAAI,EAAE;AACnC,QAAA,SAAS,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;IAC1B;AAEA,IAAA,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC;AAAE,QAAA,OAAO,KAAK;AACxC,IAAA,cAAc,CAAC,GAAG,EAAE,SAAS,EAAE,aAAa,CAAC;AAC7C,IAAA,OAAO,IAAI;AACb;;;;;;"}
|
|
1
|
+
{"version":3,"file":"wrap-imports.cjs","sources":["../../../../src/metro/wrap-imports.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport type { File } from '@babel/types'\n\n/**\n * Build-time import rewrite. For every `import { View } from\n * 'react-native'` (and the other configured modules) it aliases the\n * original export and binds a `wrap()`-ed component in its place:\n *\n * ```\n * import { View, StyleSheet } from 'react-native'\n * ⇩\n * import { View as _rnw0, StyleSheet } from 'react-native'\n * import { wrap as _rnwWrap } from 'rnwind'\n * const View = _rnwWrap(_rnw0)\n * ```\n *\n * Now `<View className=\"…\">` resolves className → style at render via the\n * wrapper — no matter how className arrived (literal, `{...rest}` spread,\n * forwarded through custom layers). Non-component exports (`StyleSheet`)\n * are left untouched.\n */\n\n/** Local binding the injected `wrap` import is aliased to. */\nconst WRAP_LOCAL = '_rnwWrap'\n/** Local binding the injected `wrapNamespace` import is aliased to. */\nconst WRAP_NS_LOCAL = '_rnwWrapNs'\n/** Module the wrapper is imported from. */\nconst RUNTIME_MODULE = 'rnwind'\n\n/**\n * Wrap-modules whose DEFAULT export is a component NAMESPACE accessed via\n * member expressions (`Animated.View`), not a single component. Their\n * default import is bound through `wrapNamespace` (a Proxy that wraps each\n * accessed component member) instead of `wrap`. Every other default import\n * is treated as a plain component.\n */\nconst NAMESPACE_DEFAULT_MODULES: ReadonlySet<string> = new Set(['react-native-reanimated'])\n\n/**\n * react-native mixes styleable components with utilities (`StyleSheet`,\n * `Platform`, …). Only these named exports are wrapped; everything else\n * passes through. Other ecosystem modules export components only and use\n * the `'all'` policy instead.\n */\nconst REACT_NATIVE_COMPONENTS: ReadonlySet<string> = new Set([\n 'View',\n 'Text',\n 'TextInput',\n 'Pressable',\n 'ScrollView',\n 'Image',\n 'ImageBackground',\n 'FlatList',\n 'SectionList',\n 'VirtualizedList',\n 'KeyboardAvoidingView',\n 'SafeAreaView',\n 'Modal',\n 'Switch',\n 'RefreshControl',\n 'ActivityIndicator',\n 'TouchableOpacity',\n 'TouchableHighlight',\n 'TouchableWithoutFeedback',\n 'TouchableNativeFeedback',\n 'Button',\n 'StatusBar',\n])\n\n/**\n * Named exports that LOOK like components (PascalCase) under an `'all'`\n * policy but aren't — React contexts, gesture-handler enums/namespaces,\n * etc. Wrapping these would turn `Gesture.Pan()` / `State.ACTIVE` /\n * `<XContext.Provider>` into a `wrap()`-ed component and break them.\n * Names ending in `Context` are excluded separately.\n */\nconst NON_COMPONENT_EXPORTS: ReadonlySet<string> = new Set([\n 'Gesture',\n 'GestureObjects',\n 'State',\n 'Directions',\n 'Extrapolation',\n 'Extrapolate',\n 'Easing',\n 'ReduceMotion',\n 'KeyframeRegistry',\n // gesture-handler enums (PascalCase, but value objects accessed as\n // `PointerType.TOUCH` / `MouseButton.LEFT`) — wrapping breaks the access.\n 'PointerType',\n 'MouseButton',\n 'HoverEffect',\n])\n\n/**\n * react-native-reanimated's NAMED exports are hooks, worklets, animation\n * builders (`FadeIn`, `ZoomIn`, `Keyframe`, `Layout`…), easing helpers and\n * enums (`SensorType`…) — NOT className-styleable components. Its only\n * styleable surface is the DEFAULT `Animated` namespace, wrapped separately\n * via {@link NAMESPACE_DEFAULT_MODULES}. Under the `'all'` policy the\n * PascalCase heuristic wrongly wrapped builders/enums into `wrap()`-ed\n * functions, so `FadeIn.duration()` / `new Keyframe()` / `SensorType.X` threw\n * \"is not a function\" / \"is not a constructor\" / read `undefined`. An empty\n * allow-list wraps none of them while leaving the default namespace intact.\n */\nconst REANIMATED_NAMED_COMPONENTS: ReadonlySet<string> = new Set()\n\n/** Per-module policy: an explicit allow-list, or `'all'` named exports. */\nexport type WrapPolicy = 'all' | ReadonlySet<string>\n\n/**\n * Default module → wrap policy. react-native is allow-listed (mixed\n * exports); the rest are component-only packages → `'all'`. Only modules\n * the project has installed are ever hit (you can't import from a missing\n * package), so listing optional peers is free.\n */\nexport const DEFAULT_WRAP_MODULES: ReadonlyMap<string, WrapPolicy> = new Map<string, WrapPolicy>([\n ['react-native', REACT_NATIVE_COMPONENTS],\n ['react-native-reanimated', REANIMATED_NAMED_COMPONENTS],\n ['react-native-svg', 'all'],\n ['react-native-gesture-handler', 'all'],\n ['react-native-safe-area-context', 'all'],\n ['expo-linear-gradient', 'all'],\n ['expo-image', 'all'],\n ['expo-blur', 'all'],\n ['expo-symbols', 'all'],\n ['@shopify/flash-list', 'all'],\n ['@shopify/react-native-skia', 'all'],\n ['lottie-react-native', 'all'],\n])\n\n/**\n * Whether a named import from a wrap-module should be wrapped.\n *\n * Explicit allow-lists (react-native) match by exact name. The `'all'`\n * policy wraps only component-style names — PascalCase, not a React\n * context (`*Context`), and not a known non-component export. This is\n * what stops `useSafeAreaInsets` (a hook) from being wrapped into a\n * component and crashing when called.\n * @param policy The module's wrap policy.\n * @param importedName The exported name being imported.\n * @returns True when the name is a component to wrap.\n */\nfunction shouldWrap(policy: WrapPolicy, importedName: string): boolean {\n if (policy !== 'all') return policy.has(importedName)\n if (!/^[A-Z]/.test(importedName)) return false\n if (importedName.endsWith('Context')) return false\n return !NON_COMPONENT_EXPORTS.has(importedName)\n}\n\n/**\n * Merge user-supplied wrap modules onto the defaults — a bare module name\n * adopts the `'all'` policy.\n * @param extra User module specifiers (or undefined).\n * @returns Effective module → policy map.\n */\nexport function buildWrapModules(extra?: readonly string[]): ReadonlyMap<string, WrapPolicy> {\n if (!extra || extra.length === 0) return DEFAULT_WRAP_MODULES\n const merged = new Map<string, WrapPolicy>(DEFAULT_WRAP_MODULES)\n for (const moduleName of extra) if (!merged.has(moduleName)) merged.set(moduleName, 'all')\n return merged\n}\n\n/**\n * The `imported` name of an import specifier (`import { a as b }` → `'a'`).\n * @param specifier Named import specifier.\n * @returns The exported name.\n */\nfunction importedNameOf(specifier: t.ImportSpecifier): string {\n return t.isIdentifier(specifier.imported) ? specifier.imported.name : specifier.imported.value\n}\n\n/**\n * Whether an import kind is type-only (`import type …` / `import { type … }`,\n * and the Flow `typeof` variants). Type-only bindings carry no runtime value —\n * preset-typescript strips them from the bundle — so wrapping one emits a\n * `const X = wrap(_rnwN)` referencing a binding that no longer exists at\n * runtime → Hermes \"Property '_rnwN' doesn't exist\". They must never be wrapped.\n * @param kind The `importKind` of a declaration or specifier.\n * @returns True when the import is type-only.\n */\nfunction isTypeOnly(kind: t.ImportDeclaration['importKind'] | t.ImportSpecifier['importKind']): boolean {\n return kind === 'type' || kind === 'typeof'\n}\n\n/**\n * Build `const Local = <wrapper>(alias)` and rebind the specifier's local\n * to `alias` in place.\n * @param specifier The import specifier to rebind.\n * @param alias The `_rnwN` alias to bind the original import to.\n * @param wrapper The runtime helper local (`_rnwWrap` / `_rnwWrapNs`).\n * @returns The wrap declaration.\n */\nfunction makeWrapDecl(\n specifier: t.ImportSpecifier | t.ImportDefaultSpecifier | t.ImportNamespaceSpecifier,\n alias: string,\n wrapper: string,\n): t.VariableDeclaration {\n const { name: localName } = specifier.local\n specifier.local = t.identifier(alias)\n return t.variableDeclaration('const', [\n t.variableDeclarator(t.identifier(localName), t.callExpression(t.identifier(wrapper), [t.identifier(alias)])),\n ])\n}\n\n/**\n * Rewrite one import declaration's wrappable specifiers, aliasing each to\n * `_rnw<N>` in place:\n * - named (`{ View }`) → `const View = wrap(_rnwN)` (per policy),\n * - namespace (`* as Animated`) → `const Animated = wrapNamespace(_rnwN)`,\n * - default → `wrapNamespace` for {@link NAMESPACE_DEFAULT_MODULES}\n * (reanimated's `Animated`), else `wrap` (a plain default component).\n * @param node Import declaration to inspect.\n * @param policy The module's wrap policy.\n * @param counter Next alias index (caller-threaded for uniqueness).\n * @returns New wrap declarations, advanced counter, and whether any\n * binding used `wrapNamespace`.\n */\nfunction wrapSpecifiers(\n node: t.ImportDeclaration,\n policy: WrapPolicy,\n counter: number,\n): { decls: t.VariableDeclaration[]; counter: number; usesNamespace: boolean } {\n const decls: t.VariableDeclaration[] = []\n const moduleName = node.source.value\n let next = counter\n let usesNamespace = false\n // `import type { … }` — whole declaration is type-only; nothing to wrap.\n if (isTypeOnly(node.importKind)) return { decls, counter: next, usesNamespace }\n for (const specifier of node.specifiers) {\n if (t.isImportSpecifier(specifier)) {\n // `import { type X }` — inline type-only specifier; skip it.\n if (isTypeOnly(specifier.importKind)) continue\n if (!shouldWrap(policy, importedNameOf(specifier))) continue\n decls.push(makeWrapDecl(specifier, `_rnw${next}`, WRAP_LOCAL))\n next += 1\n continue\n }\n const isNamespace = t.isImportNamespaceSpecifier(specifier) || NAMESPACE_DEFAULT_MODULES.has(moduleName)\n const wrapper = isNamespace ? WRAP_NS_LOCAL : WRAP_LOCAL\n decls.push(makeWrapDecl(specifier, `_rnw${next}`, wrapper))\n next += 1\n if (isNamespace) usesNamespace = true\n }\n return { decls, counter: next, usesNamespace }\n}\n\n/**\n * Insert the `wrap` import at the top and the `const X = wrap(_rnwN)`\n * declarations AFTER every import. The consts reference the aliased\n * binding `_rnwN`, and in Metro's real transform (CommonJS interop + the\n * reanimated worklets plugin) a const placed above its source import\n * evaluates before the binding initialises → `ReferenceError: _rnw0\n * doesn't exist`. ESM-only hoisting would mask this; the bundle does not.\n * @param ast Parsed Babel file (mutated).\n * @param wrapDecls The wrap declarations to place.\n * @param usesNamespace Whether any binding used `wrapNamespace`.\n */\nfunction placeWrapDecls(ast: File, wrapDecls: readonly t.VariableDeclaration[], usesNamespace: boolean): void {\n const specifiers = [t.importSpecifier(t.identifier(WRAP_LOCAL), t.identifier('wrap'))]\n if (usesNamespace) specifiers.push(t.importSpecifier(t.identifier(WRAP_NS_LOCAL), t.identifier('wrapNamespace')))\n ast.program.body.unshift(t.importDeclaration(specifiers, t.stringLiteral(RUNTIME_MODULE)))\n let afterImports = 0\n for (let index = 0; index < ast.program.body.length; index += 1) {\n if (t.isImportDeclaration(ast.program.body[index])) afterImports = index + 1\n }\n ast.program.body.splice(afterImports, 0, ...wrapDecls)\n}\n\n/**\n * Rewrite component imports from the configured wrap-modules into\n * `wrap()`-ed bindings, in place. Injects the `wrap` import once when any\n * binding was rewritten.\n * @param ast Parsed Babel file (mutated).\n * @param modules Effective module → policy map.\n * @returns True when at least one import was wrapped.\n */\nexport function rewriteWrapImports(ast: File, modules: ReadonlyMap<string, WrapPolicy>): boolean {\n const wrapDecls: t.VariableDeclaration[] = []\n let counter = 0\n let usesNamespace = false\n\n for (const node of ast.program.body) {\n if (!t.isImportDeclaration(node)) continue\n const policy = modules.get(node.source.value)\n if (!policy) continue\n const { decls, counter: nextCounter, usesNamespace: ns } = wrapSpecifiers(node, policy, counter)\n counter = nextCounter\n usesNamespace = usesNamespace || ns\n wrapDecls.push(...decls)\n }\n\n if (wrapDecls.length === 0) return false\n placeWrapDecls(ast, wrapDecls, usesNamespace)\n return true\n}\n"],"names":["t"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAGA;;;;;;;;;;;;;;;;;AAiBG;AAEH;AACA,MAAM,UAAU,GAAG,UAAU;AAC7B;AACA,MAAM,aAAa,GAAG,YAAY;AAClC;AACA,MAAM,cAAc,GAAG,QAAQ;AAE/B;;;;;;AAMG;AACH,MAAM,yBAAyB,GAAwB,IAAI,GAAG,CAAC,CAAC,yBAAyB,CAAC,CAAC;AAE3F;;;;;AAKG;AACH,MAAM,uBAAuB,GAAwB,IAAI,GAAG,CAAC;IAC3D,MAAM;IACN,MAAM;IACN,WAAW;IACX,WAAW;IACX,YAAY;IACZ,OAAO;IACP,iBAAiB;IACjB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GAA4B,EAAE;IAC7C,IAAI,OAAO,GAAG,CAAC;IACf,IAAI,aAAa,GAAG,KAAK;IAEzB,KAAK,MAAM,IAAI,IAAI,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE;AACnC,QAAA,IAAI,CAACA,YAAC,CAAC,mBAAmB,CAAC,IAAI,CAAC;YAAE;AAClC,QAAA,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;AAC7C,QAAA,IAAI,CAAC,MAAM;YAAE;QACb,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,EAAE,EAAE,GAAG,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC;QAChG,OAAO,GAAG,WAAW;AACrB,QAAA,aAAa,GAAG,aAAa,IAAI,EAAE;AACnC,QAAA,SAAS,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;IAC1B;AAEA,IAAA,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC;AAAE,QAAA,OAAO,KAAK;AACxC,IAAA,cAAc,CAAC,GAAG,EAAE,SAAS,EAAE,aAAa,CAAC;AAC7C,IAAA,OAAO,IAAI;AACb;;;;;;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var React = require('react');
|
|
3
4
|
var rnwindProvider = require('../components/rnwind-provider.cjs');
|
|
4
5
|
var lookupCss = require('../lookup-css.cjs');
|
|
5
6
|
|
|
@@ -23,14 +24,20 @@ function useTheme() {
|
|
|
23
24
|
const { scheme, tables } = rnwindProvider.useRnwind();
|
|
24
25
|
// The build registers token tables on the manifest so `useColor` works out
|
|
25
26
|
// of the box; an explicit `tables` prop layers on top (the prop wins).
|
|
27
|
+
// `getThemeTokens()` REPLACES its map on registration, so its identity is
|
|
28
|
+
// stable between (HMR) registers — a sound memo dep. Memoizing keeps the
|
|
29
|
+
// merged table a STABLE reference across renders, so every `useColor` /
|
|
30
|
+
// `useToken` / `useSize` call avoids re-allocating 2–3 objects per render.
|
|
26
31
|
const registered = lookupCss.getThemeTokens();
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
return React.useMemo(() => {
|
|
33
|
+
const base = { ...registered[BASE_SCHEME], ...tables[BASE_SCHEME] };
|
|
34
|
+
const schemeTable = { ...registered[scheme], ...tables[scheme] };
|
|
35
|
+
// Base tokens apply everywhere (CSS `:root` cascade); the active scheme's
|
|
36
|
+
// own entries override on overlap.
|
|
37
|
+
if (Object.keys(schemeTable).length === 0)
|
|
38
|
+
return base;
|
|
39
|
+
return { ...base, ...schemeTable };
|
|
40
|
+
}, [scheme, tables, registered]);
|
|
34
41
|
}
|
|
35
42
|
/**
|
|
36
43
|
* Read a raw CSS custom property's value for the active scheme. Accepts
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-scheme.cjs","sources":["../../../../../src/runtime/hooks/use-scheme.ts"],"sourcesContent":["import type { ThemeTable } from '../../core/types'\nimport { useRnwind } from '../components/rnwind-provider'\nimport { getThemeTokens } from '../lookup-css'\n\n/**\n * Synthetic scheme name applied when tokens aren't declared under any\n * `@variant` block — the \"no active variant\" fallback every theme table\n * inherits from.\n */\nconst BASE_SCHEME = 'base'\n\n/**\n * Access the resolved theme table for the active scheme.\n *\n * Tokens declared outside any `@variant` block live in the `base` table and\n * should apply everywhere — just like the CSS cascade treats `:root` as a\n * default for every ancestor-scoped override. We merge `base` under the\n * active scheme so a scheme that doesn't declare a token still sees the\n * base default, while the scheme's own entries win on overlap.\n * @returns Token table for the active scheme.\n */\nexport function useTheme(): ThemeTable {\n const { scheme, tables } = useRnwind()\n // The build registers token tables on the manifest so `useColor` works out\n // of the box; an explicit `tables` prop layers on top (the prop wins).\n const registered = getThemeTokens()\n const base = { ...registered[BASE_SCHEME], ...tables[BASE_SCHEME] }\n
|
|
1
|
+
{"version":3,"file":"use-scheme.cjs","sources":["../../../../../src/runtime/hooks/use-scheme.ts"],"sourcesContent":["import { useMemo } from 'react'\nimport type { ThemeTable } from '../../core/types'\nimport { useRnwind } from '../components/rnwind-provider'\nimport { getThemeTokens } from '../lookup-css'\n\n/**\n * Synthetic scheme name applied when tokens aren't declared under any\n * `@variant` block — the \"no active variant\" fallback every theme table\n * inherits from.\n */\nconst BASE_SCHEME = 'base'\n\n/**\n * Access the resolved theme table for the active scheme.\n *\n * Tokens declared outside any `@variant` block live in the `base` table and\n * should apply everywhere — just like the CSS cascade treats `:root` as a\n * default for every ancestor-scoped override. We merge `base` under the\n * active scheme so a scheme that doesn't declare a token still sees the\n * base default, while the scheme's own entries win on overlap.\n * @returns Token table for the active scheme.\n */\nexport function useTheme(): ThemeTable {\n const { scheme, tables } = useRnwind()\n // The build registers token tables on the manifest so `useColor` works out\n // of the box; an explicit `tables` prop layers on top (the prop wins).\n // `getThemeTokens()` REPLACES its map on registration, so its identity is\n // stable between (HMR) registers — a sound memo dep. Memoizing keeps the\n // merged table a STABLE reference across renders, so every `useColor` /\n // `useToken` / `useSize` call avoids re-allocating 2–3 objects per render.\n const registered = getThemeTokens()\n return useMemo(() => {\n const base = { ...registered[BASE_SCHEME], ...tables[BASE_SCHEME] }\n const schemeTable = { ...registered[scheme], ...tables[scheme] }\n // Base tokens apply everywhere (CSS `:root` cascade); the active scheme's\n // own entries override on overlap.\n if (Object.keys(schemeTable).length === 0) return base\n return { ...base, ...schemeTable }\n }, [scheme, tables, registered])\n}\n\n/**\n * Read a raw CSS custom property's value for the active scheme. Accepts\n * either `--foo` or the bare `foo` form for convenience.\n * @param cssVariable CSS custom property name (with or without the leading `--`).\n * @returns The resolved value, or undefined when the token is missing.\n */\nexport function useToken(cssVariable: string): string | number | undefined {\n const table = useTheme()\n const name = cssVariable.startsWith('--') ? cssVariable : `--${cssVariable}`\n return table[name]\n}\n\n/**\n * Read a color token by shorthand name — `useColor('primary')` resolves\n * `--color-primary` for the active scheme. A fully-qualified name\n * (`--color-primary`) is accepted as-is, so the call doesn't silently miss by\n * double-prefixing into `--color---color-primary`.\n * @param name Token suffix after `--color-`, or the full `--color-*` name.\n * @returns Resolved color string, or undefined when the token is missing\n * or its value isn't a string.\n */\nexport function useColor(name: string): string | undefined {\n const value = useToken(name.startsWith('--') ? name : `--color-${name}`)\n return typeof value === 'string' ? value : undefined\n}\n\n/**\n * Read a spacing token by shorthand name — `useSize('4')` resolves\n * `--spacing-4` for the active scheme. A fully-qualified `--spacing-*` name is\n * accepted as-is (no double-prefix miss).\n * @param name Token suffix after `--spacing-`, or the full `--spacing-*` name.\n * @returns Resolved spacing value, or undefined when the token is missing.\n */\nexport function useSize(name: string): number | string | undefined {\n return useToken(name.startsWith('--') ? name : `--spacing-${name}`)\n}\n"],"names":["useRnwind","getThemeTokens","useMemo"],"mappings":";;;;;;AAKA;;;;AAIG;AACH,MAAM,WAAW,GAAG,MAAM;AAE1B;;;;;;;;;AASG;SACa,QAAQ,GAAA;IACtB,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAGA,wBAAS,EAAE;;;;;;;AAOtC,IAAA,MAAM,UAAU,GAAGC,wBAAc,EAAE;IACnC,OAAOC,aAAO,CAAC,MAAK;AAClB,QAAA,MAAM,IAAI,GAAG,EAAE,GAAG,UAAU,CAAC,WAAW,CAAC,EAAE,GAAG,MAAM,CAAC,WAAW,CAAC,EAAE;AACnE,QAAA,MAAM,WAAW,GAAG,EAAE,GAAG,UAAU,CAAC,MAAM,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE;;;QAGhE,IAAI,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,KAAK,CAAC;AAAE,YAAA,OAAO,IAAI;AACtD,QAAA,OAAO,EAAE,GAAG,IAAI,EAAE,GAAG,WAAW,EAAE;IACpC,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;AAClC;AAEA;;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