How do I actually deploy an Angular 2 + Typescript + systemjs app? How do I actually deploy an Angular 2 + Typescript + systemjs app? angular angular

How do I actually deploy an Angular 2 + Typescript + systemjs app?


The key thing to understand at this level is that using the following configuration, you can't concat compiled JS files directly.

At the TypeScript compiler configuration:

{  "compilerOptions": {    "emitDecoratorMetadata": true,    "experimentalDecorators": true,    "declaration": false,    "stripInternal": true,    "module": "system",    "moduleResolution": "node",    "noEmitOnError": false,    "rootDir": ".",    "inlineSourceMap": true,    "inlineSources": true,    "target": "es5"  },  "exclude": [    "node_modules"  ]}

In the HTML

System.config({  packages: {    app: {      defaultExtension: 'js',      format: 'register'    }  }});

As a matter of fact, these JS files will contain anonymous modules. An anonymous module is a JS file that uses System.register but without the module name as first parameter. This is what the typescript compiler generates by default when systemjs is configured as module manager.

So to have all your modules into a single JS file, you need to leverage the outFile property within your TypeScript compiler configuration.

You can use the following inside gulp to do that:

const gulp = require('gulp');const ts = require('gulp-typescript');var tsProject = ts.createProject('tsconfig.json', {  typescript: require('typescript'),  outFile: 'app.js'});gulp.task('tscompile', function () {  var tsResult = gulp.src('./app/**/*.ts')                     .pipe(ts(tsProject));  return tsResult.js.pipe(gulp.dest('./dist'));});

This could be combined with some other processing:

  • to uglify things the compiled TypeScript files
  • to create an app.js file
  • to create a vendor.js file for third-party libraries
  • to create a boot.js file to import the module that bootstrap the application. This file must be included at the end of the page (when all the page is loaded).
  • to update the index.html to take into account these two files

The following dependencies are used in the gulp tasks:

  • gulp-concat
  • gulp-html-replace
  • gulp-typescript
  • gulp-uglify

The following is a sample so it could be adapted.

  • Create app.min.js file

    gulp.task('app-bundle', function () {  var tsProject = ts.createProject('tsconfig.json', {    typescript: require('typescript'),    outFile: 'app.js'  });  var tsResult = gulp.src('app/**/*.ts')                   .pipe(ts(tsProject));  return tsResult.js.pipe(concat('app.min.js'))                .pipe(uglify())                .pipe(gulp.dest('./dist'));});
  • Create vendors.min.js file

    gulp.task('vendor-bundle', function() {  gulp.src([    'node_modules/es6-shim/es6-shim.min.js',    'node_modules/systemjs/dist/system-polyfills.js',    'node_modules/angular2/bundles/angular2-polyfills.js',    'node_modules/systemjs/dist/system.src.js',    'node_modules/rxjs/bundles/Rx.js',    'node_modules/angular2/bundles/angular2.dev.js',    'node_modules/angular2/bundles/http.dev.js'  ])  .pipe(concat('vendors.min.js'))  .pipe(uglify())  .pipe(gulp.dest('./dist'));});
  • Create boot.min.js file

    gulp.task('boot-bundle', function() {  gulp.src('config.prod.js')    .pipe(concat('boot.min.js'))    .pipe(uglify())    .pipe(gulp.dest('./dist')); });

    The config.prod.js simply contains the following:

     System.import('boot')    .then(null, console.error.bind(console));
  • Update the index.html file

    gulp.task('html', function() {  gulp.src('index.html')    .pipe(htmlreplace({      'vendor': 'vendors.min.js',      'app': 'app.min.js',      'boot': 'boot.min.js'    }))    .pipe(gulp.dest('dist'));});

    The index.html looks like the following:

    <html>  <head>    <!-- Some CSS -->    <!-- build:vendor -->    <script src="node_modules/es6-shim/es6-shim.min.js"></script>    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>    <script src="node_modules/systemjs/dist/system.src.js"></script>    <script src="node_modules/rxjs/bundles/Rx.js"></script>    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>    <script src="node_modules/angular2/bundles/http.dev.js"></script>    <!-- endbuild -->    <!-- build:app -->    <script src="config.js"></script>    <!-- endbuild -->  </head>  <body>    <my-app>Loading...</my-app>    <!-- build:boot -->    <!-- endbuild -->  </body></html>

Notice that the System.import('boot'); must be done at the end of the body to wait for all your app components to be registered from the app.min.js file.

I don't describe here the way to handle CSS and HTML minification.


You can use angular2-cli build command

ng build -prod

https://github.com/angular/angular-cli/wiki/build#bundling

Builds created with the -prod flag via ng build -prod or ng serve -prod bundle all dependencies into a single file, and make use of tree-shaking techniques.

Update

This answer was submitted when angular2 was in rc4

I had tried it again on angular-cli beta21 and angular2 ^2.1.0 and it is working as expected

This answer requires initializing the app with angular-cliyou can use

ng new myApp

Or on an existing one

ng init

Update 08/06/2018

For angular 6 the syntax is different.

ng build --prod --build-optimizer

Check the documentation


You can build an Angular 2 (2.0.0-rc.1) project in Typescript using SystemJS with Gulp and SystemJS-Builder.

Below is a simplified version of how to build, bundle, and minify Tour of Heroes running 2.0.0-rc.1 (full source, live example).

gulpfile.js

var gulp = require('gulp');var sourcemaps = require('gulp-sourcemaps');var concat = require('gulp-concat');var typescript = require('gulp-typescript');var systemjsBuilder = require('systemjs-builder');// Compile TypeScript app to JSgulp.task('compile:ts', function () {  return gulp    .src([        "src/**/*.ts",        "typings/*.d.ts"    ])    .pipe(sourcemaps.init())    .pipe(typescript({        "module": "system",        "moduleResolution": "node",        "outDir": "app",        "target": "ES5"    }))    .pipe(sourcemaps.write('.'))    .pipe(gulp.dest('app'));});// Generate systemjs-based bundle (app/app.js)gulp.task('bundle:app', function() {  var builder = new systemjsBuilder('public', './system.config.js');  return builder.buildStatic('app', 'app/app.js');});// Copy and bundle dependencies into one file (vendor/vendors.js)// system.config.js can also bundled for conveniencegulp.task('bundle:vendor', function () {    return gulp.src([        'node_modules/jquery/dist/jquery.min.js',        'node_modules/bootstrap/dist/js/bootstrap.min.js',        'node_modules/es6-shim/es6-shim.min.js',        'node_modules/es6-promise/dist/es6-promise.min.js',        'node_modules/zone.js/dist/zone.js',        'node_modules/reflect-metadata/Reflect.js',        'node_modules/systemjs/dist/system-polyfills.js',        'node_modules/systemjs/dist/system.src.js',      ])        .pipe(concat('vendors.js'))        .pipe(gulp.dest('vendor'));});// Copy dependencies loaded through SystemJS into dir from node_modulesgulp.task('copy:vendor', function () {  gulp.src(['node_modules/rxjs/**/*'])    .pipe(gulp.dest('public/lib/js/rxjs'));  gulp.src(['node_modules/angular2-in-memory-web-api/**/*'])    .pipe(gulp.dest('public/lib/js/angular2-in-memory-web-api'));    return gulp.src(['node_modules/@angular/**/*'])    .pipe(gulp.dest('public/lib/js/@angular'));});gulp.task('vendor', ['bundle:vendor', 'copy:vendor']);gulp.task('app', ['compile:ts', 'bundle:app']);// Bundle dependencies and app into one file (app.bundle.js)gulp.task('bundle', ['vendor', 'app'], function () {    return gulp.src([        'app/app.js',        'vendor/vendors.js'        ])    .pipe(concat('app.bundle.js'))    .pipe(uglify())    .pipe(gulp.dest('./app'));});gulp.task('default', ['bundle']);