Vite plugin Symfony
A Vite plugin to integrate easily Vite in your Symfony application..
- create a
entrypoints.json
file inside your build directory with your js/css/preload dependencies. - reload your browser when you update your twig files
This package is intended for use with the Symfony Bundle : pentatrion/vite-bundle.
Installation
npm i vite-plugin-symfony
Create this directory structure :
├──assets
│ ├──app.js
│ ├──app.css
│...
├──public
├──composer.json
├──package.json
├──vite.config.js
Vite base config with vite 3.x
import {defineConfig} from "vite";
import symfonyPlugin from "vite-plugin-symfony";
export default defineConfig({
plugins: [
symfonyPlugin(),
],
build: {
rollupOptions: {
input: {
app: "./assets/app.js"
},
},
}
});
and your package.json :
{
"scripts": {
"dev": "vite",
"build": "vite build"
},
"devDependencies": {
"vite": "^4.0",
"vite-plugin-symfony": "^0.7.3"
}
}
Options
type VitePluginSymfonyOptions = {
publicDirectory: string
buildDirectory: string
servePublic: boolean
refresh: boolean | string[]
viteDevServerHostname: null | string
debug: boolean;
}
Note
vite-plugin-symfony
use this options :
publicDirectory
buildDirectory
to determine the right configuration for vite
:
so you have to specify the configuration either from the plugin or from vite but not in both
import symfonyPlugin from "vite-plugin-symfony";
export default {
plugins: [
symfonyPlugin({
publicDirectory: 'public',
buildDirectory: 'build'
}),
],
};
or
import symfonyPlugin from "vite-plugin-symfony";
export default {
plugins: [
symfonyPlugin(),
],
base: '/build/',
build: {
outDir: './public/build'
}
};
With pentatrion/vite-bundle
Although it has no special dependencies, this package is intended for use with the Symfony Bundle : pentatrion/vite-bundle. This bundle provides two twig functions to load your js/css files into your templates. It also acts as a proxy by forwarding requests that are not intended for it to the Vite dev server.
Migrations
If you use previous version of the plugin consult migration page.
In depth
default js entryPoint
{
"isProd": true,
"viteServer": false,
"entryPoints": {
"welcome": {
"js": [
"/build/assets/welcome-1e67239d.js"
],
"css": [],
"preload": [],
"legacy": false
}
}
}
css entry point
{
"isProd": true,
"viteServer": false,
"entryPoints": {
"theme": {
"js": [],
"css": [
"/build/assets/theme-44b5be96.css"
],
"preload": [],
"legacy": false
}
}
}