import MagicString from 'magic-string' ; import type { Ast , Style , TemplateNode } from 'svelte/types/compiler/interfaces.d' ; import { CSSModuleList , PluginOptions } from '../types' ; import { camelCase , createClassName , hasModuleAttribute , hasModuleImports } from '../lib' ; import { parseImportDeclaration , parseTemplate } from '../parsers' ; export default class Processor { public filename : string ; public options : PluginOptions ; public rawContent : string ; public cssModuleList : CSSModuleList = { } ; public importedCssModuleList : CSSModuleList = { } ; public ast : Ast ; public style : { ast : Style ; openTag : string ; closeTag : string ; } ; public magicContent : MagicString ; public styleParser : ( param : Processor ) => void ; public isParsingImports = false ; constructor ( ast , content , filename , options , parser ) { this . filename = filename ; this . options = options ; this . rawContent = content ; this . ast = ast ; this . magicContent = new MagicString ( content ) ; this . styleParser = parser . bind ( this ) ; this . style = { ast : ast . css , openTag : ast . css ? content . substring ( ast . css . start , ast . css . content . start ) : '<style module>' , closeTag : '</style>' } ; } param name returns public createModuleClassname = ( name ) : string => { const generatedClassName = createClassName ( this . filename , this . rawContent , this . ast . css . content . styles , name , this . options ) ; return generatedClassName ; } ; param name param value public addModule = ( name , value ) : void => { if ( this . isParsingImports ) { this . importedCssModuleList [ camelCase ( name ) ] = value ; } this . cssModuleList [ name ] = value ; } ; param node public parsePseudoLocalSelectors = ( node ) : void => { const pseudoLocalSelectors = node . children ?. filter ( ( item ) => item . type === 'PseudoClassSelector' && item . name === 'local' ) ?? [ ] ; if ( pseudoLocalSelectors . length > 0 ) { pseudoLocalSelectors . forEach ( ( item ) => { this . magicContent . remove ( item . start , item . start + `:local(` . length ) ; this . magicContent . remove ( item . end - 1 , item . end ) ; } ) ; } } ; returns public parse = ( ) : string => { if ( this . options . parseStyleTag && ( hasModuleAttribute ( this . ast ) || ( this . options . useAsDefaultScoping && this . ast . css ) ) ) { this . isParsingImports = false ; this . styleParser ( this ) ; } if ( this . options . parseExternalStylesheet && hasModuleImports ( this . rawContent ) ) { this . isParsingImports = true ; parseImportDeclaration ( this ) ; } if ( Object . keys ( this . cssModuleList ) . length > 0 ) { parseTemplate ( this ) ; } return this . magicContent . toString ( ) ; } ; }