title: AOT compilation fails when factory function imported from library is used in DI in Angular
body: <p>I have an Angular 6.1 application, which imports some external module.</p>
  
  <p>When I'm compiling the application in AOT mode:</p>
  
  <p><code>$ ng build --aot</code></p>
  
  <p>I'm getting this error:</p>
  
  <pre><code>ERROR in ./src/app.component.ngfactory.js
  Module not found: Error: Can't resolve '/home/user/project/node_modules/@acme/library/src/library/library' in '/home/user/project/src/app'
  resolve '/home/user/project/node_modules/@acme/library/src/library/library' in '/home/user/project/src/app'
    using description file: /home/user/project/package.json (relative path: ./src/app)
      Field 'browser' doesn't contain a valid alias configuration
      using description file: /home/user/project/node_modules/@acme/library/package.json (relative path: ./src/library/library)
        no extension
          Field 'browser' doesn't contain a valid alias configuration
          /home/user/project/node_modules/@acme/library/src/library/library doesn't exist
        .ts
          Field 'browser' doesn't contain a valid alias configuration
          /home/user/project/node_modules/@acme/library/src/library/library.ts doesn't exist
        .tsx
          Field 'browser' doesn't contain a valid alias configuration
          /home/user/project/node_modules/@acme/library/src/library/library.tsx doesn't exist
        .mjs
          Field 'browser' doesn't contain a valid alias configuration
          /home/user/project/node_modules/@acme/library/src/library/library.mjs doesn't exist
        .js
          Field 'browser' doesn't contain a valid alias configuration
          /home/user/project/node_modules/@acme/library/src/library/library.js doesn't exist
        as directory
          /home/user/project/node_modules/@acme/library/src/library/library doesn't exist
  [/home/user/project/node_modules/@acme/library/src/library/library]
  [/home/user/project/node_modules/@acme/library/src/library/library.ts]
  [/home/user/project/node_modules/@acme/library/src/library/library.tsx]
  [/home/user/project/node_modules/@acme/library/src/library/library.mjs]
  [/home/user/project/node_modules/@acme/library/src/library/library.js]
   @ ./src/app.component.ngfactory.js 12:0-92 30:102-122 30:186-206 33:204-224 33:289-309 36:204-224 36:289-309 45:102-122 45:187-207 51:102-122 51:187-207 120:102-122 120:187-207
   @ ./src/app/app.module.ngfactory.js
   @ ./src/main.ts
   @ multi ./src/main.ts
  </code></pre>
  
  <p>The path <code>node_modules/@acme/library/src/library/library</code> leads to a <code>library.d.ts</code> file, which is present in module directory.</p>
  
  <p>If I remove typing information altogether from the external package, the application compiles and works correctly. Also, if I manually merge all typings into a single <code>index.d.ts</code> file, the compilation completes just fine. </p>
  
  <p>I think there's something wrong in how the compiler resolves type definitions files, it appears like it's not looking for files with <code>.d.ts</code> extension for some reason.</p>
  
  <hr>
  
  <h1>Update 1</h1>
  
  <p>The external module <code>@acme/library</code>, is also developed and pre-compiled by me. It's a simple TypeScript library with some exported classes and functions. It's not using anything fancy from Angular like decorators and stuff.</p>
  
  <p>It's <code>package.json</code> has the following fields:</p>
  
  <pre><code>"es2015": "index.es2015.js",
  "main": "index.min.js",
  "module": "index.es5.js",
  "typings": "index.d.ts",
  </code></pre>
  
  <p>All mentioned files are present in the root directory of the package.</p>
  
  <hr>
  
  <h1>Update 2</h1>
  
  <p>After further investigation, it looks like the problem is caused by the dependency injection. Please see the example below.</p>
  
  <p>I'm using a custom factory provider in order to inject an instance of <code>Library</code> to the constructor of the component.</p>
  
  <p>The <code>libraryFactory</code> function is imported from the external module and it looks like this:</p>
  
  <pre><code>export function libraryFactory(): Library {
    const dep1 = new Dep1();
    const dep2 = new Dep2();
    return new Library(dep1, dep2);
  }
  </code></pre>
  
  <h3>Failing example</h3>
  
  <pre><code>import {Component} from '@angular/core';
  
  import {Library, libraryFactory} from '@acme/library';
  
  
  @Component({
    selector: 'app-foo',
    templateUrl: './foo.component.html',
    providers: [
      {
        provide: Library,
        useFactory: libraryFactory
      }
    ]
  })
  export class FooComponent {
  
    constructor(public library: Library) {
    }
  
  }
  </code></pre>
  
  <h3>Working example</h3>
  
  <p>However, if I get rid of the DI it compiles and works correctly:</p>
  
  <pre><code>import {Component} from '@angular/core';
  
  import {Library, libraryFactory} from '@acme/library';
  
  
  @Component({
    selector: 'app-foo',
    templateUrl: './foo.component.html'
  })
  export class FooComponent {
  
    public library: Library;
  
  
    constructor() {
      this.library = libraryFactory();
    }
  
  }
  </code></pre>
  
  <hr>
  
  <p>What could be causing this problem?</p>
  
  <p>I will be glad to provide more specific information if needed on first request.</p>