File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -135,6 +135,7 @@ document.getElementById('hero-img').src = imgUrl
135135
136136` ` ` js
137137function getImageUrl (name ) {
138+ // サブディレクトリー内のファイルは含まれないことに注意してください
138139 return new URL (` ./dir/${ name} .png` , import .meta.url).href
139140}
140141` ` `
@@ -146,6 +147,25 @@ function getImageUrl(name) {
146147const imgUrl = new URL (imagePath, import .meta.url).href
147148` ` `
148149
150+ ::: details 動作の仕組み
151+
152+ Vite は ` getImageUrl` 関数を次のように変換します:
153+
154+ ` ` ` js
155+ import __img0png from ' ./dir/img0.png'
156+ import __img1png from ' ./dir/img1.png'
157+
158+ function getImageUrl (name ) {
159+ const modules = {
160+ ' ./dir/img0.png' : __img0png,
161+ ' ./dir/img1.png' : __img1png,
162+ }
163+ return new URL (modules[` ./dir/${ name} .png` ], import .meta.url).href
164+ }
165+ ` ` `
166+
167+ :::
168+
149169::: warning SSR では動作しません
150170ブラウザーと Node.js で ` import .meta.url` のセマンティクスが異なるため、 このパターンは Vite をサーバーサイドレンダリングで使用している場合には動作しません。サーバーバンドルは事前にクライアントホストの URL を決定することもできません。
151171:::
You can’t perform that action at this time.
0 commit comments