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

TypeScript barrels break build #195

Closed
andreasonny83 opened this issue Oct 18, 2017 · 16 comments
Closed

TypeScript barrels break build #195

andreasonny83 opened this issue Oct 18, 2017 · 16 comments
Labels

Comments

@andreasonny83
Copy link

andreasonny83 commented Oct 18, 2017

Type of Issue

[x] Bug Report
[ ] Feature Request

Description

When trying to build a library of reusable Angular component, ng-packagr doesn't seem to package the generated distribution version correctly

How To Reproduce

ng-package.json

{
  "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
  "dest": "package",
  "lib": {
    "entryFile": "src/lib/public_api.ts"
  }
}

src/lib/public_api.ts

export * from './modules/modules-a';
export * from './modules/modules-b';
export * from './modules/modules-c';

Then, in each module folder I have an index.ts to export the specific module:

src/lib/modules/module-a/index.ts

export * from './component-a.component';
export * from './component-a.module';

Expected Behaviour

When I generate the library with ng-packagr -p ng-package.json, the ngx-customer-library.metadata.json contains only this line of code:

{"__symbolic":"module","version":3,"exports":[{"from":"./modules/module-3/"}],"metadata":{},"origins":{},"importAs":"my-angular-library"}a

However, everything is working fine if inside my src/lib/public_api.ts I add an extra /index:

export * from './modules/modules-a/index';
export * from './modules/modules-b/index';
export * from './modules/modules-c/index';

Version Information

ng-packagr: v1.4.1
node: v8.6.0
@angular: v4.4.5
rxjs: 5.4.3
zone.js: .8.18
@angular/cli: 1.3.2
@dherges
Copy link
Contributor

dherges commented Oct 19, 2017

Yes. There are some issue with barrels in the Angular compiler afaik. I don't know how and when exactly the issue occurs. However, I can say that it's better to use the full-path imports (pointing to a *.ts file) instead of the barrel imports.

@andreasonny83
Copy link
Author

Yes that has been my way around this problem but I was really hoping to benefits of barrels

@dherges dherges changed the title Cannot export library of modules TypeScript barrels break build Oct 19, 2017
@dherges
Copy link
Contributor

dherges commented Oct 19, 2017

Ok. Changed the title so that it's easier to track. I fear there's nothing we can do about it. I can try to find and post a link to the original issue but it will not be before the next week.

@dherges
Copy link
Contributor

dherges commented Oct 23, 2017

Here are issues that are related:

Seeing all this, I think it's best to not use TypeScript barrels!

@mitsoe
Copy link

mitsoe commented Nov 8, 2017

I was facing a similar issue and fixed it by pointing directly at the index file like so.

export * from './modules/modules-a/index';

@jeffora
Copy link

jeffora commented Mar 13, 2018

I have created a minimal reproduction for this issue in a situation we were encountering it here: https://github.com/jeffora/ng-barrel.

Hopefully the README is easy enough to follow, but I'm assuming this isn't actually a bug with ng-packagr, as the issue is the metadata.json is generated incorrectly with missing classes when using barrel files. cc @dherges

@jeffora
Copy link

jeffora commented Mar 19, 2018

I've also opened a pull request angular/angular#22856 which fixes the issue in my specific reproduction above, so might cover quite a few of these issues.

@ashutoshsinghparmar
Copy link

Had same issue and changing all the bareels import statement in all the files helped solving this issue.
But this is unconventional.

@JoostK
Copy link
Member

JoostK commented Jul 10, 2018

angular/angular#22856 was just merged into patch (6.0) and master (6.1), thanks a lot for your effort @jeffora!

For the record, should library authors re-bundle to create a fixed metadata.json file (once updated to the patched angular-cli of course), or is this fix purely on the consumer side?

@ianchi
Copy link

ianchi commented Sep 26, 2018

Hi, I'm building with Angular 6.1.8, and I still have this problem.
When my 'public_api.ts' exports pure directories with barrel files, the generated metadata is empty.
When exporting with folder/index notation, the metadata is correctly generated.

So for me, appending /index is still required.

@toupeira did you have to change any setting to make it work?

@toupeira
Copy link

@ianchi nevermind, I didn't test properly and /index is still required 😭

jotatoledo pushed a commit to ngx-extensions/ngx-extensions that referenced this issue Oct 20, 2018
* Simplified the inner folder structure
* Remove inner modules in extensions lib
* Expose nested modules by directly pointing
instead of using barrels.
See ng-packagr/ng-packagr/issues/195#issuecomment-342951630
jotatoledo pushed a commit to ngx-extensions/ngx-extensions that referenced this issue Oct 21, 2018
* Simplified the inner folder structure
* Remove inner modules in extensions lib
* Expose nested modules by directly pointing
instead of using barrels.
See ng-packagr/ng-packagr/issues/195#issuecomment-342951630
@alexrosenfeld10
Copy link

I'm having the same issue. The error message is rather ambiguous, adding it here for search ability as it took me a while to find this thread...

projects/mymodule/src/lib/reducers/index.ts(6,31): error TS2307: Cannot find module 'src/lib/reducers/myreducer.reducer'.
projects\mymodule\src\lib\mymodule.module.ts(15,39): Error during template compile of 'MyModule'
  Could not resolve src/lib/reducers relative to [object Object]..

brspence pushed a commit to brspence/momentum-ui that referenced this issue Jun 4, 2019
- ng-packagr/ng-packagr#195
- required to import/export from an explicit file
pauljeter pushed a commit to momentum-design/momentum-ui that referenced this issue Jun 4, 2019
@xjose97x
Copy link

Any update?

@Rlcolli4
Copy link

A question about this issues, when using barrel's to export my components I can't access any of the items in project I want to use them in. I'm trying to use them in a directive service and am unable to import them into the service for use even though the build works and I can reference the components in HTML. Is this expected with the current bug of barrels?

@alan-agius4
Copy link
Member

Closing as per #195 (comment)

PiotrSzperka pushed a commit to adia-technology/policies-consent-widget that referenced this issue Apr 10, 2020
PiotrSzperka added a commit to adia-technology/policies-consent-widget that referenced this issue Apr 10, 2020
pauljeter pushed a commit to momentum-design/momentum-angular that referenced this issue May 18, 2020
@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