Skip to main content

Angular 5 - Ahead of Time build configuration for rollup

Earlier the @angular Ahead of Time compilation cookbook showed you the compilation steps using the @angular/compiler-cli (ngc) and rollup. From Angular 5 it seems to be all about the Angular CLI (using ng build --prod). In some cases though it's nice to have the rollup recipe. We also want to benefit from the build optimizer and rxjs esm module which is default in Angular 5.

So I've written down the configuration steps here for building a rollup-based AoT build for Angular 5.

The typescript configuration for the @angular/compiler-cli (ngc). Store in file tsconfig_aot.json.

{
    "compilerOptions": {
        "outDir": "js",
        "module": "es2015",
        "moduleResolution": "node",
        "target": "es5",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": [
            "dom", "es2017"
        ]
    },
    "files": [
        "src/main.ts"
    ],
    "angularCompilerOptions": {
        "annotationsAs": "decorators",
        "genDir": "aot",
        "preserveWhitespace": false,
        "skipMetadataEmit" : true
    }
}

In addition to @angular libraries and rxjs you need to install rollup with plugins and also the @angular build optimizer:

npm install --save-dev @angular-devkit/build-optimizer rollup rollup-plugin-alias rollup-plugin-node-resolve rollup-plugin-uglify

And right below is the rollup configuration (rollup-config.js). Note that we've introduced the build-optimizer and also using esm module for rxjs instead of commonjs. Both these steps produce smaller builds.

import resolve from 'rollup-plugin-node-resolve';
import alias from 'rollup-plugin-alias';
import uglify from 'rollup-plugin-uglify'
const rxPaths = require('rxjs/_esm5/path-mapping');

import { buildOptimizer } from '@angular-devkit/build-optimizer';

function angularBuildOptimizer() {
  return {
    name: 'angular-optimizer',
    transform: (content) => buildOptimizer({ content }).content,
  }
}

export default {
  input: 'js/src/main.js',
  output: {
    file: 'bundle.js',
    format: 'iife'
  },
  name: 'MyModule',
  plugins: [
    alias(rxPaths()),    
    resolve(),
    angularBuildOptimizer(),
    uglify()
  ]
};

And finally to do the build:

ngc -p tsconfig_aot.json && rollup -c rollup.config.js

Comments

Leandro said…
Hi, I am getting "ENOENT: no such file or directory, lstat" in my rollup.config.js, do you know why?

Thanks!
Thanks for sharing I really loved this article the way you presented is really amazing
this is help me to gain knowledge
Best Software training institues

Popular posts from this blog

Angular components not reloading on route change

Spent a long time wondering why route changes caused strange effects on my component, and found out that it was because my component wasn't reloading at all. I had this assumption that when a route parameter changed (e.g. /projects/1 changed to /projects/2 ) the component for the route would be reloaded. But this is not the default behaviour of the Angular router. The default behaviour of the Angular router is to reuse the route if the configuration is the same (and not reload the component). But we can override this by providing a RouteReuseStrategy to our @NgModule: providers: [ { provide: RouteReuseStrategy, useClass: AARouteReuseStrategy } ] The full custom implementation of the RouteReuseStrategy will then be like this (and it's the shouldReuseRoute method that changes the behaviour so that the component is reloaded on route parameter change): export class AARouteReuseStrategy extends RouteReuseStrategy { shouldDetach(route: ActivatedRou...

Using Angular i18n translation strings outside templates

As of today in Angular 6 i18n is only available in the templates. So what if we want to use translated messages programatically and outside templates? I still use the component template html file to declare the message to be translated, but by obtaining the TemplateRef ViewChild in the component typescript file I can get the translated messages from typescript. <ng-template #boardInvitationEmailBody let-organization="organization" let-url="url"> <ng-container i18n>You have been invited to join the board portal for</ng-container> {{organization}}. <ng-container i18n> Please follow the link to: </ng-container> {{url}} </ng-template> So in the typescript code i can now call the createEmailBodyTranlated text method to get the translated text for use outside the template. @ViewChild('boardInvitationEmailBody') boardInvitationEmailBody: TemplateRef ; createEmailBodyTranslatedText(org...

Using GIT for offline web app syncing and storage

Offline web applications, progressive web apps (PWA) using service workers for being available offline also needs a way to sync changes with the server. Mostly we store data in JSON documents, and we could identify changes per line - which is something GIT is excellent at. So how could we make the browser a GIT client? I've been working on this for some months now, and compiled libgit2 to webassembly using emscripten - and it turns out to work very well. Some video demos: Cloning a repository: https://youtu.be/rcBluzpUWE4 Merging of file changes: https://youtu.be/xfGrMwLy_tw The project is here: https://github.com/fintechneo/angular-git-filebrowser