You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/working-with-image-assets.md
+53-9Lines changed: 53 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ Another way you might encounter the same problem is trying to pass the path to a
29
29
30
30
## Assets and Build Tools
31
31
32
-
You'll encounter the same problem with both Vite and Vue CLI (webpack).
32
+
You'll encounter the same problem with both Vite and the old Vue CLI (webpack).
33
33
34
34
Both tools have a convention of putting assets such as images in the folder `/src/assets`, but there's no special handling for that folder in those tools. Assets need to be imported into your JS/TS code, just like other dependencies. If they aren't imported then they'll be discarded as part of the tree-shaking process and won't be included in the final build output.
35
35
@@ -50,6 +50,8 @@ import imgSrc from '../assets/image.png'
50
50
</template>
51
51
```
52
52
53
+
If you've configured a [`base`](https://vitejs.dev/guide/build.html#public-base-path) path (Vite) or a [`publicPath`](https://cli.vuejs.org/config/#publicpath) (Vue CLI) then that will also be automatically added to the imported path string.
54
+
53
55
## Why Does a Static `src` Work?
54
56
55
57
So if images need to be imported, why does `<img src="../assets/image.png">` work just fine?
@@ -231,21 +233,63 @@ Both Vite and Vue CLI have support for a `/public` folder. This is a special fol
231
233
232
234
Using `/public` can be a good option if there are a lot of images that never change, making the hashing unnecessary. However, it does come with its own problems.
233
235
234
-
Let's assume we put our images in `/public/images`. We won't be able to reference them with static strings in the template, because those will be automatically converted to imports:
236
+
In the examples that follow, let's assume we've put our images in `/public/images`.
237
+
238
+
### `/public` with Vite
239
+
240
+
If we're using Vite, the special handling for tags like `<img src>` carries across to files in `/public`. We can use static paths, we just need to start the path with a `/`:
235
241
236
242
```vue-html
237
-
<!-- This will fail, files in /public can't be imported -->
243
+
<!-- With Vite this will work fine -->
238
244
<img src="/images/image.png">
239
245
```
240
246
241
-
To get around that, we can trick the tools by using `:src` instead of `src`:
247
+
Note that we **don't** include `/public` in the path.
248
+
249
+
The Vite plugin has special handling for files beginning with `/`. It'll first check whether the file exists in the `/public` folder, then fall back to an import if the file wasn't found. If you've configured a [`base`](https://vitejs.dev/guide/build.html#public-base-path) path, Vite will rewrite the attribute path accordingly.
250
+
251
+
If we're working with dynamic paths, or static paths on tags the plugin doesn't understand, then we have to apply the `base` path ourselves.
252
+
253
+
For example, if we have `base: '/my-app/'` in our Vite config, we can access that path using `import.meta.env.BASE_URL`:
254
+
255
+
```vue
256
+
<script setup>
257
+
// This gives us access to the `base` config option
258
+
const base = import.meta.env.BASE_URL
259
+
260
+
defineProps(['name'])
261
+
</script>
262
+
263
+
<template>
264
+
<img :src="`${base}images/${name}.png`" />
265
+
</template>
266
+
```
267
+
268
+
You can read more about the `/public` folder in [the Vite docs](https://vitejs.dev/guide/assets.html#the-public-directory).
269
+
270
+
### `/public` with Vue CLI
271
+
272
+
To use the `/public` folder with Vue CLI, we need to apply the [publicPath](https://cli.vuejs.org/config/#publicpath) ourselves, even for static paths.
273
+
274
+
For example, the following code will only work if `publicPath` is set to `/`:
242
275
243
276
```vue-html
244
-
<!-- Using :src to bypass the automatic import -->
245
-
<img :src="'/images/image.png'">
277
+
<img src="/images/image.png">
246
278
```
247
279
248
-
We also need to be careful if we're using a [base path (Vite)](https://vitejs.dev/guide/build.html#public-base-path) or [publicPath (Vue CLI / webpack)](https://cli.vuejs.org/config/#publicpath). Those won't be automatically prepended to the URL, so we need to do it ourselves.
280
+
Vue CLI won't rewrite this path at all, it'll just be left as-is.
281
+
282
+
We can access the `publicPath` using `process.env.BASE_URL`:
283
+
284
+
```vue
285
+
<script setup>
286
+
// This gives us access to the `publicPath` config option
287
+
const base = process.env.BASE_URL
288
+
</script>
289
+
290
+
<template>
291
+
<img :src="`${base}images/image.png`" />
292
+
</template>
293
+
```
249
294
250
-
* Read more about the `/public` folder in [the Vite docs](https://vitejs.dev/guide/assets.html#the-public-directory).
251
-
* Read more about the `/public` folder in [the Vue CLI docs](https://cli.vuejs.org/guide/html-and-static-assets.html#the-public-folder).
295
+
You can read more about the `/public` folder in [the Vue CLI docs](https://cli.vuejs.org/guide/html-and-static-assets.html#the-public-folder).
0 commit comments