Vite
We use vite to control and build our assets. Vite itself is a great tool for bundling. For more information about vite, read the docs
Config
Add a build config to the vite configuration.
enhavo_app:
vite:
builds:
default:
port: '%env(VITE_ADMIN_PORT)%'
manifest: '%kernel.project_dir%/public/build/.vite/manifest.json'
base: '/build'You may find the values in your vite.config.js.
portof the vite serverserver.port. Default5200manifestpath of the vite build manifest filebuild.manifestbasevite base pathbase. Default/hostof the vite serverserver.host. Defaultlocalhost
Define a port
It is important that you define a fix port in your vite.config.js config, because if you start the vite server without defining a port, vite will start it at port 5200 and if the port is occupied it just increase the port number. So the symfony application, don't know what is the real port belong to the application if multiple vite server are started.
Mode
You can define a mode in the configuration, where to find the assets.
enhavo_app:
vite:
mode: buildThe mods are:
devAlways using the vite dev server to get assetsbuildAlways using the build directory to get assetstestTest if dev server is available and use it or use the build directory
Static assets
For static assets, which are not referenced by any other file, you can use the vite public folder. For example, you have an image logo.png and you want to display it on theme, you save it under assets/theme/public/images/logo.png. All files in the public folder will be copied to the build directory on build time. So the file is available under the path public/build/theme/images/logo.png.
├── assets
│ ├── admin
│ └── theme
│ └── public
│ └──images
│ └──logo.png <-- Save your file here
└── public
└── build
├── admin
└── theme
└──images
└──logo.png <-- Automatically copied hereWhen you working in vite dev mode, the logo.png is not copied to the public/build/theme/images/logo.png, instead it is available on the vite dev server. Use the twig asset function with package vite and it will automatically use the file from the vite dev server, if it is available.
<img src="{{ asset('/build/theme/images/logo.png', 'vite') }}" />Load entrypoint
Several functions like vite_css_tags, vite_js_tags, vite_js_preload_tags where added to twig, to load an entrypoint, wheater in dev or build mode.
<!DOCTYPE html>
<html lang="en">
<head>
{{ vite_css_tags('entrypoints/app.ts') }}
</head>
<body>
{{ vite_js_tags('entrypoints/app.ts') }}
{{ vite_js_preload_tags('entrypoints/app.ts') }}
</body>
</html>Multiple builds
If your application has multiple separate frontends, for example an admin panel and a public theme, you can configure multiple vite builds. Each build has its own configuration, dev server port, and output directory. This allows you to run separate vite instances for each frontend during development.
Define multiple builds in your configuration by giving each build a unique name:
enhavo_app:
vite:
builds:
admin:
port: '%env(VITE_ADMIN_PORT)%'
manifest: '%kernel.project_dir%/public/build/admin/.vite/manifest.json'
base: '/build/admin'
theme:
port: '%env(VITE_THEME_PORT)%'
manifest: '%kernel.project_dir%/public/build/theme/.vite/manifest.json'
base: '/build/theme'Each build should have its own vite.config.js with a matching output directory:
build: {
outDir: '../public/build/theme',
}When loading entrypoints in twig, pass the build name as the second argument to specify which build to use:
<!DOCTYPE html>
<html lang="en">
<head>
{{ vite_css_tags('entrypoints/app.ts', 'theme') }}
</head>
<body>
{{ vite_js_tags('entrypoints/app.ts', 'theme') }}
{{ vite_js_preload_tags('entrypoints/app.ts', 'theme') }}
</body>
</html>Build names
If you omit the build name in the twig functions, the default build will be used. Make sure to name one of your builds default if you want to use this shorthand, or always specify the build name explicitly.