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).
Build a module on its own
Section titled “Build a module on its own”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.
-
Install the module’s dependencies.
Terminal window cd Modules/Blognpm install -
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;} -
Build the assets.
Terminal window npm run buildVite compiles
resources/assets/sass/app.scssandresources/assets/js/app.jsintopublic/build-blog. -
Load the build in the layout by uncommenting the
module_vite()calls inmaster.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,ShopCartusesbuild-shopcartandvite-shopcart.hot. -
Optional: run the Vite dev server while developing the module.
Terminal window npm run devThe dev server writes
storage/vite-blog.hotwhile it runs and removes it on exit. Because each module uses its own hot file, running a module dev server does not affect@vitein the application or in other modules.Add
/storage/*.hotto 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.
Check the result
Section titled “Check the result”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/.
Build module assets from the application
Section titled “Build module assets from the application”Use this approach to run one Vite build from the application root for the application and every enabled module.
-
Publish
vite-module-loader.jsto the application root.Terminal window php artisan vendor:publish --tag=viteTo replace an older published copy, add
--force:Terminal window php artisan vendor:publish --tag=vite --force -
Update the root
vite.config.jsto wrap the config in an asyncgetConfig()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()readsmodules_statuses.jsonand imports thepathsexport from each enabled module’svite.config.js.moduleRefreshPaths('Modules')adds Blade,lang, androutesglobs so module changes trigger live reload. -
Install
sass(needed for the module SCSS entry) and build the assets.Terminal window npm install sassnpm run build -
Load the module entry points with
@vitein 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',])
Check the result
Section titled “Check the result”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.
Publish static assets
Section titled “Publish static assets”Put files that Vite does not compile in Modules/Blog/resources/assets and publish them to public/.
-
Publish the module’s assets.
Terminal window php artisan module:publish BlogThis copies
Modules/Blog/resources/assetstopublic/modules/blog, overwriting existing files. -
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//.
Check the result
Section titled “Check the result”Open /modules/blog/images/logo.png in your browser.