Open History to see the last commit
1import * as NodeChildProcess from "node:child_process";
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as NodeURL from "node:url";
6import { beforeAll, describe, expect, it } from "vite-plus/test";
7
8interface CompiledStyle {
9 native: boolean;
10 minWidth: number;
11 maxWidth: number;
12 active: boolean | null;
13 focus: boolean | null;
14 disabled: boolean | null;
15 dataAttributes: Record<string, string> | null;
16}
17
18interface FixtureOutput {
19 tailwindChecks: {
20 iosBlocks: number;
21 androidBlocks: number;
22 iosUtilities: number;
23 androidUtilities: number;
24 };
25 transformerCheck: {
26 reinitPayload: boolean;
27 fingerprintArg: boolean;
28 themesArg: boolean;
29 };
30 platforms: Record<
31 string,
32 {
33 styles: Record<string, CompiledStyle[] | undefined>;
34 payloadIncludesAllCompiled: boolean;
35 payloadLeaks: string[];
36 }
37 >;
38}
39
40const FIXTURE_SOURCE = `export const Probe = () => (
41 <div className="android:shrink android:grow-0 ios:flex-1 pt-12 ios:pt-[72px] ios:font-[family-name:Menlo] android:font-mono flex-1 sm:p-6 sm:text-lg" />
42);
43`;
44
45const IOS_CLASSES = ["ios:flex-1", "ios:pt-[72px]", "ios:font-[family-name:Menlo]"];
46const ANDROID_CLASSES = ["android:shrink", "android:grow-0", "android:font-mono"];
47const SHARED_CLASSES = ["flex-1", "pt-12"];
48const RESPONSIVE_CLASSES = ["sm:p-6", "sm:text-lg"];
49
50const runFixture = (globalCss: string, probeSource: string): FixtureOutput => {
51 const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "uniwind-platform-"));
52 try {
53 // So `@import "tailwindcss"` and `@import "uniwind"` resolve like in the app.
54 NodeFS.symlinkSync(
55 NodeFS.realpathSync(new URL("../../node_modules", import.meta.url)),
56 NodePath.join(tempDir, "node_modules"),
57 "dir",
58 );
59 NodeFS.writeFileSync(NodePath.join(tempDir, "global.css"), globalCss);
60 NodeFS.writeFileSync(NodePath.join(tempDir, "Probe.tsx"), probeSource);
61
62 const fixture = NodeURL.fileURLToPath(
63 new URL("./uniwind-platform-variants.fixture.cjs", import.meta.url),
64 );
65 const stdout = NodeChildProcess.execFileSync(process.execPath, [fixture, tempDir], {
66 encoding: "utf8",
67 maxBuffer: 32 * 1024 * 1024,
68 timeout: 60_000,
69 });
70 // The fixture's compiler may log warnings to stdout before the JSON payload.
71 const start = stdout.indexOf('{"tailwindChecks"');
72 if (start === -1) {
73 throw new Error(`Fixture produced no JSON:\n${stdout}`);
74 }
75 return JSON.parse(stdout.slice(start)) as FixtureOutput;
76 } finally {
77 NodeFS.rmSync(tempDir, { recursive: true, force: true });
78 }
79};
80
81describe("uniwind platform variants compile per platform", () => {
82 let output: FixtureOutput;
83
84 beforeAll(() => {
85 output = runFixture('@import "tailwindcss";\n@import "uniwind";\n', FIXTURE_SOURCE);
86 }, 60_000);
87
88 it("compiles the fixture utilities into shared platform blocks", () => {
89 expect(output.tailwindChecks).toEqual({
90 iosBlocks: 1,
91 androidBlocks: 1,
92 iosUtilities: 3,
93 androidUtilities: 3,
94 });
95 });
96
97 it("keeps ios: utilities out of the android bundle and guards them on ios", () => {
98 for (const className of IOS_CLASSES) {
99 expect(
100 output.platforms.android?.styles[className],
101 `android has ${className}`,
102 ).toBeUndefined();
103 expect(output.platforms.ios?.styles[className], `ios lacks ${className}`).toBeDefined();
104 expect(output.platforms.ios?.styles[className]?.every((style) => style.native)).toBe(true);
105 }
106 });
107
108 it("keeps android: utilities out of the ios bundle and guards them on android", () => {
109 for (const className of ANDROID_CLASSES) {
110 expect(output.platforms.ios?.styles[className], `ios has ${className}`).toBeUndefined();
111 expect(
112 output.platforms.android?.styles[className],
113 `android lacks ${className}`,
114 ).toBeDefined();
115 expect(output.platforms.android?.styles[className]?.every((style) => style.native)).toBe(
116 true,
117 );
118 }
119 });
120
121 it("keeps shared and responsive utilities usable on both platforms", () => {
122 for (const className of [...SHARED_CLASSES, ...RESPONSIVE_CLASSES]) {
123 for (const platform of ["ios", "android"]) {
124 const styles = output.platforms[platform]?.styles[className];
125 expect(styles, `${platform} lacks ${className}`).toBeDefined();
126 expect(styles?.every((style) => !style.native)).toBe(true);
127 }
128 }
129 for (const platform of ["ios", "android"]) {
130 for (const className of RESPONSIVE_CLASSES) {
131 const style = output.platforms[platform]?.styles[className]?.at(-1);
132 expect(style?.minWidth, `${className} lost its breakpoint`).toBeGreaterThan(0);
133 }
134 }
135 });
136
137 it("serializes every compiled style into the platform payload and leaks nothing", () => {
138 for (const platform of ["ios", "android"]) {
139 expect(output.platforms[platform]?.payloadIncludesAllCompiled).toBe(true);
140 expect(output.platforms[platform]?.payloadLeaks, `${platform} payload leaks`).toEqual([]);
141 }
142 });
143});
144
145describe("uniwind keeps media rules nested inside class rules attached to the class", () => {
146 let nested: FixtureOutput;
147
148 beforeAll(() => {
149 nested = runFixture(
150 [
151 '@import "tailwindcss";',
152 '@import "uniwind";',
153 "",
154 ".container-x {",
155 " width: 100%;",
156 " @media (width >= 40rem) {",
157 " max-width: 40rem;",
158 " }",
159 "}",
160 "",
161 "@utility foo-x {",
162 " padding: 2px;",
163 " @variant ios {",
164 " padding: 3px;",
165 " }",
166 "}",
167 "",
168 ].join("\n"),
169 'export const Probe = () => <div className="container-x foo-x" />;\n',
170 );
171 }, 60_000);
172
173 it("keeps the nested breakpoint entry of a nested media rule", () => {
174 for (const platform of ["ios", "android"]) {
175 const entries = nested.platforms[platform]?.styles["container-x"];
176 expect(entries, `${platform} lacks container-x`).toBeDefined();
177 expect(entries?.some((style) => !style.native && style.minWidth === 0)).toBe(true);
178 expect(entries?.some((style) => !style.native && style.minWidth > 0)).toBe(true);
179 }
180 });
181
182 it("keeps @variant declarations inside @utility on their platform", () => {
183 const iosEntries = nested.platforms.ios?.styles["foo-x"];
184 expect(iosEntries?.some((style) => !style.native)).toBe(true);
185 expect(iosEntries?.some((style) => style.native)).toBe(true);
186
187 const androidEntries = nested.platforms.android?.styles["foo-x"];
188 expect(androidEntries?.some((style) => !style.native)).toBe(true);
189 expect(androidEntries?.some((style) => style.native)).toBe(false);
190 });
191});
192
193describe("uniwind keeps selector and transformer behavior", () => {
194 let output: FixtureOutput;
195
196 beforeAll(() => {
197 output = runFixture(
198 '@import "tailwindcss";\n@import "uniwind";\n',
199 'export const Probe = () => <div className="active:opacity-50 disabled:opacity-50 data-x:underline aria-disabled:text-red-500" />;\n',
200 );
201 }, 60_000);
202
203 it("keeps state and data variants conditioned, and rejects unsupported compounds", () => {
204 for (const platform of ["ios", "android"]) {
205 const styles = output.platforms[platform]?.styles;
206 expect(styles?.["active:opacity-50"]?.every((style) => style.active === true)).toBe(true);
207 expect(styles?.["disabled:opacity-50"]?.every((style) => style.disabled === true)).toBe(true);
208 expect(styles?.["data-x:underline"]?.every((style) => style.dataAttributes?.["data-x"])).toBe(
209 true,
210 );
211 // `[aria-disabled="true"]` is not expressible at runtime; emitting it
212 // unconditionally (the pre-#9355 behavior) styles the element always.
213 expect(styles).not.toHaveProperty("aria-disabled:text-red-500");
214 }
215 });
216
217 it("emits the global.css virtual module with a native styles fingerprint", () => {
218 expect(output.transformerCheck).toEqual({
219 reinitPayload: true,
220 fingerprintArg: true,
221 themesArg: true,
222 });
223 });
224});