(
styles: Map<string, Record<string, string>>,
{ pluginCode, config }: TransformOptions
)
| 33 | } |
| 34 | |
| 35 | export function transform( |
| 36 | styles: Map<string, Record<string, string>>, |
| 37 | { pluginCode, config }: TransformOptions |
| 38 | ): Promise<Set<string>> { |
| 39 | const { references, buckets } = collectRefs(styles) |
| 40 | if (!references.length) return Promise.resolve(new Set()) |
| 41 | |
| 42 | return runTransformVariableBatch( |
| 43 | references.map(({ code, name, value }) => ({ code, name, value })), |
| 44 | workerUnitOptions(config), |
| 45 | pluginCode |
| 46 | ).then((transformResults) => { |
| 47 | const replacements = transformResults.map((result) => { |
| 48 | const noFallback = stripFallback(result) |
| 49 | const canonicalName = canonicalizeVarName(noFallback) |
| 50 | return canonicalName ? toVarExpr(canonicalName) : noFallback |
| 51 | }) |
| 52 | const usedNames = new Set<string>() |
| 53 | // Keep original reference names to handle transforms that inline literal values. |
| 54 | references.forEach(({ name }) => usedNames.add(`--${name}`)) |
| 55 | replacements.forEach((repl) => extractVarNames(repl).forEach((n) => usedNames.add(n))) |
| 56 | |
| 57 | for (const bucket of buckets.values()) { |
| 58 | const style = styles.get(bucket.nodeId) |
| 59 | if (!style) continue |
| 60 | |
| 61 | let occurrence = 0 |
| 62 | style[bucket.property] = replaceVarFunctions(bucket.value, ({ full }) => { |
| 63 | const refIndex = bucket.matchIndices[occurrence++]! |
| 64 | return replacements[refIndex] ?? full |
| 65 | }) |
| 66 | } |
| 67 | |
| 68 | return usedNames |
| 69 | }) |
| 70 | } |
| 71 | |
| 72 | export function collectRefs(styles: Map<string, Record<string, string>>) { |
| 73 | const references: VariableReferenceInternal[] = [] |
no test coverage detected