Skip to content

Commit eb25a62

Browse files
authored
perf: directly inline values since Svelte no longer inlines (#13035)
1 parent ec35c83 commit eb25a62

File tree

3 files changed

+19
-45
lines changed

3 files changed

+19
-45
lines changed

.changeset/wild-flies-run.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/enhanced-img': patch
3+
---
4+
5+
perf: directly inline values since Svelte no longer inlines variables into template

packages/enhanced-img/src/preprocessor.js

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as path from 'node:path';
55

66
import MagicString from 'magic-string';
77
import { walk } from 'zimmerframe';
8-
import { VERSION } from 'svelte/compiler';
98
import { parse } from 'svelte-parse-markup';
109

1110
// TODO: expose this in vite-imagetools rather than duplicating it
@@ -43,13 +42,6 @@ export function image(opts) {
4342
*/
4443
const imports = new Map();
4544

46-
/**
47-
* Vite name to declaration name
48-
* e.g. __VITE_ASSET_0__ => __DECLARED_ASSET_0__
49-
* @type {Map<string, string>}
50-
*/
51-
const consts = new Map();
52-
5345
/**
5446
* @param {import('svelte/compiler').AST.RegularElement} node
5547
* @param {AST.Text | AST.ExpressionTag} src_attribute
@@ -110,7 +102,7 @@ export function image(opts) {
110102
image = await process(resolved_id, opts);
111103
images.set(resolved_id, image);
112104
}
113-
s.update(node.start, node.end, img_to_picture(consts, content, node, image));
105+
s.update(node.start, node.end, img_to_picture(content, node, image));
114106
} else {
115107
// e.g. <img src="./foo.svg" /> => <img src={__IMPORTED_ASSET_0__} />
116108
const name = '__IMPORTED_ASSET_' + imports.size + '__';
@@ -160,27 +152,19 @@ export function image(opts) {
160152

161153
await Promise.all(pending_ast_updates);
162154

163-
// add imports and consts to <script module> block
155+
// add imports
164156
let text = '';
165157
if (imports.size) {
166158
for (const [path, import_name] of imports.entries()) {
167159
text += `\timport ${import_name} from "${path}";\n`;
168160
}
169161
}
170162

171-
if (consts.size) {
172-
for (const [vite_name, declaration_name] of consts.entries()) {
173-
text += `\tconst ${declaration_name} = "${vite_name}";\n`;
174-
}
175-
}
176-
177-
if (ast.module) {
163+
if (ast.instance) {
178164
// @ts-ignore
179-
s.appendLeft(ast.module.content.start, text);
165+
s.appendLeft(ast.instance.content.start, text);
180166
} else {
181-
s.prepend(
182-
`<script ${VERSION.startsWith('4') ? 'context="module"' : 'module'}>${text}</script>\n`
183-
);
167+
s.prepend(`<script>${text}</script>\n`);
184168
}
185169

186170
return {
@@ -318,12 +302,11 @@ function stringToNumber(param) {
318302
}
319303

320304
/**
321-
* @param {Map<string,string>} consts
322305
* @param {string} content
323306
* @param {import('svelte/compiler').AST.RegularElement} node
324307
* @param {import('vite-imagetools').Picture} image
325308
*/
326-
function img_to_picture(consts, content, node, image) {
309+
function img_to_picture(content, node, image) {
327310
/** @type {import('../types/internal.ts').Attribute[]} attributes */
328311
const attributes = node.attributes;
329312
const index = attributes.findIndex(
@@ -338,11 +321,11 @@ function img_to_picture(consts, content, node, image) {
338321
let res = '<picture>';
339322

340323
for (const [format, srcset] of Object.entries(image.sources)) {
341-
res += `<source srcset=${to_value(consts, srcset)}${sizes_string} type="image/${format}" />`;
324+
res += `<source srcset=${to_value(srcset)}${sizes_string} type="image/${format}" />`;
342325
}
343326

344327
res += `<img ${serialize_img_attributes(content, attributes, {
345-
src: to_value(consts, image.img.src),
328+
src: to_value(image.img.src),
346329
width: image.img.w,
347330
height: image.img.h
348331
})} />`;
@@ -351,20 +334,11 @@ function img_to_picture(consts, content, node, image) {
351334
}
352335

353336
/**
354-
* @param {Map<string, string>} consts
355337
* @param {string} src
356338
*/
357-
function to_value(consts, src) {
358-
if (src.startsWith('__VITE_ASSET__')) {
359-
let var_name = consts.get(src);
360-
if (!var_name) {
361-
var_name = '__DECLARED_ASSET_' + consts.size + '__';
362-
consts.set(src, var_name);
363-
}
364-
return `{${var_name}}`;
365-
}
366-
367-
return `"${src}"`;
339+
function to_value(src) {
340+
// __VITE_ASSET__ needs to be contained in double quotes to work with Vite asset plugin
341+
return src.startsWith('__VITE_ASSET__') ? `{"${src}"}` : `"${src}"`;
368342
}
369343

370344
/**

packages/enhanced-img/test/Output.svelte

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
<script module>
1+
<script lang="ts">
22
import __IMPORTED_ASSET_0__ from "./foo.svg";
3-
const __DECLARED_ASSET_0__ = "__VITE_ASSET__2AM7_y_a__ 1440w, __VITE_ASSET__2AM7_y_b__ 960w";
4-
const __DECLARED_ASSET_1__ = "__VITE_ASSET__2AM7_y_c__ 1440w, __VITE_ASSET__2AM7_y_d__ 960w";
5-
const __DECLARED_ASSET_2__ = "__VITE_ASSET__2AM7_y_e__ 1440w, __VITE_ASSET__2AM7_y_f__ 960w";
6-
const __DECLARED_ASSET_3__ = "__VITE_ASSET__2AM7_y_g__";
7-
</script>
8-
<script lang="ts">
3+
94
105
import manual_image1 from './no.png';
116
@@ -28,7 +23,7 @@
2823
<picture><source srcset="/1 1440w, /2 960w" type="image/avif" /><source srcset="/3 1440w, /4 960w" type="image/webp" /><source srcset="5 1440w, /6 960w" type="image/png" /><img src="/7" alt="nested test" width=1440 height=1440 /></picture>
2924
</div>
3025

31-
<picture><source srcset={__DECLARED_ASSET_0__} type="image/avif" /><source srcset={__DECLARED_ASSET_1__} type="image/webp" /><source srcset={__DECLARED_ASSET_2__} type="image/png" /><img src={__DECLARED_ASSET_3__} alt="production test" width=1440 height=1440 /></picture>
26+
<picture><source srcset={"__VITE_ASSET__2AM7_y_a__ 1440w, __VITE_ASSET__2AM7_y_b__ 960w"} type="image/avif" /><source srcset={"__VITE_ASSET__2AM7_y_c__ 1440w, __VITE_ASSET__2AM7_y_d__ 960w"} type="image/webp" /><source srcset={"__VITE_ASSET__2AM7_y_e__ 1440w, __VITE_ASSET__2AM7_y_f__ 960w"} type="image/png" /><img src={"__VITE_ASSET__2AM7_y_g__"} alt="production test" width=1440 height=1440 /></picture>
3227

3328
<picture><source srcset="/1 1440w, /2 960w" type="image/avif" /><source srcset="/3 1440w, /4 960w" type="image/webp" /><source srcset="5 1440w, /6 960w" type="image/png" /><img src="/7" width="5" height="10" alt="dimensions test" /></picture>
3429

0 commit comments

Comments
 (0)