oveleon/contao-theme-compiler-bundle
Contao Theme Compiler Bundle adds an automated workflow for compiling and managing theme assets in Contao. It helps build CSS/JS from sources, handles bundling and output paths, and streamlines deploying optimized assets for your site’s frontend.
Install the Bundle
Add to composer.json:
composer require oveleon/contao-theme-compiler-bundle
Enable in config/bundles.php:
return [
// ...
Oveleon\ContaoThemeCompilerBundle\OveleonContaoThemeCompilerBundle::class => ['all' => true],
];
Configure the Bundle Publish the default config:
php bin/console oveleon:theme-compiler:install
Edit config/packages/oveleon_theme_compiler.yaml to define:
src/Resources/public/css, src/Resources/public/js).web/assets/).postcss, webpack, or vite).First Compilation Run the compiler:
php bin/console oveleon:theme-compiler:compile
Verify assets appear in web/assets/ (or your configured output).
php bin/console oveleon:theme-compiler:watch
config/packages/oveleon_theme_compiler_[theme_name].yaml.<link rel="stylesheet" href="{{ asset('assets/css/main.css') }}">
Git Hooks for Automation
Add a post-merge hook to compile assets on branch updates:
php bin/console oveleon:theme-compiler:compile --env=dev
Use --env to target specific environments (e.g., prod for minified builds).
Modular Theme Development
themes/[theme_name]/assets/.config/packages/oveleon_theme_compiler_[theme_name].yaml:
oveleon_theme_compiler:
themes:
my_theme:
source: "%kernel.project_dir%/themes/my_theme/assets"
output: "web/assets/my_theme"
@import in CSS/SCSS to share base styles across themes.Build Pipeline
tools:
css:
postcss: true
options:
plugins: [autoprefixer, cssnano]
js:
vite: true
%kernel.environment% to toggle features:
tools:
js:
sourcemaps: "%kernel.debug%"
Contao Asset Management
Url::asset() helper with compiled paths:
Url::asset('assets/css/main.css', true); // Force fresh load
Asset::addCss() with versioned filenames:
Asset::addCss('assets/css/main-' . filemtime('web/assets/css/main.css') . '.css');
tools:
js:
vite: true
config: "vite.config.js" # Path to your Vite config
themes:
backend:
source: "%kernel.project_dir%/src/Resources/contao/backend"
output: "system/modules/backend/assets"
tl_theme table:
// In a Contao hook (e.g., `onInitializeSystem`)
if ($this->Input->get('act') === 'switch_theme') {
$theme = $this->Input->get('theme');
$this->compileTheme($theme);
}
Caching Issues
php bin/console cache:clear
php bin/console oveleon:theme-compiler:clear-cache
var/cache/dev/oveleon_theme_compiler for stale manifests.Tool Configuration Conflicts
postcss, webpack) are installed globally or via devDependencies:
npm install -g postcss-cli
tools config.File Overwrites
output: "web/assets/%theme_name%"
Contao Asset Versioning
config/local.php:
$GLOBALS['TL_CONFIG']['versionAssets'] = true;
Dry Runs Test changes without overwriting files:
php bin/console oveleon:theme-compiler:compile --dry-run
Outputs a diff of changes to var/log/oveleon_theme_compiler.log.
Verbose Logging Enable debug mode for detailed logs:
php bin/console oveleon:theme-compiler:compile --env=dev -v
Manifest Inspection
Check the generated manifest file (e.g., web/assets/manifest.json) for:
Custom Compilers
Extend the bundle’s compiler system by implementing Oveleon\ContaoThemeCompilerBundle\Compiler\CompilerInterface:
namespace App\Compiler;
use Oveleon\ContaoThemeCompilerBundle\Compiler\CompilerInterface;
class CustomCompiler implements CompilerInterface {
public function compile(string $source, string $output): void {
// Custom logic (e.g., call a CLI tool)
}
}
Register in config/packages/oveleon_theme_compiler.yaml:
compilers:
custom:
class: App\Compiler\CustomCompiler
extensions: ['.custom']
Pre/Post-Compile Hooks Use Symfony events to hook into the compilation process:
// In a service or controller
$dispatcher->addListener(
OveleonThemeCompilerEvents::PRE_COMPILE,
function (PreCompileEvent $event) {
// Modify $event->getFiles() or $event->getOptions()
}
);
Dynamic Theme Detection
Override Oveleon\ContaoThemeCompilerBundle\Theme\ThemeDetectorInterface to auto-detect themes from Contao’s database or filesystem.
Environment Variables
Use Symfony’s %env() in config for dynamic paths:
output: "%kernel.project_dir%/%env(ASSET_OUTPUT_DIR)%/assets"
Set via .env:
ASSET_OUTPUT_DIR=web/custom_assets
Theme Inheritance Merge configs for child themes by extending parent configs:
# config/packages/oveleon_theme_compiler_child.yaml
extends: oveleon_theme_compiler_parent.yaml
oveleon_theme_compiler:
tools:
css:
postcss:
options:
plugins: [parent_plugins, child_plugin]
Excluding Files Skip specific files/folders in compilation:
exclude:
- "src/Resources/public/css/_private.scss"
- "node_modules/"
How can I help you explore Laravel packages today?