Skip to content

Plumbing craft-vite for a 'public' webfolder? #102

@MattAppleton

Description

@MattAppleton

Question

I am new to Vite, and have been following this great video introduction on craftquest . The course setup is working fine, its lovely to work with but now I want to try setting up Vite on a production job. I use Serverpilot which uses a 'public' root folder.

Im having trouble pursuing the logic and Vite setup to work in my 'public' setup at build time — any help would be very much appreciated!

Thus far I run into issues setting my Vite outDir to 'public/dist/' — 'dev' scripts work fine

But a 'build' script gives me a twig file with this kind of bizarre script in the head, with NO css, and no workable js.


<script type="module">!function(){const e=document.createElement("link").relList;if(!(e&&e.supports&&e.supports("modulepreload"))){for(const e of document.querySelectorAll('link[rel="modulepreload"]'))r(e);new MutationObserver((e=>{for(const o of e)if("childList"===o.type)for(const e of o.addedNodes)if("LINK"===e.tagName&&"modulepreload"===e.rel)r(e);else if(e.querySelectorAll)for(const o of e.querySelectorAll("link[rel=modulepreload]"))r(o)})).observe(document,{childList:!0,subtree:!0})}function r(e){if(e.ep)return;e.ep=!0;const r=function(e){const r={};return e.integrity&&(r.integrity=e.integrity),e.referrerpolicy&&(r.referrerPolicy=e.referrerpolicy),"use-credentials"===e.crossorigin?r.credentials="include":"anonymous"===e.crossorigin?r.credentials="omit":r.credentials="same-origin",r}(e);fetch(e.href,r)}}();</script>

Additional context

heres my vite.config.ts

import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";
import ViteRestart from 'vite-plugin-restart';
import viteCompression from 'vite-plugin-compression';

export default defineConfig(({command}) => ({
    base: command === 'serve' ? '/' : '/dist/',
    plugins: [
        tailwindcss(),
        ViteRestart(
            {
                restart: ['./templates/**/*']
            }
       ),
        viteCompression({
            filter: /\.(mjs|json|css|map)$/i,
        })
       
    ],
    build: {
        manifest: true,
        rollupOptions: {
            input: {
                app: 'src/js/app.js',
            }
        },
        outDir: 'public/dist/', // Output directory for built files
        emptyOutDir: true, // Ensure it cleans the folder before build
    },
    publicDir: false, // Prevents Vite from treating "public" as the root
    server: {
        port: 5173,
        host: '0.0.0.0',
        strictPort: true,
        cors: {
            origin: /https?:\/\/([A-Za-z0-9\-\.]+)?(localhost|\.local|\.test|\.site)(?::\d+)?$/
        },
        origin: "https://danielsilver-vite.ddev.site:5173",
        watch: {
            ignored: ['./storage/**', './vendor/**', './web/**', './public/**'],
        }
    }
}));

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions