Skip to content

Commit dd0673f

Browse files
committed
tidy up
mainly to reduce the diff of #4
1 parent e74f660 commit dd0673f

File tree

3 files changed

+21
-31
lines changed

3 files changed

+21
-31
lines changed

.gitignore

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,3 @@ yarn-error.log*
3030

3131
# turbo
3232
.turbo
33-
34-
# tsup package output
35-
*.cjs
36-
css.js
37-
css.d.ts
38-
index.js
39-
index.d.ts
40-
vite.js
41-
vite.d.ts

package/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.d.ts
2+
*.js
3+
*.cjs

package/vite.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,10 @@ export function ecsstatic(options: Options = {}) {
102102

103103
const parsedAst = this.parse(code) as ESTree.Program;
104104

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;
111107

112-
const importNames = [cssImportName, scssImportName].filter(Boolean) as string[];
108+
const importNames = [...ecsstaticImports.keys()];
113109

114110
const cssTemplateLiterals = findCssTaggedTemplateLiterals(parsedAst, importNames);
115111
if (cssTemplateLiterals.length === 0) return;
@@ -119,7 +115,7 @@ export function ecsstatic(options: Options = {}) {
119115

120116
for (const node of cssTemplateLiterals) {
121117
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;
123119

124120
// lazy populate inlinedVars until we need it, to delay problems that come with this mess
125121
if (quasi.expressions.length && evaluateExpressions && !inlinedVars) {
@@ -151,7 +147,7 @@ export function ecsstatic(options: Options = {}) {
151147
}
152148

153149
// 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);
155151

156152
return {
157153
code: magicCode.toString(),
@@ -204,28 +200,28 @@ async function processTemplateLiteral(rawTemplate: string, { inlinedVars = '' })
204200

205201
/** parses ast and returns info about all css/scss ecsstatic imports */
206202
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 }>();
210204

211205
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+
) {
213210
const { start, end } = node;
214-
if (node.specifiers.some(({ imported }: any) => ['css', 'scss'].includes(imported.name))) {
215-
statements.push({ start, end });
216-
}
217211
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 });
223219
}
224220
});
225221
}
226222
}
227223

228-
return { cssImportName, scssImportName, statements };
224+
return statements;
229225
}
230226

231227
/**

0 commit comments

Comments
 (0)