TerrePorter's avatar

vite and vue

Hello,

I'm having problems with npm run dev, it doesn't seem to be updating my vue files. I have to run npm run build, for the page to update when i make changes to the vue files.

import laravel from 'laravel-vite-plugin';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
 

export default defineConfig({
    build: {
        outDir: '../../public/build-admin',
        emptyOutDir: true,
        manifest: "manifest.json",
    },
    plugins: [
        laravel({
            publicDirectory: '../../public',
            buildDirectory: 'build-admin',
            input: [
                __dirname + '/resources/assets/css/app.css',
                __dirname + '/resources/assets/js/app.js',
                __dirname + '/resources/assets/js/app-edit-categories.js',                
            ],
            refresh: true,
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            }
        }),
    ],
});

Do i have something wrong with my configuration?

0 likes
6 replies
LaryAI's avatar
Level 58

It looks like your Vite configuration is mostly correct, but there are a few things you can check and adjust to ensure that your development server is properly refreshing when you make changes to your Vue files.

  1. Ensure Vite is running in development mode: Make sure you are running npm run dev and not npm run build. The npm run dev command should start the Vite development server, which watches for changes and updates the browser automatically.

  2. Check the refresh option: The refresh option in the laravel-vite-plugin should be set to true, which you have done correctly. This should enable hot module replacement (HMR).

  3. Check your file paths: Ensure that the paths to your Vue files and other assets are correct. Sometimes incorrect paths can cause issues with file watching.

  4. Check for errors in the console: Sometimes, errors in your Vue components or other JavaScript files can prevent HMR from working correctly. Check the browser console and terminal for any error messages.

  5. Update Vite and plugins: Ensure you are using the latest versions of Vite and its plugins. Sometimes bugs are fixed in newer versions.

Here is a slightly adjusted version of your configuration with some additional comments:

import laravel from 'laravel-vite-plugin';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
    build: {
        outDir: '../../public/build-admin',
        emptyOutDir: true,
        manifest: "manifest.json",
    },
    plugins: [
        laravel({
            publicDirectory: '../../public',
            buildDirectory: 'build-admin',
            input: [
                __dirname + '/resources/assets/css/app.css',
                __dirname + '/resources/assets/js/app.js',
                __dirname + '/resources/assets/js/app-edit-categories.js',                
            ],
            refresh: true, // This should enable HMR
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            }
        }),
    ],
    server: {
        watch: {
            usePolling: true, // This can help in some environments where file watching is unreliable
        },
    },
});

Additional Steps:

  1. Check your package.json scripts: Ensure that your package.json has the correct scripts for running the development server:

    "scripts": {
        "dev": "vite",
        "build": "vite build"
    }
    
  2. Run the development server: Make sure you are running the development server with:

    npm run dev
    
  3. Check for Vite and plugin updates: Update your dependencies to the latest versions:

    npm update
    
  4. Clear cache: Sometimes clearing the cache can help:

    rm -rf node_modules/.vite
    

By following these steps and ensuring your configuration is correct, you should be able to get Vite to properly update your Vue files during development. If the problem persists, consider providing more details about your environment and any error messages you are seeing.

TerrePorter's avatar

the information lary provided did not fix the issue.

Does anyone else have ideas?

martinbean's avatar

@terreporter I think you’d be better off just ripping Vite out, and then starting over to be honest, with the amount of threads you’ve created. You shouldn’t be having so many issues, and you shouldn’t be changing tings like the public and build directory paths, the manifest filename, etc.

Start over. Copy the code from the docs. If that doesn’t work then stop there and diagnose that problem. You seem to just be barreling ahead, changing things, and now in a bind where you’ve deviated and changed so many things that you it’s impossible to know what the actual, original problem may have been.

TerrePorter's avatar

@martinbean I know i was jumping around a lot. But i have gotten it to work, the vue page loads. The only thing i have right now is vite not updating the vue template when i make changes when using npm run dev. I can work around that by just npm run build, but it slows down the development cycle.

martinbean's avatar

The only thing i have right now is vite not updating the vue template when i make changes when using npm run dev

@TerrePorter Yes, and I imagine it’s because you’ve changed so much that you shouldn’t have needed to (the directory paths, the manifest name, etc) that you’ve probably now “broken” whatever convention Laravel was using to detect changes and do the hot-reloading.

TerrePorter's avatar

@martinbean That may be the case, but I using vite in a sub directory and i needed to change the paths or it wouldn't find the files

Please or to participate in this conversation.