Skip to content

Commit ecac3a4

Browse files
sammooredanstepanov
authored andcommitted
refactor(compiler): drop the rem conversion from out-of-range dimension tokens
The second pass already converts a rem dimension token with inlineRem, so converting it to px before handing it back to lightningcss changes nothing. Removes the remMultiplier parameter threaded through foldDeclaration, sanitizeTokenList and unparseIndividualTransform. Output is identical on lightningcss 1.30.1 and 1.33.0 for 70000rem through var() and translate, with and without inlineRem.
1 parent ad42e35 commit ecac3a4

3 files changed

Lines changed: 21 additions & 50 deletions

File tree

‎src/compiler/compiler.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ export function compile(
255255
}
256256
}
257257

258-
const unparsed = unparseIndividualTransform(decl, effectiveRem);
258+
const unparsed = unparseIndividualTransform(decl);
259259
if (unparsed) {
260260
return unparsed;
261261
}
262262

263263
if (inliningPlan) {
264-
return foldDeclaration(decl, inliningPlan, locals, effectiveRem);
264+
return foldDeclaration(decl, inliningPlan, locals);
265265
}
266266

267267
return undefined;

‎src/compiler/individual-transforms.ts‎

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import type {
99
Translate,
1010
} from "lightningcss";
1111

12-
import { round } from "./declarations";
13-
1412
/**
1513
* Carries `translate`, `rotate` and `scale` across the first pass as the tokens
1614
* they were written as. lightningcss before
@@ -20,11 +18,10 @@ import { round } from "./declarations";
2018
*/
2119
export function unparseIndividualTransform(
2220
declaration: Declaration,
23-
remMultiplier: number | false = false,
2421
): Declaration | undefined {
2522
switch (declaration.property) {
2623
case "translate": {
27-
const value = translateTokens(declaration.value, remMultiplier);
24+
const value = translateTokens(declaration.value);
2825

2926
return value
3027
? {
@@ -96,23 +93,12 @@ export function needsDimensionToken(value: number): boolean {
9693
// No token holds a calc(), which React Native cannot take anyway: the compiler warns for one and ignores the declaration
9794
function lengthToken(
9895
value: Length | DimensionPercentageFor_LengthValue,
99-
remMultiplier: number | false,
10096
): TokenOrValue | undefined {
10197
switch (value.type) {
10298
case "value":
10399
case "dimension": {
104100
const length = value.value;
105101
if (needsDimensionToken(length.value)) {
106-
if (length.unit === "rem" && remMultiplier !== false) {
107-
return {
108-
type: "token",
109-
value: {
110-
type: "dimension",
111-
value: round(length.value * remMultiplier),
112-
unit: "px",
113-
},
114-
};
115-
}
116102
return {
117103
type: "token",
118104
value: {
@@ -132,17 +118,14 @@ function lengthToken(
132118
}
133119

134120
// A translate holding a calc() stays parsed, so it reaches the compiler's warning unless lightningcss folds it into a transform beside it
135-
function translateTokens(
136-
value: Translate,
137-
remMultiplier: number | false,
138-
): TokenOrValue[] | undefined {
121+
function translateTokens(value: Translate): TokenOrValue[] | undefined {
139122
if (value === "none") {
140123
return NONE_TOKENS;
141124
}
142125

143-
const x = lengthToken(value.x, remMultiplier);
144-
const y = lengthToken(value.y, remMultiplier);
145-
const z = lengthToken(value.z, remMultiplier);
126+
const x = lengthToken(value.x);
127+
const y = lengthToken(value.y);
128+
const z = lengthToken(value.z);
146129

147130
return x && y && z ? spaceSeparated([x, y, z]) : undefined;
148131
}

‎src/compiler/inline-variables.ts‎

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type {
99
} from "lightningcss";
1010

1111
import type { UniqueVarInfo } from "./compiler.types";
12-
import { round } from "./declarations";
1312
import { needsDimensionToken } from "./individual-transforms";
1413

1514
export interface VariableInliningPlan {
@@ -91,7 +90,6 @@ export function foldDeclaration(
9190
declaration: Declaration,
9291
plan: VariableInliningPlan,
9392
blockLocalNames: ReadonlySet<string> | undefined,
94-
remMultiplier: number | false,
9593
): Declaration | Declaration[] | undefined {
9694
const isFoldable = (name: string) =>
9795
plan.universalNames.has(name) || blockLocalNames?.has(name) === true;
@@ -114,7 +112,7 @@ export function foldDeclaration(
114112
property: "custom",
115113
value: {
116114
name: declaration.value.name,
117-
value: sanitizeTokenList(folded, remMultiplier),
115+
value: sanitizeTokenList(folded),
118116
},
119117
};
120118
}
@@ -133,7 +131,7 @@ export function foldDeclaration(
133131
property: "unparsed",
134132
value: {
135133
propertyId: declaration.value.propertyId,
136-
value: sanitizeTokenList(folded, remMultiplier),
134+
value: sanitizeTokenList(folded),
137135
},
138136
};
139137
}
@@ -400,17 +398,11 @@ function dimensionToken(value: number, unit: string): TokenOrValue {
400398
// Strips null Option fields (parcel-bundler/lightningcss#1065, #1081) and rewrites
401399
// out-of-range integer dimensions as raw dimension tokens (#1188) on declarations
402400
// returned to lightningcss >= 1.30.2.
403-
function sanitizeTokenList(
404-
parts: TokenOrValue[],
405-
remMultiplier: number | false,
406-
): TokenOrValue[] {
407-
return parts.map((part) => sanitizeTokenOrValue(part, remMultiplier));
401+
function sanitizeTokenList(parts: TokenOrValue[]): TokenOrValue[] {
402+
return parts.map((part) => sanitizeTokenOrValue(part));
408403
}
409404

410-
function sanitizeTokenOrValue(
411-
part: TokenOrValue,
412-
remMultiplier: number | false,
413-
): TokenOrValue {
405+
function sanitizeTokenOrValue(part: TokenOrValue): TokenOrValue {
414406
switch (part.type) {
415407
case "var": {
416408
const name =
@@ -424,7 +416,7 @@ function sanitizeTokenOrValue(
424416
type: "var",
425417
value: {
426418
name,
427-
fallback: sanitizeTokenList(part.value.fallback, remMultiplier),
419+
fallback: sanitizeTokenList(part.value.fallback),
428420
},
429421
};
430422
}
@@ -444,7 +436,7 @@ function sanitizeTokenOrValue(
444436
type: "env",
445437
value: {
446438
...base,
447-
fallback: sanitizeTokenList(part.value.fallback, remMultiplier),
439+
fallback: sanitizeTokenList(part.value.fallback),
448440
},
449441
};
450442
}
@@ -453,7 +445,7 @@ function sanitizeTokenOrValue(
453445
type: "function",
454446
value: {
455447
name: part.value.name,
456-
arguments: sanitizeTokenList(part.value.arguments, remMultiplier),
448+
arguments: sanitizeTokenList(part.value.arguments),
457449
},
458450
};
459451
case "unresolved-color":
@@ -464,30 +456,26 @@ function sanitizeTokenOrValue(
464456
type: "unresolved-color",
465457
value: {
466458
...part.value,
467-
alpha: sanitizeTokenList(part.value.alpha, remMultiplier),
459+
alpha: sanitizeTokenList(part.value.alpha),
468460
},
469461
};
470462
case "light-dark":
471463
return {
472464
type: "unresolved-color",
473465
value: {
474466
type: "light-dark",
475-
light: sanitizeTokenList(part.value.light, remMultiplier),
476-
dark: sanitizeTokenList(part.value.dark, remMultiplier),
467+
light: sanitizeTokenList(part.value.light),
468+
dark: sanitizeTokenList(part.value.dark),
477469
},
478470
};
479471
default:
480472
part.value satisfies never;
481473
return part;
482474
}
483475
case "length":
484-
if (needsDimensionToken(part.value.value)) {
485-
if (part.value.unit === "rem" && remMultiplier !== false) {
486-
return dimensionToken(round(part.value.value * remMultiplier), "px");
487-
}
488-
return dimensionToken(part.value.value, part.value.unit);
489-
}
490-
return part;
476+
return needsDimensionToken(part.value.value)
477+
? dimensionToken(part.value.value, part.value.unit)
478+
: part;
491479
case "angle":
492480
return needsDimensionToken(part.value.value)
493481
? dimensionToken(part.value.value, part.value.type)

0 commit comments

Comments
 (0)