CLI
The Aurelia CLI includes a built-in bundler that uses RequireJS or SystemJS, offering an alternative to Webpack for managing module bundling. This bundler provides flexibility and ease of use, especially for developers preferring a more straightforward setup without the complexity of Webpack.
CLI’s Built-in Bundler
The CLI's built-in bundler is designed to simplify the bundling process by automatically handling module dependencies and optimizing the application for production. It supports both RequireJS and SystemJS module loaders, allowing developers to choose based on their project requirements.
Setting Up the Built-in Bundler
To create a new project using the CLI's built-in bundler:
Initialize a New Project:
Choose Configuration Options:
Custom Setup: Select the Custom option when prompted.
Bundler Selection: Choose between CLI's built-in bundler with RequireJS or CLI's built-in bundler with SystemJS.
Transpiler: Select your preferred transpiler (Babel or TypeScript).
CSS Preprocessor: Choose a CSS preprocessor if needed (e.g., Sass, Less).
Install Dependencies: The CLI will prompt to install the necessary dependencies. Confirm to proceed:
or
Project Structure
After setup, your project will include the following key directories and files:
aurelia_project/: Contains configuration files and Gulp tasks.
tasks/: Holds Gulp task definitions.
aurelia.json: Main configuration file for the bundler.
src/: Your application's source code.
package.json: Lists project dependencies and scripts.
Dependency Management
Efficient dependency management is crucial for maintaining a scalable and maintainable project. The built-in bundler in Aurelia CLI simplifies this by offering both automatic and manual dependency tracing.
Auto Tracing
The built-in bundler automatically traces JavaScript dependencies, supporting various module formats:
Module Formats Supported:
CommonJS
AMD
UMD
Native ES Modules
Core Node.js Module Stubbing: The bundler stubs core Node.js modules to ensure browser compatibility, using the same stubs as Webpack and Browserify.
Aurelia View Template Tracing: Dependencies specified in Aurelia view templates using
<require>
are automatically traced and bundled.Automatic Installation: For ESNext applications, the bundler can automatically install missing NPM packages during development using the
--auto-install
flag:Warning:
Manual Tracing
While auto-tracing handles most dependencies, certain scenarios require manual intervention:
Modules Not Explicitly Required: Some modules may not be directly imported into your code. To include them in the bundle, specify them in the
dependencies
section ofaurelia.json
:Legacy Modules Without Module Support: For packages that do not support any module format, use the
prepend
orshim
configurations to integrate them:Prepend: Add the script paths in the
prepend
array to include them before the module loader:Shim: Wrap the legacy scripts as AMD modules:
Conditional Bundling: Handle environment-specific dependencies by specifying the
env
property:
Advanced Usage
The built-in bundler offers advanced customization options to cater to complex project requirements. These include modifying module tracing behavior and globally configuring shim settings.
onRequiringModule Callback
Customize how the bundler handles module tracing by implementing the onRequiringModule
callback in aurelia_project/tasks/build.js
:
Usage Scenarios:
Ignore Specific Modules: Prevent the bundler from including certain modules.
Bundle Additional Dependencies: Manually include dependencies that are not auto-traced.
Supply Module Implementations: Provide custom module implementations that are especially useful for legacy libraries.
Global wrapShim Configuration
Apply wrapShim
settings globally to all shimmed dependencies by modifying aurelia.json
:
NPM Package Alias
Alias NPM packages to use local copies or alternative versions by specifying the path
and main
properties:
Usage:
Useful for projects that require patched versions of libraries or local modifications.
Lazy Bundling of Main Files
Optimize bundle size by enabling lazy loading for certain packages:
Behavior:
Only the modules explicitly imported (e.g.,
import { map } from 'lodash/map';
) are bundled.The main file is bundled only when
import _ from 'lodash';
is used.
CLI + Built-in Bundler Advanced
Leverage the flexibility of RequireJS and SystemJS to implement advanced bundling strategies.
Prepend Configuration
Include scripts before the module loader to integrate non-module libraries:
Example Usage: Integrating tempusdominus-bootstrap-4
with dependencies:
Avoid importing $
from 'jquery' after it has been prepended to prevent duplicate instances.
Shim Configuration
Wrap legacy libraries that do not support module loaders using shims:
Example: Exposing moment
globally for legacy compatibility:
Use the wrapShim
option to delay the execution of legacy scripts until all dependencies are loaded.
CLI’s Built-in Bundler Cookbook
The following recipes demonstrate common integration scenarios with the CLI's built-in bundler, including popular libraries and handling specific requirements.
jQuery Integration
Install jQuery:
or
Import jQuery in your TypeScript or JavaScript files:
Normalize.css
Install Normalize.css:
or
Include in Your HTML Templates:
Bootstrap CSS v4
Install Bootstrap and Dependencies:
or
Import Bootstrap JavaScript in
main.ts
:Include Bootstrap CSS in your HTML templates:
Customize Bootstrap CSS v4
Initialize a New Project with Sass:
Choose Custom setup.
Select CLI's built-in bundler with RequireJS or SystemJS.
Choose Babel or TypeScript.
Select Sass as the CSS preprocessor.
Install Dependencies:
or
Customize Bootstrap Variables in
app.scss
:Include Compiled CSS in
app.html
:
Bootstrap CSS v3 (Legacy)
Install Bootstrap v3 and Dependencies:
or
Configure
aurelia.json
:Import Bootstrap JavaScript in
main.ts
:Include Bootstrap CSS in
app.html
:
Font Awesome v5 Free
Install Font Awesome:
or
Configure
aurelia.json
:Include Font Awesome CSS in
app.html
:
Font Awesome v4
Install Font Awesome v4:
or
Configure
aurelia.json
:Include Font Awesome CSS in
app.html
:
Foundation CSS v6
Install Foundation and Dependencies:
or
Import Foundation JavaScript in
main.ts
:Configure
app.ts
:Include Foundation CSS in
app.html
:
Materialize CSS v1
Install Materialize and Dependencies:
or
Configure
app.ts
:Include Materialize CSS in
app.html
:
TypeORM with Shimmed Decorators
Install TypeORM:
or
Configure
aurelia.json
:Adjust
tsconfig.json
:Usage in Code:
Copying Other Files (e.g., Fonts)
Resources not handled by JavaScript module loaders, such as fonts and images, need to be copied to the appropriate output directories.
Configure
aurelia.json
:Usage in CSS:
Path Mappings
Simplify import statements by defining path mappings in aurelia.json
and tsconfig.json
.
Configure
aurelia.json
:Configure
tsconfig.json
:Usage in Imports:
Styling Your Application
Manage and import styles effectively within your Aurelia application.
Without CSS Preprocessor:
With CSS Preprocessor (e.g., Sass):
Write styles in
.scss
files.Import compiled
.css
files in your HTML templates:
Updating Libraries
Keep your project's dependencies up-to-date to leverage improvements and patches.
Updating a Single Library:
or
Updating Multiple Libraries:
Define an Update Script in
package.json
:Run the Update:
or
JavaScript Minification
Optimize your application's performance by minifying JavaScript during production builds.
Default Minification Settings in
aurelia.json
:Custom Minification Options:
Last updated
Was this helpful?