Skip to content

Commit

Permalink
feat: Add documentation for Craft Cloud usage with Vite ([#83](#83))
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Feb 28, 2024
1 parent 9acaedc commit 73f65ed
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,44 @@ So for example:
) }}
```
## Craft Cloud
During a [build](https://craftcms.com/knowledge-base/cloud-builds), Craft Cloud deploys static assets to a CDN, so you’ll need to configure the plugin to use the appropriate URLs:
```php
<?php
// config/vite.php
use craft\cloud\Helper as CloudHelper;
return [
'manifestPath' => CloudHelper::artifactUrl('dist/.vite/manifest.json'),
'serverPublic' => CloudHelper::artifactUrl('dist/'),
];
```
This helper function returns a CDN URL that includes your project and environment identifiers, like this:
```
https://cdn.craft.com/{project-uuid}/builds/{environment-uuid}/artifacts/dist/
```
Outside of Cloud, this behaves as though it were prepended with `@web`.
If you’d prefer to use an on-disk `manifestPath` when working locally (instead of a URL), the `CloudHelper::isCraftCloud()` function lets you switch based on the environment:
```php
<?php
// config/vite.php
use craft\cloud\Helper as CloudHelper;
return [
'manifestPath' => CloudHelper::isCraftCloud() ? CloudHelper::artifactUrl('dist/.vite/manifest.json') : '@webroot/dist/.vite/manifest.json',
'serverPublic' => CloudHelper::artifactUrl('dist/'),
];
```
Additionally, your Vite config should have [public `base`](https://vitejs.dev/guide/build.html#public-base-path) set to use the same CDN URL. In Craft Cloud’s build pipeline, this is exposed as an [`CRAFT_CLOUD_ARTIFACT_BASE_URL` environment variable](https://craftcms.com/knowledge-base/cloud-builds#build-command):
```javascript
// vite.config.js
export default ({ command }) => ({
base: command === 'serve' ? '' : `${process.env.CRAFT_CLOUD_ARTIFACT_BASE_URL || ''}/dist/`,
})
```
## Vite Roadmap
Some things to do, and ideas for potential features:
Expand Down

0 comments on commit 73f65ed

Please sign in to comment.