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
The build writes its output to dist/, or to the build target's outputPath if you set one:
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 builduses the build target'sdefaultConfiguration, which isproductionafterng add.ng build -c developmentusesng 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 productionserves 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:
- Everything the build makes gets the replacing file: the code of both threads, the web preview and the apps
ng build --platformmakes.ng serverebuilds 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 environmentscreatessrc/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
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:
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 withconst,letorclassthrows a used-before-definedReferenceError, 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.
initialchecks each entry's Lynx bundle.anyComponentStylechecks each component's stylesheet.- The web preview bundle isn't checked.
- Why 2 MB?
ng addraisesng new'sinitialbudget 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:
- 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
configoption at it.
Related
- Run on iOS and Android: build installable apps.
- Publish to the app stores
- Embed in an existing native app
@blotch/angular-lynx-buildAPI
