Skip to content

Commit

Permalink
chore: simplify output of hoisted declarations (#12675)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Sep 13, 2024
1 parent 899eede commit fa031ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
30 changes: 12 additions & 18 deletions packages/enhanced-img/src/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,33 +137,27 @@ export function image(opts) {
}
});

// add imports
// add imports and consts to <script module> block
let text = '';
if (imports.size) {
let text = '';
for (const [path, import_name] of imports.entries()) {
text += `import ${import_name} from "${path}";`;
}
if (ast.instance) {
// @ts-ignore
s.appendLeft(ast.instance.content.start, text);
} else {
s.prepend(`<script>${text}</script>`);
text += `\timport ${import_name} from "${path}";\n`;
}
}

if (consts.size) {
let text = '';
for (const [vite_name, declaration_name] of consts.entries()) {
text += `\tconst ${declaration_name} = "${vite_name}";\n`;
}
if (ast.module) {
// @ts-ignore
s.appendLeft(ast.module.content.start, text);
} else {
s.prepend(
`<script ${VERSION.startsWith('4') ? 'context="module"' : 'module'}>\n${text}</script>\n`
);
}
}

if (ast.module) {
// @ts-ignore
s.appendLeft(ast.module.content.start, text);
} else {
s.prepend(
`<script ${VERSION.startsWith('4') ? 'context="module"' : 'module'}>${text}</script>\n`
);
}

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/enhanced-img/test/Output.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script context="module">
<script context="module">
import __IMPORTED_ASSET_0__ from "./foo.svg";
const __DECLARED_ASSET_0__ = "__VITE_ASSET__2AM7_y_a__ 1440w, __VITE_ASSET__2AM7_y_b__ 960w";
const __DECLARED_ASSET_1__ = "__VITE_ASSET__2AM7_y_c__ 1440w, __VITE_ASSET__2AM7_y_d__ 960w";
const __DECLARED_ASSET_2__ = "__VITE_ASSET__2AM7_y_e__ 1440w, __VITE_ASSET__2AM7_y_f__ 960w";
const __DECLARED_ASSET_3__ = "__VITE_ASSET__2AM7_y_g__";
</script>
<script lang="ts">
import __IMPORTED_ASSET_0__ from "./foo.svg";
import manual_image1 from './no.png';
Expand Down

0 comments on commit fa031ef

Please sign in to comment.