Skip to content

Commit f879ec4

Browse files
committed
update for vite
1 parent b2da20e commit f879ec4

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

README.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,56 @@ This package provides a helper to generate paths to assets on the client-side th
66
npm install @codinglabs/laravel-asset --save
77
```
88

9-
Vue.js example:
9+
Vue 3 global mixin:
1010
``` javascript
11-
import {asset} from '@codinglabs/laravel-asset'
11+
import { asset } from '@codinglabs/laravel-asset'
12+
13+
return createApp({ render: () => h(app, props) })
14+
// ...
15+
.mixin({
16+
methods: {
17+
asset,
18+
},
19+
})
20+
```
1221

13-
window.Vue = require('vue')
22+
Vue 3 component example:
23+
``` vue
24+
<script setup>
25+
import { asset } from '@codinglabs/laravel-asset'
26+
</script>
1427
15-
Vue.mixin({
16-
methods: {
17-
asset: asset
18-
}
19-
})
28+
<template>
29+
<img :src="asset('path/to/foo.jpg')">
30+
</template>
2031
```
2132

2233
## Usage
23-
The package can be configured with a mix `.env` variable:
34+
The asset prefix can be configured with an environment variable, or by setting an asset path on the server side in the document metadata.
35+
36+
If the asset url is unknown when building the app (ie. Laravel Vapor, where the asset URL is not generated until the deploy step), the metadata option can be used.
37+
38+
To configure with an `.env` variable:
2439

2540
```
2641
ASSET_URL=https://foo.cloudfront.net
27-
MIX_ASSET_URL="${ASSET_URL}"
42+
VITE_ASSET_URL="${ASSET_URL}"
2843
```
2944

30-
Or by adding a meta tag, which is handy if the `ASSET_URL` is determined at deployment time (like with Laravel Vapor):
45+
To configure with a meta tag in Laravel Blade:
3146

3247
```html
3348
<meta name="asset-url" content="{{ config('app.asset_url') }}">
3449
```
3550

36-
37-
To inject an asset into a Vuejs component, use a [dynamic prop](https://vuejs.org/v2/guide/components-props.html#Passing-Static-or-Dynamic-Props):
51+
Once configured, simply call the `asset()` helper like you would when using the Laravel helper:
3852

3953
```html
4054
<img :src="asset('path/to/foo.jpg')">
4155
```
4256

43-
This will be prefixed with the asset URL the same as how the Laravel `asset()` helper works.
44-
45-
If you need to conditionally build against a specific environment file, take a look at [mix-env-file](https://github.com/johnwilhite/mix-env-file).
46-
4757
## About Coding Labs
48-
Coding Labs is a web app development agency based on the Gold Coast, Australia. See our open source projects [on our website](https://codinglabs.io).
58+
Coding Labs is a web app development agency based on the Gold Coast, Australia. See our open source projects [on our website](https://codinglabs.com.au).
4959

5060
## License
51-
The MIT License (MIT). Please see [License File](LICENSE) for more information.
61+
The MIT License (MIT). Please see [License File](LICENSE) for more information.

asset.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const asset = (path) => {
2-
// default to MIX_ASSET_URL
3-
let prefix = process.env.MIX_ASSET_URL
2+
// default to VITE_ASSET_URL
3+
let prefix = process.env.VITE_ASSET_URL
44

55
if (!prefix) {
66
// fallback to determining ASSET_URL from meta tag

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": false,
33
"name": "@codinglabs/laravel-asset",
4-
"version": "1.0.3",
4+
"version": "2.0.0",
55
"description": "A helper to resolve paths to assets in a Laravel app",
66
"main": "asset.js",
77
"repository": {

0 commit comments

Comments
 (0)