Laravel Vite with package development - laravel

How to use laravel vite with custom package development. I tried to add my path to vite.config.js but am getting an error.
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
// input: ['../Packages/Plugin/Customers/public/css/styles'],
refresh: true,
}),
],
});
I tried to duplicate this in my package folder but same error. Can you please assist.

I followed Kamlesh login, I was able to create vite.config.js file inside my custom package as shown below.
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel([
'resources/asset/css/settings.css',
'resources/asset/js/custom.js',
'resources/asset/js/ecstore_profile.js',
'resources/asset/js/general_settings.js',
]),
],
resolve: {
alias: {
'settings': '/resources/asset/css/settings.css',
'custom': '/resources/asset/js/custom.s',
'profile': '/resources/asset/js/ecstore_profile.js',
'general': '/resources/asset/js/general_settings.js',
},
},
});
Then I added each file to the vite.config.js file. Then run build inside the custom package and publish this to laravel public folder. all working fine.

Related

Laraveldaily CRUD example throws error: cannot find module '#vitejs/plugin-vue'

Trying to implement the CRUD example with Laravel and vue3 from
https://laraveldaily.com/post/laravel-8-vue-3-crud-composition-api
the step npm run dev throws
failed to load config from F:_MYDATA\ProjectsLaravel\nuky\vite.config.js
error when starting dev server:
Error: Cannot find module '#vitejs/plugin-vue'
The vite.config.js looks like:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '#vitejs/plugin-vue';
export default defineConfig({
plugins: [
laravel({
input: [
'resources/css/app.css',
'resources/js/app.js',
],
refresh: true,
}),
vue({
template: {
transformAssetUrls: {
base: null,
includeAbsolute: false,
},
},
})
],
});
Seemingly the error is in the config file itself. Anyway, I tried restarting VSC, TS server and executing a npm i -D #types/node
Also tried: npm i #vuejs/plugin-vue
Nothing helped.

I try to import jquery and swiper js from node_modules

Here is my vite.config.js
`
import {
defineConfig
} from 'vite';
import laravel from 'laravel-vite-plugin';
import path from 'path';
export default defineConfig({
plugins: [
laravel([
'resources/js/app.js'
]), ],
resolve: {
alias: {
'~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
}
},
});
`
app.js code
import '../../swiper/swiper-bundle';
import '../../jquery/dist/jquery';
I try to import packages that installed vs npm but I am not able to import those packages files in the app.js file

Laravel 9 with Vite: GET requests to missing scss.map file

I'm getting extraneous GET requests to /<slug>/app.scss.map, where <slug> varies based on where in the site I am. Thankfully I'm only getting this in dev mode (not if I use npm run build to build the assets), but it is very annoying.
I have import "../sass/app.scss"; in my resources/js/app.js, so I'm not surprised that this map file should be generated. The issue is that it isn't and I'm getting a lot of extraneous GET requests with no apparent value that all fail.
What changes do I need to make to either get rid of the GET requests altogether, or produce the expected file served from the correct route (i.e. not one that is local to whatever page I'm on)?
vite.config.js
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import path from 'path'
import cdn from 'vite-plugin-cdn-import'
export default defineConfig({
build: {
rollupOptions: {
external: ["jquery"],
output: {
globals: {
jquery: ["$","window.jQuery"],
},
},
},
},
plugins: [
cdn({
// ...
}),
laravel([
'resources/css/app.css',
'resources/js/app.js',
// ...
]),
],
resolve: {
alias: {
// ...
}
},
});

Laravel + Vite. Production build redirecting to /build path in url

I'm using vite to compile assets in laravel, everything is going well on the local development. But when I build the assets for production vite build and then I open the laravel in the browser abc.com then the website is automatically redirecting to abc.com/build. I don't want this behaviour, I want to be located everything on the root domain. abc.com.
I tried different configuration, base configration in the vite.config.json but still not able to solve that.
Can you tell me how I can solve it? So the root link should not be redirected to /build.
Here is my vite.config.json.
// vite.config.js
import laravel from "laravel-vite-plugin";
import { defineConfig } from "vite";
import vue from "#vitejs/plugin-vue";
import {
ElementPlusResolver,
HeadlessUiResolver
} from "unplugin-vue-components/resolvers";
import IconsResolver from "unplugin-icons/resolver";
import Icons from "unplugin-icons/vite";
import Components from "unplugin-vue-components/vite";
import vueJsx from "#vitejs/plugin-vue-jsx";
import { resolve } from "path";
import AutoImport from "unplugin-auto-import/vite";
export default defineConfig({
plugins: [
vue(),
vueJsx(),
laravel(["src/main.ts"]),
Icons({
/* options */
}),
Components({
dts: true,
resolvers: [
IconsResolver(),
ElementPlusResolver(),
HeadlessUiResolver({
prefix: "Tw"
})
// untitled-uiUiResolver({
// prefix: "x"
// })
],
dirs: [
"./src/untitled-ui/components/**",
"./src/components/**",
"./src/layouts/**",
"./src/forms/**",
"./src/sections/**",
"./src/popper/**"
]
}),
AutoImport({
include: [
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
/\.vue$/,
/\.vue\?vue/, // .vue
/\.md$/ // .md
],
imports: [
"vue",
"vue-router"
// {
// "#/untitled-ui/utils/use-api": [
// "api",
// ["geoApi", "geo"],
// "apiGet",
// "apiPost",
// "apiPatch",
// "apiDelete"
// ]
// }
],
vueTemplate: false,
dirs: [
"./src/untitled-ui/components/**",
"./src/untitled-ui/utils/**"
],
dts: "./auto-imports.d.ts",
eslintrc: {
enabled: false, // Default `false`
filepath: "./.eslintrc-auto-import.json", // Default `./.eslintrc-auto-import.json`
globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
}
})
// laravel(["resources/css/app.css", "resources/js/app.js"])
],
resolve: {
alias: {
"#": resolve(__dirname, "src")
}
},
});
I have just removed "import.meta.env.BASE_URL" which located in createWebHistory() of vue router setting and it works correctly.
/resource/js/router/index.js: createWebHistory(import.meta.env.BASE_URL) => createWebHistory()
Check this link for Laravel Vite Docs
In blade template html head
<head>
{{
Vite::useHotFile(storage_path('vite.hot')) // Customize the "hot" file...
->useBuildDirectory('bundle') // Customize the build directory...
->useManifestFilename('assets.json') // Customize the manifest filename...
->withEntryPoints(['resources/js/app.js']) // Specify the entry points...
}}
</head>
Within the vite.config.js file
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
hotFile: 'storage/vite.hot', // Customize the "hot" file...
buildDirectory: 'bundle', // Customize the build directory...
input: ['resources/js/app.js'], // Specify the entry points...
}),
],
build: {
manifest: 'assets.json', // Customize the manifest filename...
},
});
You should change the buildDirectory value to './'
But then problem is index.php and .htaccess is removed because the public directory is cleaned.
use this code inAppServiceProvider on register method:
$this->app->bind('path.public', function () {
return base_path('public_html');
});
With this code, the manifest.json problem is solved

Default Laravel + Vite configuration throws WebSocket connection to failed:

So Laravel decided to innovate once again and fix what was not broken, so Mix is gone and now default asset bundling goes with Vite.
I'm following the absolute default in their documentation to a bunch of front-end bugs and finally only several remained:
I use Laragon with SSL.
I haven't configured anything additional and my vite.config.js looks like this:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '#vitejs/plugin-vue';
export default defineConfig({
plugins: [
laravel({
input: 'resources/js/app.js',
refresh: true,
}),
vue({
template: {
transformAssetUrls: {
base: null,
includeAbsolute: false,
},
},
}),
],
});
When I run npm run dev and visit the Laragon domain I get the following in the console:
client.ts:78 WebSocket connection to 'wss://127.0.0.1:5173/' failed.
client.ts:48 [vite] failed to connect to websocket.
your current setup:
(browser) 127.0.0.1:5173/ <--[HTTP]--> 127.0.0.1:5173/ (server)
(browser) 127.0.0.1:5173/ <--[WebSocket (failing)]--> 127.0.0.1:5173/ (server)
Check out your Vite / network configuration and https://vitejs.dev/config/server-options.html#server-hmr .
I guess I need to configure my actual domain somewhere? I tried doing that in a server object in the config, but it didn't help those errors.
PS: Now in my vue files I need to import including the .vue extension e.g. import Button from '#/Components/Button.vue' is there any way I can ommit the .vue like it was with Laravel Mix?
I haven't use laragon before, but if you have a custom domain, eg, like
http://cutom-domain.test, you need to tell vite to use the certificate like so;
In your vite.config.js, add a server key with the following configuration
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '#vitejs/plugin-vue';
import fs from 'fs';
import { homedir } from 'os';
import { resolve } from 'path';
// Ignore the protocol on the host, ie do not put "http"
const host = 'cutom-domain.test';
const viteServerConfig = host => {
let keyPath = resolve(homedir(), `.config/valet/Certificates/${host}.key`)
let certificatePath = resolve(homedir(), `.config/valet/Certificates/${host}.crt`)
if (!fs.existsSync(keyPath)) {
return {}
}
if (!fs.existsSync(certificatePath)) {
return {}
}
return {
hmr: {host},
host,
https: {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certificatePath),
},
}
}
export default defineConfig({
server: viteServerConfig(host),
plugins: [
laravel({
input: 'resources/js/app.js',
refresh: true,
}),
vue({
template: {
transformAssetUrls: {
base: null,
includeAbsolute: false,
},
},
}),
],
});
Credit to this blogpost that explains more - Making Vite and Valet play nice together
I don't know if it's still relevant, but looking in the source code of laravel-vite-plugin I found a way to solve this problem in a very simple way, without even changing the vite.config.js file.
Put these two variables in the .env file and set them with full path to .key and .crt files on your system:
VITE_DEV_SERVER_KEY='C:/laragon/etc/ssl/laragon.key'
VITE_DEV_SERVER_CERT='C:/laragon/etc/ssl/laragon.crt'
Do not change anything on vite.config.js file. Here is my (fresh install of laravel + jetstream w/ inertia and --ssr):
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '#vitejs/plugin-vue';
export default defineConfig({
plugins: [
laravel({
input: 'resources/js/app.js',
ssr: 'resources/js/ssr.js',
refresh: true,
}),
vue({
template: {
transformAssetUrls: {
base: null,
includeAbsolute: false,
},
},
}),
],
});
And that's it. Run npm run dev and Vite will "magically" start a development server with https on.
VITE v4.0.4 ready in 1248 ms
➜ Local: https://laravel.test:5173/
➜ Network: https://192.168.1.2:5173/
➜ press h to show help
LARAVEL v9.48.0 plugin v0.7.3
➜ APP_URL: https://laravel.test/
Even though the configuration present in the official documentation also works, this way is much simpler, and the host, key and cert variables are not defined in the file, but they are dynamic reflecting the dev environment.
Hope this helps someone.
Here is the source where I found this, and you can also inspect in node_modules\laravel-vite-plugin\dist\index.js of your project.
When I do npm run build instead of regular npm run dev, the problem is gone. I guess, build mechanism is different for prod, so there is no WSS related errors in console.
So, in other words, perform a production Vite build and deploy it (if you are testing on a remote project).

Resources