Skip to content

Commit 588a9e6

Browse files
Refactor nested tagged template into conditional branches
1 parent 551bbdb commit 588a9e6

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/transformers/imports.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,22 @@ window['${DYNAMIC_IMPORT_REPLACEMENT}'] = ${DYNAMIC_IMPORT_REPLACEMENT};`;
121121
break;
122122
}
123123
}
124-
self.importedExternalsSyntax[name] = `import ${
125-
defaultSpecifier !== null
126-
? `${defaultSpecifier}${specificSpecifiers.length > 0 ? ',' : ''}`
127-
: ''
128-
}${
129-
specificSpecifiers.length > 0 ? `{${specificSpecifiers.join(',')}}` : ''
130-
} from '${name}';`;
124+
125+
const multipleSpecifiers = specificSpecifiers.length > 0;
126+
if (defaultSpecifier !== null) {
127+
if (multipleSpecifiers) {
128+
self.importedExternalsSyntax[
129+
name
130+
] = `import ${defaultSpecifier},{${specificSpecifiers.join(',')}} from '${name}';`;
131+
} else {
132+
self.importedExternalsSyntax[name] = `import ${defaultSpecifier} from '${name}';`;
133+
}
134+
} else if (multipleSpecifiers) {
135+
self.importedExternalsSyntax[name] = `import {${specificSpecifiers.join(
136+
',',
137+
)}} from '${name}';`;
138+
}
139+
131140
source.remove(...range);
132141

133142
self.importedExternalsLocalNames = self.importedExternalsLocalNames.concat(
@@ -162,9 +171,9 @@ window['${DYNAMIC_IMPORT_REPLACEMENT}'] = ${DYNAMIC_IMPORT_REPLACEMENT};`;
162171
const source = new MagicString(code);
163172
const program = parse(code);
164173

165-
Object.values(this.importedExternalsSyntax).forEach(importedExternalSyntax =>
166-
source.prepend(importedExternalSyntax),
167-
);
174+
for (const importedExternalSyntax of Object.values(this.importedExternalsSyntax)) {
175+
source.prepend(importedExternalSyntax);
176+
}
168177

169178
walk.simple(program, {
170179
Identifier(node: Identifier) {

0 commit comments

Comments
 (0)