Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: rollup inlines external dependency because import path was re-written #101

Closed
1 of 2 tasks
dherges opened this issue Aug 10, 2017 · 31 comments
Closed
1 of 2 tasks
Labels

Comments

@dherges
Copy link
Contributor

dherges commented Aug 10, 2017

Type of Issue

  • Bug Report
  • Feature Request

Description

FESM bundles include inlined external dependencies.

How To Reproduce

Exmaple w/ ngx-translate:

The ng-package.json:

{
    "$schema": "../../node_modules/ng-packagr/lib/ng-package.schema.json",
    "lib": {
        "entryFile": "public_api.ts",
        "externals": {
            "@ngx-translate/core": "ngx-translate-core"
        }
    }
}

The source code:

import { TranslateService } from '@ngx-translate/core';

export class MyLib {
  // do stuff with TranslateService
}

In the .ng_build directory, the generated .js file ends up with the following import:

import { TranslateModule, TranslateLoader } from '@ngx-translate/core/index';

The import path from @ngx-translate/core in the source was re-written to @ngx-translate/core/index, thus the externals mapping does not match and rollup inlines the third-party dependency.


To confirm the cause, the following setting then produces a good FESM bundle!

{
    "lib": {
        "entryFile": "public_api.ts",
        "externals": {
            "@ngx-translate/core": "ngx-translate-core",
            "@ngx-translate/core/index": "ngx-translate-core"
        }
    }
}

FESM Bundle:

import { TranslateLoader, TranslateModule } from '@ngx-translate/core/index';

Expected Behaviour

Externals dependencies shouldn't be rewritten.

Usage of lib.externals in ng-package.json SHOULD BE the TypeScript import path from sources!
In above examples: @ngx-translate/core.

Version Information

ng-packagr: v1.0.0-pre.14
node: v8.1.1
@angular: v4.3.3
@ngx-translate/core: v7.1.0
rxjs: 5.4.2
zone.js: 0.8.16
@dherges
Copy link
Contributor Author

dherges commented Sep 6, 2017

Cause explained in angular/angular#16084

Issue tracked by angular/tsickle#376

@dherges
Copy link
Contributor Author

dherges commented Sep 7, 2017

@dherges
Copy link
Contributor Author

dherges commented Sep 7, 2017

See also angular/angular#16152 (back-dating to April...Oo)

@avatsaev
Copy link
Contributor

avatsaev commented Sep 19, 2017

Is it why I get the Field 'browser' doesn't contain a valid alias configuration errors all over the place?

Is there any work around?

@dherges
Copy link
Contributor Author

dherges commented Sep 19, 2017

@avatsaev Possibly yes. Possibly no. Angular CLI's error messaging can be quite verbosive w/o concrete indication of the cause. The above error message often raises when an import cannot be properly resolved.

@avatsaev
Copy link
Contributor

avatsaev commented Sep 19, 2017

Could you please help me out, I've been pulling my hair for 2 days, every time I install my custom built package from an archive or npm, I get this error when I build the host angular app with angular cli:


ERROR in ./node_modules/ngx-codemirror/ngx-codemirror/ngx-codemirror.es5.js
Module not found: Error: Can't resolve '../../lib/codemirror' in '/Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/ngx-codemirror/ngx-codemirror'
resolve '../../lib/codemirror' in '/Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/ngx-codemirror/ngx-codemirror'
  using description file: /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/ngx-codemirror/package.json (relative path: ./ngx-codemirror)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/ngx-codemirror/package.json (relative path: ./ngx-codemirror)
    using description file: /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/package.json (relative path: ./node_modules/lib/codemirror)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/lib/codemirror doesn't exist
      .ts
        Field 'browser' doesn't contain a valid alias configuration
        /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/lib/codemirror.ts doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/lib/codemirror.js doesn't exist
      as directory
        /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/lib/codemirror doesn't exist

this is my ng-package.json:

{
  "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
  "lib": {
    "name": "ngx-codemirror",
    "entryFile": "src/app/lib/index.ts",
    "externals": {
      "codemirror/index": "codemirror"
    }
  }
}

My external dependency is codemirror and I use the typings from @types/codemirror

And when I package the lib without externals defined in ng-package.json i get these warnings:

'codemirror/index' is imported by .ng_build/ts/ngx-codemirror/directives/codemirror/codemirror.directive.js, but could not be resolved – treating it as an external dependency
'codemirror/index' is imported by .ng_build/ngx-codemirror/ngx-codemirror.es5.js, but could not be resolved – treating it as an external dependency
No name was provided for external module 'codemirror/index' in options.globals – guessing 'index'

@dherges
Copy link
Contributor Author

dherges commented Sep 19, 2017

Did you try the workaround as described above?

{
    "lib": {
        "entryFile": "public_api.ts",
        "externals": {
            "@ngx-translate/core": "ngx-translate-core",
            "@ngx-translate/core/index": "ngx-translate-core"
        }
    }
}

@avatsaev
Copy link
Contributor

isn't it what I did?

{
  "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
  "lib": {
    "name": "ngx-codemirror",
    "entryFile": "src/app/lib/index.ts",
    "externals": {
      "codemirror/index": "codemirror"
    }
  }
}```

@dherges
Copy link
Contributor Author

dherges commented Sep 20, 2017

Did you add both? codemirror and codemirror/index?

If still not working, you could try to check the global token "codemirror" - which one do they use? Seems like they write "CodeMirror" ... is it case sensitive? see #119

If then still not working, you could try to upgrade to ng-packagr@1.2.0 since it has added rollup-plugin-commonjs support and that may (maybe?) solve your issue.

When you find the solution, please let us know so we can keep this as documentation on how to configure external libraries.

@avatsaev
Copy link
Contributor

avatsaev commented Sep 20, 2017

So I did add both as you suggested (with 1.2.0):
Here is the public repo: https://github.com/avatsaev/ngx-codemirror

"lib": {
    "name": "ngx-codemirror",
    "entryFile": "src/app/lib/index.ts",
    "externals": {
      "codemirror/index": "codemirror",
      "codemirror": "codemirror"
    }
  }

and got this error:


ERROR in ./node_modules/ngx-codemirror/ngx-codemirror/ngx-codemirror.es5.js
Module not found: Error: Can't resolve 'codemirror/index' in '/Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/ngx-codemirror/ngx-codemirror'
resolve 'codemirror/index' in '/Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/ngx-codemirror/ngx-codemirror'
  Parsed request is a module
  using description file: /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/ngx-codemirror/package.json (relative path: ./ngx-codemirror)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/ngx-codemirror/package.json (relative path: ./ngx-codemirror)
    resolve as module
      /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/ngx-codemirror/ngx-codemirror/node_modules doesn't exist or is not a directory
      /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/ngx-codemirror/node_modules doesn't exist or is not a directory
      /Users/avatsaev/DEV/Angular/LIBS/ngx-codemirror/node_modules/node_modules doesn't exist or is not a directory
      /Users/avatsaev/DEV/Angular/LIBS/node_modules doesn't exist or is not a directory
      /Users/avatsaev/DEV/Angular/node_modules doesn't exist or is not a directory
      /Users/avatsaev/DEV/node_modules doesn't exist or is not a directory
      /Users/avatsaev/node_modules doesn't exist or is not a directory
      /Users/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory


Thing is, it works without any problems when I run the project with ng serve

@Ks89
Copy link

Ks89 commented Sep 25, 2017

I have the same problem with Mousetrap

@ronnyek
Copy link

ronnyek commented Sep 26, 2017

Same problems with @swimlane/ngx-datatable, which is unfortunate because soooo close to having converted my entire manual build process over to jsut ng-packagr + asset bundling

@ferreirarod
Copy link

Same problem with angular-calendar

@dherges
Copy link
Contributor Author

dherges commented Oct 3, 2017

Hi all, @avatsaev @ferreirarod @ronnyek @Ks89,

I wanted took a look at the ngx-codemirror error. I think it should be representative for what most of you are experiencing. This is what I got:

$ git checkout https://github.com/avatsaev/ngx-codemirror.git && cd ngx-codemirror
$ yarn install && yarn pkg
yarn install v1.1.0
info No lockfile found.
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
warning "license-webpack-plugin@1.0.2" has unmet peer dependency "webpack-sources@>= 1.0.0".
[4/4] 📃  Building fresh packages...
success Saved lockfile.
✨  Done in 381.03s.
yarn run v1.1.0
$ ng-packagr -p ng-package.json && cd dist && npm pack
Building Angular library from ng-package.json
Built Angular library from ~/github/ngx-codemirror, written to ~/github/ngx-codemirror/dist
ngx-codemirror-1.0.0.tgz
✨  Done in 43.61s.

This is a build success for the library.

What exactly is the build error? Can you provide repros?

@avatsaev
Copy link
Contributor

avatsaev commented Oct 3, 2017

It builds no problem, but try to use the resulting pkg in a host angular cli app, then compile the app in prod mode, you’ll get an error

@dherges
Copy link
Contributor Author

dherges commented Oct 3, 2017

Sorry. I stopped one step short:

$ cd dist && yarn link && cd ..
$ yarn link "ngx-codemirror"
$ yarn build
ERROR in ./dist/ngx-codemirror/ngx-codemirror.es5.js
Module not found: Error: Can't resolve 'codemirror/index' in '~/github/ngx-codemirror/dist/ngx-codemirror'
resolve 'codemirror/index' in '~/github/ngx-codemirror/dist/ngx-codemirror'
  Parsed request is a module
  using description file: ~github/ngx-codemirror/dist/package.json (relative path: ./ngx-codemirror)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: ~github/ngx-codemirror/dist/package.json (relative path: ./ngx-codemirror)
    resolve as module

@dherges
Copy link
Contributor Author

dherges commented Oct 3, 2017

Modified app.module.ts to try to find out why the import cannot be resolved.

//import {CodeMirrorModule} from 'ngx-codemirror';
import { fromTextArea } from 'codemirror/index';

It builds.


Then, I modified ngx-codemirror.es5.js by hand:

import { Directive, ElementRef, EventEmitter, Input, NgModule, Output } from '@angular/core';
import { CommonModule } from '@angular/common';
import { fromTextArea } from 'codemirror';
var CodeMirrorDirective = /** @class */ (function () {

and app.module.ts:

import {CodeMirrorModule} from 'ngx-codemirror';

And yarn build:

Time: 22340ms
chunk {inline} inline.bundle.js, inline.bundle.js.map (inline) 5.83 kB [entry] [rendered]
chunk {main} main.bundle.js, main.bundle.js.map (main) 636 kB {vendor} [initial] [rendered]
chunk {polyfills} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 217 kB {inline} [initial] [rendered]
chunk {styles} styles.bundle.js, styles.bundle.js.map (styles) 20.4 kB {inline} [initial] [rendered]
chunk {vendor} vendor.bundle.js, vendor.bundle.js.map (vendor) 2.33 MB [initial] [rendered]
✨  Done in 28.95s.

I see two possibilities:

  • either wait for tsickle#376 to be fixed
  • or try to get PR angular#19083 so good that it gets merged
  • beside that, I find it strange that one time ng build is fine when "import 'codemirror/index'" is in the app and it fails when the import is in the library ... very very strange ... it could also be an ng CLI issue. It's hard to tell because the "Field 'browser' doesn't contain a valid alias configuration" error message is so un-descriptive of the issue and it seems to happen quite often ...

@avatsaev
Copy link
Contributor

avatsaev commented Oct 3, 2017

"Field 'browser' doesn't contain a valid alias configuration" error message is so un-descriptive of the issue and it seems to happen quite often ..

very true, and there is a lot of these confusing build system err msgs.

@sumigoma
Copy link
Contributor

sumigoma commented Oct 3, 2017

I'm running into a similar situation with a library I'm trying to create with ng-packagr. The library builds fine, but when I try to npm link from a host angular-cli app, I get a similar error. The library is simple and has no peerDependencies besides @angular/core, @angular/common, rxjs and zone.js, but I get a Field 'browser' doesn't contain a valid alias configuration error as well, with it being unable to resolve any of the .d.ts files generated by the library.

Using angular 4.4.4, cli 1.4.4, ng-packagr 1.2.1, though I tried a bunch of different combinations to no avail.

@sumigoma
Copy link
Contributor

sumigoma commented Oct 3, 2017

@dherges The library produced by ng-packagr looks structurally similar to @angular's packages (the recommended angular package format), so what's the difference between what they're doing and what we're doing? I tried changing tsconfig in the library to have

"angularCompilerOptions": {
		"annotateForClosureCompiler": false

(which seems to be related to the issues above) but that didn't work either.

@dherges
Copy link
Contributor Author

dherges commented Oct 3, 2017

Hi @sumigoma,

please make sure that your issue IS or IS NOT the appended /index path.

If it's not, please check whether your issue is solved by #117 (or #132).

@sumigoma
Copy link
Contributor

sumigoma commented Oct 3, 2017

You're right, my issue actually looks more like #117. I did encounter the appended /index path problem at one point as well, but adding externals: to ng-package.json did the trick for that issue.

flynetworks pushed a commit to HarmoWatch/ngx-redux-core that referenced this issue Oct 5, 2017
@dherges
Copy link
Contributor Author

dherges commented Oct 10, 2017

though not much ng-packagr can do about it, it's causing a lot of confusion and thus it's high prio

@DavidParks8
Copy link
Member

DavidParks8 commented Oct 17, 2017

As of the release of angular 4.4.5, this should no longer be an issue due to this fix

@avatsaev
Copy link
Contributor

avatsaev commented Oct 17, 2017

Seems to be working with 4.4.5 on ngx-codemirror

@dherges
Copy link
Contributor Author

dherges commented Oct 18, 2017

Is this issue solved by angular/angular#19579 ?

I think it is.

If yes, this issue can be closed.

@dherges
Copy link
Contributor Author

dherges commented Oct 18, 2017

Should the dependency on @angular/tsc-wrapped be bumped to ^4.4.5 to work for it properly?

@Ks89
Copy link

Ks89 commented Oct 18, 2017

I confirm that now it's working also with Mousetrap. Well done and thank u.

@DavidParks8
Copy link
Member

Let's bump the dependency.

@dherges
Copy link
Contributor Author

dherges commented Oct 19, 2017

Closing this issue as it should be solved by the change in @angular/tsc-wrapped!

Should the same issue re-occur, please verify that you are using the latest version of tsc-wrapped. If then the issue still re-occurs, please let know in this thread!

@github-actions
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

This action has been performed automatically by a bot.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

7 participants