I’m using Laravel v9.19 and Vite v4.0.0. When I’m adding config.js
file using @vite
directive, it is always executing after all the html content is loaded. But, I’m trying to execute config.js
file before all the html content is loaded. The purpose is, I’m trying to access the configuration values immediately after body tag is loaded. Is there any way I can include config.js
file inside head tag?
vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import VitePluginEntryInject from 'vite-plugin-entry-inject';
export default defineConfig({
plugins: [
VitePluginEntryInject({
// head-prepend/head/body-prepend/body
injectTo: 'head'
}),
laravel({
input: [
'resources/scss/theme.scss',
'resources/scss/user.scss',
'resources/js/config.js',
],
refresh: true,
}),
],
});
welcome.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
@vite('resources/js/config.js')
</head>
<body>
<script>
console.log(window.config);
</script>
</body>
</html>
config.js
const config = {
mode: 'dark'
}
window.config = config;
I want to load config.js file inside head tag that I can access config values inside html marukup.
2
Answers
You can modify a
vite.config.js
adding the build section like that:And as a result in your blade file, you can do like that
Try this: vite.config.js
Add this to your app.js: