import { walk } from 'svelte/compiler' ; import type { TemplateNode } from 'svelte/types/compiler/interfaces.d' ; import type Processor from '../processors/processor' ; param processor param classNames returns const updateMultipleClasses = ( processor , classNames ) : string => { const classes = classNames . split ( ' ' ) ; const generatedClassNames = classes . reduce ( ( accumulator , currentValue , currentIndex ) => { let value = currentValue ; const rawValue = value . trim ( ) ; if ( rawValue in processor . cssModuleList ) { value = value . replace ( rawValue , processor . cssModuleList [ rawValue ] ) ; } if ( currentIndex < classes . length - 1 ) { value += ' ' ; } return `${ accumulator }${ value }` ; } , '' ) ; return generatedClassNames ; } ; param processor param expression const parseExpression = ( processor , expression ) : void => { if ( expression . type === 'Literal' ) { const generatedClassNames = updateMultipleClasses ( processor , expression . value ) ; processor . magicContent . overwrite ( expression . start , expression . end , `'${ generatedClassNames }'` ) ; } } ; param processor export default ( processor ) : void => { const directiveLength = 'class:' . length ; const allowedAttributes = [ 'class' , ... processor . options . includeAttributes ] ; walk ( processor . ast . html , { enter ( baseNode ) { const node = baseNode as TemplateNode ; if ( node . type === 'Script' || node . type === 'Style' ) { this . skip ( ) ; } if ( [ 'Element' , 'InlineComponent' ] . includes ( node . type ) && node . attributes . length > 0 ) { node . attributes . forEach ( ( item ) => { if ( item . type === 'Attribute' && allowedAttributes . includes ( item . name ) ) { item . value . forEach ( ( classItem ) => { if ( classItem . type === 'Text' ) { const generatedClassNames = updateMultipleClasses ( processor , classItem . data ) ; processor . magicContent . overwrite ( classItem . start , classItem . start + classItem . data . length , generatedClassNames ) ; } else if ( classItem . type === 'MustacheTag' && classItem ?. expression ?. type === 'ConditionalExpression' ) { const { consequent , alternate } = classItem . expression ; parseExpression ( processor , consequent ) ; parseExpression ( processor , alternate ) ; } } ) ; } if ( item . type === 'Class' ) { const classNames = item . name . split ( '.' ) ; const name = classNames . length > 1 ? classNames [ 1 ] : classNames [ 0 ] ; if ( name in processor . cssModuleList ) { const start = item . start + directiveLength ; const end = start + item . name . length ; if ( item . expression . type === 'Identifier' && item . name === item . expression . name ) { processor . magicContent . overwrite ( start , end , `${ processor . cssModuleList [ name ] }={${ item . name }}` ) ; } else { processor . magicContent . overwrite ( start , end , processor . cssModuleList [ name ] ) ; } } } } ) ; } } } ) ; } ;