0

I'm using a service to make http get requests:

import { Headers, Http }      from '@angular/http';
import { Observable }         from 'rxjs';
import 'rxjs/add/operator/map';

.... classcode.....

getOffices(): Observable<Office[]> {
    const url = `${this.apiurl}office`
    console.log(url);
    return this.http.get(url)
      .map(response => response.json());
  };

This all works when using SystemJS, which I'm using while I'm developing. However, when I use AoT compilation with rollup and uglify I keep getting the error:

bundle.min.js:7 EXCEPTION: Uncaught (in promise): TypeError: this.http.get(...).map is not a function

All I can find is people saying to add import 'rxjs/add/operator/map' to the main.ts file or the app.module.ts file. I've added this to every related file as well as added import 'rxjs/Rx' to all related file but nothing seems to work..

Has anybody been successful in using http request in combination with AoT compilation?

this is de tsconfig file I'm using for AoT:

    {
  "compilerOptions": {
    "target": "es2015",
    "module": "es2015",
    "moduleResolution": "node",
    "declaration": false,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "pretty": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitUseStrict": false,
    "noFallthroughCasesInSwitch": true,
    "outDir": "./dist",
    "rootDir": "./compiled"
  },
  "compileOnSave": false,
  "files": [
    "compiled/main-ngc.ts"
  ],
  "filesGlob":[
      "typings.d.ts",
      "compiled/main-ngc.ts",
      "compiled/main.ts"
      ],
  "exclude": [
    "node_modules"
  ]
}

and these are the build commands I'm using:

"build": "tsc -p tsconfig-compiled.json && gulp libs",
"rollup": "rollup -f iife -c -o dist/bundle.es2015.js",
"es5": "tsc --target es5 --allowJs dist/bundle.es2015.js --out dist/bundle.js",
"build_prod": "npm run ngc && npm run build && npm run rollup && npm run es5 && npm run minify && gulp clean:build && gulp build_prod",
"minify": "uglifyjs dist/bundle.js --screw-ie8 --compress --mangle --output dist/bundle.min.js",

any help would be welcome!

1 Answers1

0

I've found the solution. The problem was during the tree-shaking using rollup. I didn't add the rxjs library as a refrence in my rollup.config.js file.

import rollup      from 'rollup';
import nodeResolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify';

export default {
  entry: 'dist/main.js',
  dest: 'dist/bundle.min.js',
  sourceMap: true,
  format: 'iife',
  moduleName: 'main',
  plugins: [
      nodeResolve({jsnext: true, module: true}),
      commonjs({
        include: 'node_modules/rxjs/**',
      }),
      uglify()
  ]
}

The clue here is the commonjs part:

commonjs({
    include: 'node_modules/rxjs/**',
  })

Hope this helps somebody!