@@ -102,14 +102,10 @@ export function ecsstatic(options: Options = {}) {
102
102
103
103
const parsedAst = this . parse ( code ) as ESTree . Program ;
104
104
105
- const {
106
- cssImportName,
107
- scssImportName,
108
- statements : ecsstaticImportStatements ,
109
- } = findEcsstaticImports ( parsedAst ) ;
110
- if ( ecsstaticImportStatements . length === 0 ) return ;
105
+ const ecsstaticImports = findEcsstaticImports ( parsedAst ) ;
106
+ if ( ecsstaticImports . size === 0 ) return ;
111
107
112
- const importNames = [ cssImportName , scssImportName ] . filter ( Boolean ) as string [ ] ;
108
+ const importNames = [ ... ecsstaticImports . keys ( ) ] ;
113
109
114
110
const cssTemplateLiterals = findCssTaggedTemplateLiterals ( parsedAst , importNames ) ;
115
111
if ( cssTemplateLiterals . length === 0 ) return ;
@@ -119,7 +115,7 @@ export function ecsstatic(options: Options = {}) {
119
115
120
116
for ( const node of cssTemplateLiterals ) {
121
117
const { start, end, quasi, tag, _originalName } = node ;
122
- const isScss = tag . type === 'Identifier' && tag . name === scssImportName ;
118
+ const isScss = tag . type === 'Identifier' && ecsstaticImports . get ( tag . name ) ?. isScss ;
123
119
124
120
// lazy populate inlinedVars until we need it, to delay problems that come with this mess
125
121
if ( quasi . expressions . length && evaluateExpressions && ! inlinedVars ) {
@@ -151,7 +147,7 @@ export function ecsstatic(options: Options = {}) {
151
147
}
152
148
153
149
// remove ecsstatic imports, we don't need them anymore
154
- ecsstaticImportStatements . forEach ( ( { start, end } ) => magicCode . update ( start , end , '' ) ) ;
150
+ for ( const { start, end } of ecsstaticImports . values ( ) ) magicCode . remove ( start , end ) ;
155
151
156
152
return {
157
153
code : magicCode . toString ( ) ,
@@ -204,28 +200,28 @@ async function processTemplateLiteral(rawTemplate: string, { inlinedVars = '' })
204
200
205
201
/** parses ast and returns info about all css/scss ecsstatic imports */
206
202
function findEcsstaticImports ( ast : ESTree . Program ) {
207
- let cssImportName : string | undefined ;
208
- let scssImportName : string | undefined ;
209
- let statements : Array < { start : number ; end : number } > = [ ] ;
203
+ const statements = new Map < string , { isScss : boolean ; start : number ; end : number } > ( ) ;
210
204
211
205
for ( const node of ast . body . filter ( ( node ) => node . type === 'ImportDeclaration' ) ) {
212
- if ( node . type === 'ImportDeclaration' && node . source . value === '@acab/ecsstatic' ) {
206
+ if (
207
+ node . type === 'ImportDeclaration' &&
208
+ node . source . value ?. toString ( ) . startsWith ( '@acab/ecsstatic' )
209
+ ) {
213
210
const { start, end } = node ;
214
- if ( node . specifiers . some ( ( { imported } : any ) => [ 'css' , 'scss' ] . includes ( imported . name ) ) ) {
215
- statements . push ( { start, end } ) ;
216
- }
217
211
node . specifiers . forEach ( ( specifier ) => {
218
- if ( specifier . type === 'ImportSpecifier' && specifier . imported . name === 'css' ) {
219
- cssImportName = specifier . local . name ;
220
- }
221
- if ( specifier . type === 'ImportSpecifier' && specifier . imported . name === 'scss' ) {
222
- scssImportName = specifier . local . name ;
212
+ if (
213
+ specifier . type === 'ImportSpecifier' &&
214
+ [ 'css' , 'scss' ] . includes ( specifier . imported . name )
215
+ ) {
216
+ const tagName = specifier . local . name ;
217
+ const isScss = specifier . imported . name === 'scss' ;
218
+ statements . set ( tagName , { isScss, start, end } ) ;
223
219
}
224
220
} ) ;
225
221
}
226
222
}
227
223
228
- return { cssImportName , scssImportName , statements } ;
224
+ return statements ;
229
225
}
230
226
231
227
/**
0 commit comments