Bundlers
A comprehensive guide for configuring different bundlers with Aurelia 2 applications, including Webpack, Vite, and Parcel.
Aurelia is a framework that prides itself on flexibility and minimal boilerplate. This flexibility extends to how you bundle and build your application. Whether you prefer Webpack, Vite, Parcel, or another bundler, Aurelia 2 offers a straightforward approach to configuration.
Bundling is the process of gathering your source code, templates, styles, and related assets into optimized sets of files that are easier for browsers to load. Below, we'll walk through common bundler choices in Aurelia 2 and how to integrate them.
Webpack
Webpack is a powerful and widely used bundler that allows deep customization of your build. Below is an overview of how to set up and configure a basic Aurelia 2 app with Webpack.
Installing Webpack
The @aurelia/webpack-loader
is essential for properly processing Aurelia's HTML templates and TypeScript files with Aurelia-specific transformations.
Tip: If you are migrating from Aurelia 1, you may already have a
webpack.config.js
that you can adapt for Aurelia 2, but you'll need to update the loader configuration.
Basic Configuration
Here's a complete webpack configuration for Aurelia 2:
Advanced Configuration Options
Development Aliases
For development, you can alias Aurelia packages to their development builds for better debugging:
Hot Module Replacement (HMR)
To enable HMR for Aurelia components:
TypeScript Type Checking
For enhanced TypeScript support with template type checking:
Production Optimizations
Minification: Webpack 5+ includes Terser plugin by default for production builds
Code Splitting: Configure
optimization.splitChunks
for better cachingSource Maps: Use
'source-map'
for production debuggingContent Hashing: Use
[contenthash]
in filenames for long-term caching
Vite
Vite is a fast, modern bundler (and dev server) that works excellently with Aurelia. Its plugin system provides quick project startup, HMR (Hot Module Replacement), and speedy builds.
Installing
Basic Usage
In vite.config.js
:
TypeScript Support
For TypeScript apps, add this declaration file to your project (usually generated by the Aurelia CLI):
html.d.ts
Plugin Configuration Options
The Aurelia Vite plugin accepts several configuration options:
Development Configuration
By default, the Aurelia Vite plugin automatically uses development bundles when in development mode:
Production & Code Splitting
Vite automatically optimizes for production. You can customize chunk splitting:
Source Maps and Debugging
Parcel
Parcel is a zero-configuration bundler that works well with Aurelia 2 through the official transformer.
Installing
Configuration
Create a .parcelrc
file in your project root:
For JavaScript projects:
Package.json Configuration
You can configure Aurelia-specific options in your package.json
:
TypeScript Support
For TypeScript projects, add the same html.d.ts
declaration file mentioned in the Vite section.
CSS and Styling
CSS Modules
Aurelia supports CSS modules with proper bundler configuration:
SCSS/SASS Support
Add SASS loaders for SCSS support:
Troubleshooting
Common Issues
"Cannot resolve module" errors
Ensure file extensions are properly configured in bundler resolve settings
Check that Aurelia loaders are properly configured for HTML and TypeScript files
Slow development builds
For Webpack: Enable
experiments.lazyCompilation
and proper development aliasesFor Vite: Ensure
useDev: true
is set in the Aurelia plugin options
HMR not working
Verify HMR is enabled in both bundler and Aurelia loader configurations
Check browser console for HMR-related warnings
TypeScript template errors
Ensure proper HTML type definitions are included
Consider enabling
experimentalTemplateTypeCheck
in webpack loader options
Performance Tips
Use development bundles: Always alias to
.dev.mjs
files during developmentEnable code splitting: Configure manual chunks for better loading performance
Optimize assets: Use appropriate loaders for images, fonts, and other assets
Source map strategy: Use
eval-cheap-source-map
for development,source-map
for production
Conclusion
Aurelia 2 provides excellent bundler flexibility through dedicated loaders and plugins. Vite offers the fastest development experience with minimal configuration, while Webpack provides maximum customization options. Parcel offers a good middle ground with zero-configuration setup.
Key points to remember:
Always use the official Aurelia bundler plugins/loaders
Configure proper TypeScript declarations for HTML modules
Use development bundles during development for better debugging
Enable HMR for the best development experience
For advanced configurations and bundler-specific optimizations, refer to the official documentation of your chosen bundler alongside Aurelia's guides.
Last updated
Was this helpful?