π Seamless Vite integration for ColdBox applications with automatic development/production mode switching.
This ColdBox module provides a powerful vite()
helper function for loading assets generated by Vite and the coldbox-vite-plugin
. It intelligently handles both development (hot reload) and production (manifest-based) modes automatically.
- π₯ Hot Module Replacement - Automatic development mode detection
- π¦ Production Optimization - Manifest-based asset resolution with preloading
- π― Smart Tag Generation - Automatic
<script>
and<link>
tag creation - βοΈ Zero Configuration - Works out of the box with sensible defaults
- π§ Highly Configurable - Customize paths and behavior as needed
- π Performance Optimized - Preload tags and module loading support
Install via CommandBox:
box install vite-helpers
In your ColdBox views, use the vite()
helper to load your assets:
<!--- Load a single JavaScript entry point --->
#vite('resources/assets/js/app.js')#
<!--- Load a CSS file --->
#vite('resources/assets/css/app.css')#
<!--- Load multiple assets --->
#vite(['resources/assets/js/app.js', 'resources/assets/css/app.css'])#
<!--- Get asset paths without rendering tags --->
<cfset assetPaths = vite().getAssetPaths(['app.js', 'app.css']) />
<!--- Custom configuration --->
#vite()
.setBuildDirectory('/custom/build')
.setManifestFileName('custom-manifest.json')
.render('app.js')#
The module operates in two distinct modes:
- Trigger: When
/includes/hot
file exists - Behavior: Assets served directly from Vite dev server
- Features: Hot module replacement, instant updates
- Example Output:
<script type="module" src="http://127.0.0.1:5173/@vite/client"></script> <script type="module" src="http://127.0.0.1:5173/resources/assets/js/app.js"></script>
- Trigger: When
/includes/hot
file doesn't exist - Behavior: Assets resolved via Vite manifest
- Features: Preload tags, optimized loading, cache-busted filenames
- Example Output:
<link rel="modulepreload" href="/includes/build/assets/app-5b5efed9.js" /> <script type="module" src="/includes/build/assets/app-5b5efed9.js"></script>
Configure the module in your ModuleConfig.cfc
or override in your main config/ColdBox.cfc
:
// In your ModuleConfig.cfc or ColdBox.cfc
moduleSettings = {
"vite-helpers" = {
"hotFilePath" : "/includes/hot", // Hot reload detection file
"buildDirectory" : "/includes/build", // Production build output
"manifestFileName" : ".vite/manifest.json" // Vite manifest file
}
};
Setting | Default | Description |
---|---|---|
hotFilePath |
/includes/hot |
π₯ Path to hot file that signals development mode |
buildDirectory |
/includes/build |
π Directory where Vite outputs production assets |
manifestFileName |
.vite/manifest.json |
π Vite's asset manifest file name |
This module is designed to work seamlessly with the coldbox-vite-plugin
:
-
Install the Vite plugin in your frontend project:
npm install coldbox-vite-plugin --save-dev
-
Configure your
vite.config.js
:import { defineConfig } from 'vite' import coldbox from 'coldbox-vite-plugin' export default defineConfig({ plugins: [ coldbox({ input: ['resources/assets/js/app.js', 'resources/assets/css/app.css'] }) ] })
- Start your Vite dev server:
npm run dev
- The plugin creates the hot file automatically
- Assets are served from
http://127.0.0.1:5173
- Enjoy hot module replacement! β‘
- Build your assets:
npm run build
- Vite generates the manifest and assets
- Remove or delete the hot file
- Assets are served from your build directory
Run the test suite:
# Navigate to the test runner
box server start
# Then visit: http://localhost:{port}/tests/runner.cfm
# Or use TestBox CLI
box testbox run
- Parameters:
entrypoints
(string|array) - Asset entry points to resolve - Returns: Array of resolved asset paths
- Example:
<cfset paths = vite().getAssetPaths(['app.js', 'app.css']) />
- Parameters:
entrypoints
(string|array) - Asset entry points to render - Returns: void (outputs HTML directly)
- Example:
#vite().render('app.js')#
We welcome contributions! Please see our Contributing Guidelines for details.
- Fork and clone the repository
- Install dependencies:
box install
- Make your changes
- Run tests:
box testbox run
- Format code:
box run-script format
- Submit a pull request
See CHANGELOG.md for release history and updates.
- The Vite team for creating an amazing build tool
- The ColdBox community for continuous support and feedback
- All contributors who help improve this module
Made with β€οΈ by the ColdBox Team