Skip to content

Commit a520cd6

Browse files
[9.x] Document transformOnServe Vite configuration (#8514)
* Adds documentation on the transformOnServe vite configuration option * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 80a43ac commit a520cd6

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

vite.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- [Subresource Integrity (SRI)](#subresource-integrity-sri)
2828
- [Arbitrary Attributes](#arbitrary-attributes)
2929
- [Advanced Customization](#advanced-customization)
30+
- [Correcting Dev Server URLs](#correcting-dev-server-urls)
3031

3132
<a name="introduction"></a>
3233
## Introduction
@@ -792,3 +793,42 @@ export default defineConfig({
792793
},
793794
});
794795
```
796+
797+
<a name="correcting-dev-server-urls"></a>
798+
### Correcting Dev Server URLs
799+
800+
Some plugins within the Vite ecosystem assume that URLs which begin with a forward-slash will always point to the Vite dev server. However, due to the nature of the Laravel integration, this is not the case.
801+
802+
For example, the `vite-imagetools` plugin outputs URLs like the following while Vite is serving your assets:
803+
804+
```html
805+
<img src="/@imagetools/f0b2f404b13f052c604e632f2fb60381bf61a520">
806+
```
807+
808+
The `vite-imagetools` plugin is expecting that the output URL will be intercepted by Vite and the plugin may then handle all URLs that start with `/@imagetools`. If you are using plugins that are expecting this behaviour, you will need to manually correct the URLs. You can do this in your `vite.config.js` file by using the `transformOnServe` option.
809+
810+
In this particular example, we will append the dev server URL to all occurrences of `/@imagetools` within the generated code:
811+
812+
```js
813+
import { defineConfig } from 'vite';
814+
import laravel from 'laravel-vite-plugin';
815+
import { imagetools } from 'vite-imagetools';
816+
817+
export default defineConfig({
818+
plugins: [
819+
laravel({
820+
// ...
821+
transformOnServe: (code, devServerUrl) => code.replaceAll('/@imagetools', devServerUrl+'/@imagetools'),
822+
}),
823+
imagetools(),
824+
],
825+
});
826+
```
827+
828+
Now, while Vite is serving Assets, it will output URLs that point to the Vite dev server:
829+
830+
```html
831+
- <img src="/@imagetools/f0b2f404b13f052c604e632f2fb60381bf61a520"><!-- [tl! remove] -->
832+
+ <img src="http://[::1]:5173/@imagetools/f0b2f404b13f052c604e632f2fb60381bf61a520"><!-- [tl! add] -->
833+
```
834+

0 commit comments

Comments
 (0)