Skip to content

Build module assets with Vite

At the end, the Blog module serves compiled CSS and JavaScript. Choose one of two approaches: a separate Vite build per module, or one root Vite build for the whole application. Both require Node.js and a Blog module (see Create a module).

Use this approach when a module manages its own assets and build output (public/build-blog).

module:make Blog generates Modules/Blog/vite.config.js and Modules/Blog/package.json. The generated package.json requires vite ^8.0.0, laravel-vite-plugin ^3.1, and sass ^1.70.0, which need Node.js ^20.19.0 || >=22.12.0. You can ignore npm 11 install-scripts warnings for @parcel/watcher.

  1. Install the module’s dependencies.

    Terminal window
    cd Modules/Blog
    npm install
  2. Add a rule to resources/assets/sass/app.scss. The generated file is empty, and Vite compiles an empty SCSS entry into a JS chunk instead of a stylesheet.

    Modules/Blog/resources/assets/sass/app.scss
    body {
    color: #222;
    }
  3. Build the assets.

    Terminal window
    npm run build

    Vite compiles resources/assets/sass/app.scss and resources/assets/js/app.js into public/build-blog.

  4. Load the build in the layout by uncommenting the module_vite() calls in master.blade.php.

    Modules/Blog/resources/views/components/layouts/master.blade.php
    {{-- in <head> --}}
    {{ module_vite('build-blog', 'resources/assets/sass/app.scss', storage_path('vite-blog.hot')) }}
    {{-- before </body> --}}
    {{ module_vite('build-blog', 'resources/assets/js/app.js', storage_path('vite-blog.hot')) }}

    Pass three arguments: the build directory (build-<lowercase name>), the entry path relative to the module folder, and the module’s hot file path (storage/vite-<lowercase name>.hot). For example, ShopCart uses build-shopcart and vite-shopcart.hot.

  5. Optional: run the Vite dev server while developing the module.

    Terminal window
    npm run dev

    The dev server writes storage/vite-blog.hot while it runs and removes it on exit. Because each module uses its own hot file, running a module dev server does not affect @vite in the application or in other modules.

    Add /storage/*.hot to your root .gitignore.

To use Vue, React, or Svelte in a standalone module build, uncomment the framework plugin in Modules/Blog/vite.config.js and install its npm package in Modules/Blog.

Confirm that public/build-blog/manifest.json exists. Open a page using the layout and verify it renders a <link rel="stylesheet"> and <script type="module"> pointing to /build-blog/assets/.

Use this approach to run one Vite build from the application root for the application and every enabled module.

  1. Publish vite-module-loader.js to the application root.

    Terminal window
    php artisan vendor:publish --tag=vite

    To replace an older published copy, add --force:

    Terminal window
    php artisan vendor:publish --tag=vite --force
  2. Update the root vite.config.js to wrap the config in an async getConfig() function and merge module asset paths. Keep any custom plugins and options you already have.

    vite.config.js
    import { defineConfig } from 'vite';
    import laravel, { refreshPaths } from 'laravel-vite-plugin';
    import { bunny } from 'laravel-vite-plugin/fonts';
    import collectModuleAssetsPaths, { moduleRefreshPaths } from './vite-module-loader.js';
    import tailwindcss from '@tailwindcss/vite';
    async function getConfig() {
    const paths = [
    'resources/css/app.css',
    'resources/js/app.js',
    ];
    const allPaths = await collectModuleAssetsPaths(paths, 'Modules');
    return defineConfig({
    plugins: [
    laravel({
    input: allPaths,
    refresh: [...refreshPaths, ...moduleRefreshPaths('Modules')],
    fonts: [
    bunny('Instrument Sans', {
    weights: [400, 500, 600],
    }),
    ],
    }),
    tailwindcss(),
    ],
    server: {
    watch: {
    ignored: ['**/storage/framework/views/**'],
    },
    },
    });
    }
    export default getConfig();

    collectModuleAssetsPaths() reads modules_statuses.json and imports the paths export from each enabled module’s vite.config.js. moduleRefreshPaths('Modules') adds Blade, lang, and routes globs so module changes trigger live reload.

  3. Install sass (needed for the module SCSS entry) and build the assets.

    Terminal window
    npm install sass
    npm run build
  4. Load the module entry points with @vite in the layout.

    Modules/Blog/resources/views/components/layouts/master.blade.php
    @vite([
    'Modules/Blog/resources/assets/sass/app.scss',
    'Modules/Blog/resources/assets/js/app.js',
    ])

Open public/build/manifest.json and look for Modules/Blog/resources/assets/js/app.js. Toggle Blog in modules_statuses.json and rebuild to confirm the entry follows the module’s enabled status.

Put files that Vite does not compile in Modules/Blog/resources/assets and publish them to public/.

  1. Publish the module’s assets.

    Terminal window
    php artisan module:publish Blog

    This copies Modules/Blog/resources/assets to public/modules/blog, overwriting existing files.

  2. Reference a published file with Module::asset().

    Modules/Blog/resources/views/index.blade.php
    <img src="{{ Module::asset('blog:images/logo.png') }}" alt="Blog">

    Use <lowercase module name>:<path in assets>. The helper returns a protocol-relative URL starting with //.

Open /modules/blog/images/logo.png in your browser.