@@ -4,8 +4,9 @@ import { existsSync } from 'node:fs';
4
4
import * as path from 'node:path' ;
5
5
6
6
import MagicString from 'magic-string' ;
7
- import { walk } from 'zimmerframe ' ;
7
+ import sharp from 'sharp ' ;
8
8
import { parse } from 'svelte-parse-markup' ;
9
+ import { walk } from 'zimmerframe' ;
9
10
10
11
// TODO: expose this in vite-imagetools rather than duplicating it
11
12
const OPTIMIZABLE = / ^ [ ^ ? ] + \. ( a v i f | h e i f | g i f | j p e g | j p g | p n g | t i f f | w e b p ) ( \? .* ) ? $ / ;
@@ -70,51 +71,51 @@ export function image(opts) {
70
71
const original_url = src_attribute . raw . trim ( ) ;
71
72
let url = original_url ;
72
73
73
- const sizes = get_attr_value ( node , 'sizes' ) ;
74
- const width = get_attr_value ( node , 'width' ) ;
75
- url += url . includes ( '?' ) ? '&' : '?' ;
76
- if ( sizes && 'raw' in sizes ) {
77
- url += 'imgSizes=' + encodeURIComponent ( sizes . raw ) + '&' ;
78
- }
79
- if ( width && 'raw' in width ) {
80
- url += 'imgWidth=' + encodeURIComponent ( width . raw ) + '&' ;
74
+ if ( OPTIMIZABLE . test ( url ) ) {
75
+ const sizes = get_attr_value ( node , 'sizes' ) ;
76
+ const width = get_attr_value ( node , 'width' ) ;
77
+ url += url . includes ( '?' ) ? '&' : '?' ;
78
+ if ( sizes && 'raw' in sizes ) {
79
+ url += 'imgSizes=' + encodeURIComponent ( sizes . raw ) + '&' ;
80
+ }
81
+ if ( width && 'raw' in width ) {
82
+ url += 'imgWidth=' + encodeURIComponent ( width . raw ) + '&' ;
83
+ }
84
+ url += 'enhanced' ;
81
85
}
82
- url += 'enhanced' ;
83
86
84
- if ( OPTIMIZABLE . test ( url ) ) {
85
- // resolves the import so that we can build the entire picture template string and don't
86
- // need any logic blocks
87
- const resolved_id = ( await opts . plugin_context . resolve ( url , filename ) ) ?. id ;
88
- if ( ! resolved_id ) {
89
- const file_path = url . substring ( 0 , url . indexOf ( '?' ) ) ;
90
- if ( existsSync ( path . resolve ( opts . vite_config . publicDir , file_path ) ) ) {
91
- throw new Error (
92
- `Could not locate ${ file_path } . Please move it to be located relative to the page in the routes directory or reference it beginning with /static/. See https://vitejs.dev/guide/assets for more details on referencing assets.`
93
- ) ;
94
- }
87
+ // resolves the import so that we can build the entire picture template string and don't
88
+ // need any logic blocks
89
+ const resolved_id = ( await opts . plugin_context . resolve ( url , filename ) ) ?. id ;
90
+ if ( ! resolved_id ) {
91
+ const query_index = url . indexOf ( '?' ) ;
92
+ const file_path = query_index >= 0 ? url . substring ( 0 , query_index ) : url ;
93
+ if ( existsSync ( path . resolve ( opts . vite_config . publicDir , file_path ) ) ) {
95
94
throw new Error (
96
- `Could not locate ${ file_path } . See https://vitejs.dev/guide/assets for more details on referencing assets.`
95
+ `Could not locate ${ file_path } . Please move it to be located relative to the page in the routes directory or reference it beginning with /static/. See https://vitejs.dev/guide/assets for more details on referencing assets.`
97
96
) ;
98
97
}
98
+ throw new Error (
99
+ `Could not locate ${ file_path } . See https://vitejs.dev/guide/assets for more details on referencing assets.`
100
+ ) ;
101
+ }
99
102
103
+ if ( OPTIMIZABLE . test ( url ) ) {
100
104
let image = images . get ( resolved_id ) ;
101
105
if ( ! image ) {
102
106
image = await process ( resolved_id , opts ) ;
103
107
images . set ( resolved_id , image ) ;
104
108
}
105
109
s . update ( node . start , node . end , img_to_picture ( content , node , image ) ) ;
106
110
} else {
107
- // e.g. <img src="./foo.svg" /> => <img src={__IMPORTED_ASSET_0__} />
108
111
const name = '__IMPORTED_ASSET_' + imports . size + '__' ;
109
- const { start, end } = src_attribute ;
110
- // update src with reference to imported asset
111
- s . update (
112
- is_quote ( content , start - 1 ) ? start - 1 : start ,
113
- is_quote ( content , end ) ? end + 1 : end ,
114
- `{${ name } }`
115
- ) ;
116
- // update `enhanced:img` to `img`
117
- s . update ( node . start + 1 , node . start + 1 + 'enhanced:img' . length , 'img' ) ;
112
+ const metadata = await sharp ( resolved_id ) . metadata ( ) ;
113
+ const new_markup = `<img ${ serialize_img_attributes ( content , node . attributes , {
114
+ src : `{${ name } }` ,
115
+ width : metadata . width || 0 ,
116
+ height : metadata . height || 0
117
+ } ) } />`;
118
+ s . update ( node . start , node . end , new_markup ) ;
118
119
imports . set ( original_url , name ) ;
119
120
}
120
121
}
@@ -175,15 +176,6 @@ export function image(opts) {
175
176
} ;
176
177
}
177
178
178
- /**
179
- * @param {string } content
180
- * @param {number } index
181
- * @returns {boolean }
182
- */
183
- function is_quote ( content , index ) {
184
- return content . charAt ( index ) === '"' || content . charAt ( index ) === "'" ;
185
- }
186
-
187
179
/**
188
180
* @param {string } resolved_id
189
181
* @param {{
0 commit comments