For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/building-for-production.md.

Build your app

ng build compiles your app into a bundle, the single file a Lynx app runs from. This page covers what the build produces, production and development builds, environment files, size budgets and customizing the build tool.

To turn the bundle into apps you can install, see Run on iOS and Android.

Build the bundle

ng build

The build writes its output to dist/, or to the build target's outputPath if you set one:

dist/
├── main.lynx.bundle
├── main.web.bundle
└── static/
    ├── image/
    │   └── logo.bf916cea19.png
    └── font/
        └── Inter-Regular.275384a067.ttf
FileWhat it is
main.lynx.bundleYour whole app: compiled code, templates and styles. iOS and Android run this file
main.web.bundleThe same app for the web preview
static/Images, fonts, SVGs and media your code imports. Small files are put inside the bundle instead

A bundle is like the JavaScript and CSS of a web app packed into one file. It doesn't run on its own: a host app with the Lynx engine loads it. See How a Lynx app runs.

ng build runs the @blotch/angular-lynx-build:application builder. It bundles with RSpeedy, Lynx's build tool, and takes the entry point from the build target's browser option, as in any Angular app.

Production and development builds

ng build makes a production build: minified, with Angular's dev mode off, and AngularLynx's debugging and profiling code removed.

As in Angular, the optimization option decides which kind of build you get. There's no separate mode option:

  • ng build uses the build target's defaultConfiguration, which is production after ng add.
  • ng build -c development uses ng new's development configuration ("optimization": false), with dev mode on. Like a production build, it loads its images from the app, not from a dev server.
  • Flags override single options, e.g. ng build --source-map.
  • ng serve -c production serves a production build.

Some tools only work in development builds, such as LynxLogger and the render counters.

Environment files

As in a web app, a configuration can swap one file for another with fileReplacements. That's how each build picks its environment, such as the backend it talks to:

angular.json
"configurations": {
  "production": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.prod.ts"
      }
    ]
  }
}
  • Everything the build makes gets the replacing file: the code of both threads, the web preview and the apps ng build --platform makes. ng serve rebuilds when you edit it.
  • Paths are relative to the workspace, like every path in angular.json, and can lead out of it, e.g. ../shared/environment.prod.ts.
  • ng generate environments creates src/environments/ and a replacement for the development configuration. It warns that the build target uses a third-party builder; the replacement works.

Rebuild on every change

ng build --watch

This rebuilds the bundle whenever a file changes, like the npm run watch script that ng new adds. It can't be combined with --platform. While developing, use ng serve instead.

Lazy code ships in the bundle

Lazy routes (loadComponent()) and @defer blocks work as written. Their code is built into main.lynx.bundle, though, not into separate files.

So lazy loading doesn't make the bundle smaller. It still delays creating the views until they're needed. See Routing and @defer.

Make the script smaller with eagerImports

A lazy route makes the main thread's script bigger than the same code with no import(). The bundler can't merge a module that an import() loads into the module that imports it.

So every module it shares with the rest of your app stays a module of its own, with the glue code that joins them. That covers Angular's packages, rxjs, AngularLynx, and your own shared files.

The eagerImports build option compiles your import()s on the main thread as static imports, so that code can be merged:

angular.json
"architect": {
  "build": {
    "builder": "@blotch/angular-lynx-build:application",
    "options": {
      "eagerImports": true
    }
  }
}

In a benchmark app with five lazy screens, the main thread's script went from 625 kB to 601 kB (−3.8%). The engine reads the whole script as it loads, so a smaller one should take less memory too, though that saving hasn't been measured on a phone.

It's off by default because it changes when your code runs:

  • Each module an import() loads runs when the app starts, as a static import does, not when its route is first shown. Code at its top level runs at launch, even for a route nobody opens, and the time it takes is added to the launch.
  • An import cycle through your routes file can break. If a lazily loaded module imports the routes file that loads it, the import() kept them apart. As static imports, one of them runs first. While it runs, reading an export of the other that's declared with const, let or class throws a used-before-defined ReferenceError, because the other hasn't run yet. An exported function declaration is hoisted, so the function itself is there to call, but its body throws the same way if it reads such an export too early. That's a read at its top level, or in a class's static field or static block, which run when the class is defined. A read in a function or a method is fine when it's called later, such as when the route is shown.

@defer blocks' imports stay lazy, and so do other packages' and an import() whose path isn't a string. With experimental_isLazyBundle, which puts each lazy route in a bundle of its own, the option does nothing.

Budgets

Budgets fail the build when it grows past a size you set. They work as in a web app, with the same units and messages: over the warning size the build prints a warning, and over the error size it fails.

angular.json
"configurations": {
  "production": {
    "budgets": [
      { "type": "initial", "maximumWarning": "2MB", "maximumError": "5MB" },
      { "type": "anyComponentStyle", "maximumWarning": "4kB", "maximumError": "8kB" }
    ]
  }
}
  • initial checks each entry's Lynx bundle. anyComponentStyle checks each component's stylesheet.
  • The web preview bundle isn't checked.
  • Why 2 MB? ng add raises ng new's initial budget from 500 kB and 1 MB to 2 MB and 5 MB. A Lynx bundle carries more than a web app's first download. Besides Angular, it holds AngularLynx's renderer and the runtime the background thread loads (see Lynx's two threads). So a new app's bundle is already about 720 kB, 300 kB gzipped.

The build plugin's README explains what every budget type measures.

Customize RSpeedy

An app doesn't need a lynx.config.ts. Add one to change something RSpeedy does, such as its environments, its entries or the AngularLynx plugin's options. Whatever it doesn't set still comes from angular.json.

Import defineConfig and the plugin from @blotch/angular-lynx-build, which brings RSpeedy with it:

lynx.config.ts
import { defineConfig, pluginAngularLynx } from '@blotch/angular-lynx-build';

export default defineConfig({
  plugins: [pluginAngularLynx({ enableCSSInheritance: true })],
});
  • The plugin's options are listed in the build API reference.
  • RSpeedy's own options, such as output.assetPrefix, are in RSpeedy's docs.
  • Another config file? Point the build target's config option at it.