Getting started
Install the plugin and build your first Drupal theme with Vite.
@ueberbit/vite-plugin-drupal turns Vite into a Drupal theme build tool. It bundles your assets, generates the *.libraries.yml, *.breakpoints.yml and a *_dist theme, registers custom elements by file name and gives you HMR for Twig.
Install
Add the plugin and Vite to your theme:
pnpm add -D @ueberbit/vite-plugin-drupal vite
pnpm add tailwindcss alpinejs vue @vueuse/core morphdom
Configure Vite
import uebertool from '@ueberbit/vite-plugin-drupal'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
uebertool(),
],
})
Add the scripts to the theme's package.json. The uebertool binary generates the TypeScript setup in .uebertool/ (see TypeScript).
{
"scripts": {
"dev": "uebertool && vite",
"build": "uebertool && vite build"
}
}
Wire up the theme
The build generates a hidden theme called <theme>_dist in dist/. Use it as base theme so Drupal picks up the generated libraries and breakpoints:
name: My theme
type: theme
base theme: mytheme_dist
core_version_requirement: ^11
Then add js/main.ts and css/tailwind.css:
@import "tailwindcss";
Companion module
Install uebertool_companion and its submodules alongside the theme. Several features depend on it:
uebertool_twig_loaderattaches the library of a template automatically (templates/foo/foo.tsis attached wheneverfoo.html.twigrenders).uebertool_cascade_layerwraps Drupal's own CSS in@layer drupal.uebertool_asset_rendererprevents Drupal from messing with theme assets.
Run it
pnpm dev # libraries point to the Vite dev server, Twig HMR enabled
pnpm build # hashed production assets in dist/
drush cr # after switching between dev and build
Both commands rewrite libraries.yml. Drupal caches library definitions, so clear the cache whenever you switch between dev and build.