So before, with laravel MIX, in the webpack.mix.js
file, you can write
mix.js('resources/js/app.js', 'public/js')
.js('resources/js/header.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.sass('resources/sass/main.scss', 'public/css')
.sass('resources/sass/header.scss', 'public/css')
and it will be compiled to public/asset
separately. Like the JS files will be compiled to public/asset/js
and the sass files will be at public/asset/css
. And when you put the files in the head
of the blaze.php
in your views you simply write
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
<link rel="stylesheet" href="{{ asset('css/main.css') }}">
<link rel="stylesheet" href="{{ asset('css/header.css') }}">
<script type="text/javascript" src="{{ asset('js/app.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/header.js') }}"></script>
How do I incorporate this with the now Laravel-Vite? Because when I run npm run dev
the seperate files scss
and js
just gets compiled together in a single folder which is public/build/assets/
. They are not being separated as to whether js file goes to js folder and scss file goes to css folder.
Here is what it looks like in my vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: [
'resources/sass/app.scss',
'resources/sass/main.scss',
'resources/js/app.js',
],
refresh: true,
}),
],
});
for use scss with Vite in laravel 9
step 1:- create app.scss file in resources/sass directory
step 2:- then made changes in
vite.config.js
filestep 3:- made changes in Laravel Blade file. in my case, `resources/views/app.blade.php
Done.
now as per your question I understand that you need to save build assets as custom path then see official documentation
as per doc, we need to set
buildDirectory
key option in vite.config.js file& laravel blade file will be as like below
Note :- we need to install
npm add --save-dev sass
otherwise we will error aboutPreprocessor dependency "sass" not found. Did you install it?