Built-in bundler

The Aurelia CLI's built-in bundler offers an alternative to Webpack, supporting bundling with RequireJS or SystemJS.

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 production application. 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:

  1. Initialize a New Project:

    au new appName
  2. 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).

  3. Install Dependencies: The CLI will prompt to install necessary dependencies. Confirm to proceed:

    npm install

    or

    yarn install

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:

  1. Module Formats Supported:

    • CommonJS

    • AMD

    • UMD

    • Native ES Modules

  2. Core Node.js Module Stubbing: The bundler stubs core Node.js modules to ensure browser compatibility, using the same stubs as Webpack and Browserify.

  3. Aurelia View Template Tracing: Dependencies specified in Aurelia view templates using <require> are automatically traced and bundled.

  4. Automatic Installation: For ESNext applications, the bundler can automatically install missing NPM packages during development using the --auto-install flag:

circle-exclamation

Manual Tracing

While auto-tracing handles most dependencies, certain scenarios require manual intervention:

  1. Modules Not Explicitly Required: Some modules may not be directly imported in your code. To include them in the bundle, specify them in the dependencies section of aurelia.json:

  2. Legacy Modules Without Module Support: For packages that do not support any module format, use the prepend or shim 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:

  3. 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:

  1. Ignore Specific Modules: Prevent the bundler from including certain modules.

  2. Bundle Additional Dependencies: Manually include dependencies that are not auto-traced.

  3. 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:

Note: The wrapShim option delays the execution of legacy code until the module is loaded, ensuring proper initialization.

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:

circle-exclamation

Shim Configuration

Wrap legacy libraries that do not support module loaders using shims:

Example: Exposing moment globally for legacy compatibility:

circle-info

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

  1. Install jQuery:

    or

  2. Import jQuery in your TypeScript or JavaScript files:

Normalize.css

  1. Install Normalize.css:

    or

  2. Include in Your HTML Templates:

Bootstrap CSS v4

  1. Install Bootstrap and Dependencies:

    or

  2. Import Bootstrap JavaScript in main.ts:

  3. Include Bootstrap CSS in your HTML templates:

Customize Bootstrap CSS v4

  1. 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.

  2. Install Dependencies:

    or

  3. Customize Bootstrap Variables in app.scss:

  4. Include Compiled CSS in app.html:

circle-exclamation

Bootstrap CSS v3 (Legacy)

  1. Install Bootstrap v3 and Dependencies:

    or

  2. Configure aurelia.json:

  3. Import Bootstrap JavaScript in main.ts:

  4. Include Bootstrap CSS in app.html:

circle-info

Both bootstrap/css/bootstrap.min.css and bootstrap/dist/css/bootstrap.min.css are valid paths. Ensure the copyFiles target path matches your CSS inclusion.

Font Awesome v5 Free

  1. Install Font Awesome:

    or

  2. Configure aurelia.json:

  3. Include Font Awesome CSS in app.html:

Font Awesome v4

  1. Install Font Awesome v4:

    or

  2. Configure aurelia.json:

  3. Include Font Awesome CSS in app.html:

Foundation CSS v6

  1. Install Foundation and Dependencies:

    or

  2. Import Foundation JavaScript in main.ts:

  3. Configure app.ts:

  4. Include Foundation CSS in app.html:

Materialize CSS v1

  1. Install Materialize and Dependencies:

    or

  2. Configure app.ts:

  3. Include Materialize CSS in app.html:

TypeORM with Shimmed Decorators

  1. Install TypeORM:

    or

  2. Configure aurelia.json:

  3. Adjust tsconfig.json:

  4. Usage in Code:

circle-info

Ensure that the aurelia.json paths align with your server-side entity locations to maintain consistency between frontend and backend models.

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.

  1. Configure aurelia.json:

  2. Usage in CSS:

circle-info

Ensure all copied files are referenced correctly in your CSS or HTML to load resources at runtime.

Path Mappings

Simplify import statements by defining path mappings in aurelia.json and tsconfig.json.

  1. Configure aurelia.json:

  2. Configure tsconfig.json:

  3. Usage in Imports:

circle-info

Restart your editor after updating tsconfig.json to ensure path mappings are recognized.

Styling Your Application

Manage and import styles effectively within your Aurelia application.

  1. Without CSS Preprocessor:

  2. With CSS Preprocessor (e.g., Sass):

    • Write styles in .scss files.

    • Import compiled .css files in your HTML templates:

circle-info

Unlike Webpack, the built-in bundler processes styles before bundling. Always reference the compiled .css files in your templates.

Updating Libraries

Keep your project's dependencies up-to-date to leverage improvements and patches.

  1. Updating a Single Library:

    or

  2. Updating Multiple Libraries:

    • Define an Update Script in package.json:

    • Run the Update:

      or

circle-info

Ensure updated libraries are compatible with Aurelia version 1 to prevent integration issues.

JavaScript Minification

Optimize your application's performance by minifying JavaScript during production builds.

  1. Default Minification Settings in aurelia.json:

  2. Custom Minification Options:

circle-info

Adjust minification options to balance build performance and code optimization based on your project needs.

Last updated

Was this helpful?