@@ -5,7 +5,6 @@ import * as path from 'node:path';
5
5
6
6
import MagicString from 'magic-string' ;
7
7
import { walk } from 'zimmerframe' ;
8
- import { VERSION } from 'svelte/compiler' ;
9
8
import { parse } from 'svelte-parse-markup' ;
10
9
11
10
// TODO: expose this in vite-imagetools rather than duplicating it
@@ -43,13 +42,6 @@ export function image(opts) {
43
42
*/
44
43
const imports = new Map ( ) ;
45
44
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
-
53
45
/**
54
46
* @param {import('svelte/compiler').AST.RegularElement } node
55
47
* @param {AST.Text | AST.ExpressionTag } src_attribute
@@ -110,7 +102,7 @@ export function image(opts) {
110
102
image = await process ( resolved_id , opts ) ;
111
103
images . set ( resolved_id , image ) ;
112
104
}
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 ) ) ;
114
106
} else {
115
107
// e.g. <img src="./foo.svg" /> => <img src={__IMPORTED_ASSET_0__} />
116
108
const name = '__IMPORTED_ASSET_' + imports . size + '__' ;
@@ -160,27 +152,19 @@ export function image(opts) {
160
152
161
153
await Promise . all ( pending_ast_updates ) ;
162
154
163
- // add imports and consts to <script module> block
155
+ // add imports
164
156
let text = '' ;
165
157
if ( imports . size ) {
166
158
for ( const [ path , import_name ] of imports . entries ( ) ) {
167
159
text += `\timport ${ import_name } from "${ path } ";\n` ;
168
160
}
169
161
}
170
162
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 ) {
178
164
// @ts -ignore
179
- s . appendLeft ( ast . module . content . start , text ) ;
165
+ s . appendLeft ( ast . instance . content . start , text ) ;
180
166
} else {
181
- s . prepend (
182
- `<script ${ VERSION . startsWith ( '4' ) ? 'context="module"' : 'module' } >${ text } </script>\n`
183
- ) ;
167
+ s . prepend ( `<script>${ text } </script>\n` ) ;
184
168
}
185
169
186
170
return {
@@ -318,12 +302,11 @@ function stringToNumber(param) {
318
302
}
319
303
320
304
/**
321
- * @param {Map<string,string> } consts
322
305
* @param {string } content
323
306
* @param {import('svelte/compiler').AST.RegularElement } node
324
307
* @param {import('vite-imagetools').Picture } image
325
308
*/
326
- function img_to_picture ( consts , content , node , image ) {
309
+ function img_to_picture ( content , node , image ) {
327
310
/** @type {import('../types/internal.ts').Attribute[] } attributes */
328
311
const attributes = node . attributes ;
329
312
const index = attributes . findIndex (
@@ -338,11 +321,11 @@ function img_to_picture(consts, content, node, image) {
338
321
let res = '<picture>' ;
339
322
340
323
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 } " />` ;
342
325
}
343
326
344
327
res += `<img ${ serialize_img_attributes ( content , attributes , {
345
- src : to_value ( consts , image . img . src ) ,
328
+ src : to_value ( image . img . src ) ,
346
329
width : image . img . w ,
347
330
height : image . img . h
348
331
} ) } />`;
@@ -351,20 +334,11 @@ function img_to_picture(consts, content, node, image) {
351
334
}
352
335
353
336
/**
354
- * @param {Map<string, string> } consts
355
337
* @param {string } src
356
338
*/
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 } "` ;
368
342
}
369
343
370
344
/**
0 commit comments