11import type { CodegenConfig } from '@/utils/codegen'
22
3- import { runTransformVariableBatch } from '@/mcp/transform-variables/requester'
4- import { workerUnitOptions } from '@/utils/codegen'
53import {
6- canonicalizeVarName ,
74 expandShorthands ,
8- extractVarNames ,
95 formatHexAlpha ,
10- normalizeCssVarName ,
11- preprocessCssValue ,
12- replaceVarFunctions ,
13- stripFallback ,
146 normalizeStyleValues ,
15- parseBackgroundShorthand ,
16- toVarExpr
7+ parseBackgroundShorthand
178} from '@/utils/css'
189import { cssToClassNames } from '@/utils/tailwind'
1910
@@ -338,63 +329,6 @@ function hasAtomicPadding(s: Record<string, string>) {
338329 return ! ! ( s [ 'padding-top' ] || s [ 'padding-right' ] || s [ 'padding-bottom' ] || s [ 'padding-left' ] )
339330}
340331
341- type VariableReferenceInternal = {
342- nodeId : string
343- property : string
344- code : string
345- name : string
346- value ?: string
347- }
348-
349- type PropertyBucket = {
350- nodeId : string
351- property : string
352- value : string
353- matchIndices : number [ ]
354- }
355-
356- type ApplyVariableOptions = {
357- pluginCode ?: string
358- config : CodegenConfig
359- }
360-
361- export async function applyVariableTransforms (
362- styles : Map < string , Record < string , string > > ,
363- { pluginCode, config } : ApplyVariableOptions
364- ) : Promise < Set < string > > {
365- const { references, buckets } = collectVariableReferences ( styles )
366- if ( ! references . length ) return new Set ( )
367-
368- const transformResults = await runTransformVariableBatch (
369- references . map ( ( { code, name, value } ) => ( { code, name, value } ) ) ,
370- workerUnitOptions ( config ) ,
371- pluginCode
372- )
373-
374- const replacements = transformResults . map ( ( result ) => {
375- const noFallback = stripFallback ( result )
376- const canonicalName = canonicalizeVarName ( noFallback )
377- return canonicalName ? toVarExpr ( canonicalName ) : noFallback
378- } )
379- const usedNames = new Set < string > ( )
380- // Keep original reference names to handle transforms that inline literal values.
381- references . forEach ( ( { name } ) => usedNames . add ( `--${ name } ` ) )
382- replacements . forEach ( ( repl ) => extractVarNames ( repl ) . forEach ( ( n ) => usedNames . add ( n ) ) )
383-
384- for ( const bucket of buckets . values ( ) ) {
385- const style = styles . get ( bucket . nodeId )
386- if ( ! style ) continue
387-
388- let occurrence = 0
389- style [ bucket . property ] = replaceVarFunctions ( bucket . value , ( { full } ) => {
390- const refIndex = bucket . matchIndices [ occurrence ++ ]
391- return refIndex != null ? ( replacements [ refIndex ] ?? full ) : full
392- } )
393- }
394-
395- return usedNames
396- }
397-
398332export function stripInertShadows ( style : Record < string , string > , node : SceneNode ) : void {
399333 if ( ! style [ 'box-shadow' ] ) return
400334 if ( hasRenderableFill ( node ) ) return
@@ -421,51 +355,6 @@ function isFillRenderable(fill: Paint | undefined): boolean {
421355 return true
422356}
423357
424- function collectVariableReferences ( styles : Map < string , Record < string , string > > ) {
425- const references : VariableReferenceInternal [ ] = [ ]
426- const buckets = new Map < string , PropertyBucket > ( )
427-
428- for ( const [ nodeId , style ] of styles . entries ( ) ) {
429- for ( const [ property , value ] of Object . entries ( style ) ) {
430- let hasMatch = false
431- const indices : number [ ] = [ ]
432-
433- const normalized = preprocessCssValue ( value )
434- if ( normalized !== value ) {
435- style [ property ] = normalized
436- }
437-
438- replaceVarFunctions ( normalized , ( { full, name, fallback } ) => {
439- const trimmed = name . trim ( )
440- if ( ! trimmed . startsWith ( '--' ) ) return full
441-
442- hasMatch = true
443- const refIndex =
444- references . push ( {
445- nodeId,
446- property,
447- code : full ,
448- name : normalizeCssVarName ( trimmed . slice ( 2 ) ) ,
449- value : fallback ?. trim ( )
450- } ) - 1
451- indices . push ( refIndex )
452- return full
453- } )
454-
455- if ( hasMatch ) {
456- const key = `${ nodeId } :${ property } `
457- const bucket = buckets . get ( key )
458- if ( bucket ) {
459- bucket . matchIndices . push ( ...indices )
460- } else {
461- buckets . set ( key , { nodeId, property, value : normalized , matchIndices : indices } )
462- }
463- }
464- }
465- }
466- return { references, buckets }
467- }
468-
469358export function styleToClassNames (
470359 style : Record < string , string > ,
471360 config : CodegenConfig ,
0 commit comments