Skip to content

Instantly share code, notes, and snippets.

@ok200paul
Last active July 13, 2022 01:59
Show Gist options
  • Save ok200paul/afbf84aad023f4a89950b6fc7666f757 to your computer and use it in GitHub Desktop.
Save ok200paul/afbf84aad023f4a89950b6fc7666f757 to your computer and use it in GitHub Desktop.
Laravel Vite config file that works when using Laravel Valet HTTPS (locally) and a CI runner (eg GitHub Actions)
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';
import fs from 'fs';
import { resolve } from 'path';
import { homedir } from 'os';
let homeDir = homedir()
let serverConfig = {};
/**
* Evaluate if the script is running in a CI runner
*/
import ciDetect from '@npmcli/ci-detect';
const ciRunner = ciDetect()
/**
* If not running under CI, apply Laravel Valet settings
*/
if(!ciRunner){
if (homeDir) {
let host = 'mysite.com.test'; // Update to the domain valet is running under eg 'mysite.com.test'
serverConfig = {
https: {
key: fs.readFileSync(resolve(homeDir, `.config/valet/Certificates/${host}.key`)),
cert: fs.readFileSync(resolve(homeDir, `.config/valet/Certificates/${host}.crt`)),
},
hmr: {
host: host,
},
host: host,
};
}
}
else {
console.log('Building assets within '+ciRunner+' CI context.');
}
export default defineConfig({
plugins: [
laravel([
'resources/css/app.css',
'resources/js/app.js'
]),
vue({
template: {
transformAssetUrls: {
// The Vue plugin will re-write asset URLs, when referenced
// in Single File Components, to point to the Laravel web
// server. Setting this to `null` allows the Laravel plugin
// to instead re-write asset URLs to point to the Vite
// server instead.
base: null,
// The Vue plugin will parse absolute URLs and treat them
// as absolute paths to files on disk. Setting this to
// `false` will leave absolute URLs un-touched so they can
// reference assets in the public directly as expected.
includeAbsolute: false,
},
},
}),
],
server: serverConfig,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment