ng-packagr: Secondary entry point bug: `src/` path gets part of package import path
Type of Issue
[x] Bug Report
[ ] Feature Request
Description
When placing the main entry point in src/ and every subentry point in a new folder inside src/
you will get the primary point correctly at @my/package-name but every secondary entry point is only accessible by the path @my/package-name/src/*.
This is a remaining error after fixing the base url stuff in this PR https://github.com/dherges/ng-packagr/pull/862
Relates to: https://github.com/dherges/ng-packagr/issues/854
How To Reproduce
I created a new example in the ng-packagr project. See my branch https://github.com/georgiee/ng-packagr/tree/src-entry-point-bug.
- Launch example
nested-src. - It works. But only because it’s fixed by providing this import
import { STATIC_FOO_VALUE, FooClass } from '@sample/nested-src/src/foo';
instead of
import { STATIC_FOO_VALUE, FooClass } from '@sample/nested-src/foo';
Expected Behaviour
This path should work:
import { STATIC_FOO_VALUE, FooClass } from '@sample/nested-src/foo';
Version Information
$: ng -v
@angular-devkit/architect 0.6.3 (cli-only)
@angular-devkit/core 0.6.0
@angular-devkit/schematics 0.6.3 (cli-only)
@angular/cdk 6.0.1
@angular/router 6.0.1
@ngtools/json-schema 1.1.0
@schematics/angular 0.6.3 (cli-only)
@schematics/update 0.6.3 (cli-only)
rxjs 6.1.0
typescript 2.7.2
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 9
- Comments: 30 (14 by maintainers)
Is there any intention on clarifying why placing package.json under the src folder works here? First, it is very commonplace and standard for the package.json to be at the root level of the package it is describing. Second, it’s not intuitive that
produces the packages:
while
produces the packages:
Issues with pathing and dependencies notwithstanding, the location of my source code does not change. Why does the location of the package.json file itself dictate package destinations?
My exact use case doesn’t need a main module so I would rather have a package.json at the root that has a name of the common namespace (abc/def, in my example) and is completely independent from the packaged code itself and ng/package.json files in each of the feature modules that dictate how they would be packaged.
This also effects when adding secondary entry point to lib generated from
ng g libraryHey everyone, so I managed to compile my library without the use of a /src in the root but I had to make some changes that’s worth mentioning. I followed the suggestion by @alan-agius4 to put secondary entry points at the root of the library. Also combined what I could gather from the Angular Package Format and the secondary entry points documentation. Notice the absence of a /src folder in my root directory.
My versions:
The following is my library directory structure:
After I run
ng build --project=my-lib, I get the following directory structure indist/my-lib:Just as a side note as well, the
public-api.tsat my root has the following code:Hopefully this helps and I didn’t miss out anything. Also my first time contributing.
@alan-agius4 really thanks so much again, I was able to get it working in my dummy issue repo fairly easy, without even duplication of dependencies: https://github.com/abbazabacto/ngpackagr-issue/commit/9534c2b6271e4618002c75253a84ad3c9837cdd4
I can totally live with the
package.jsonfile in the./srcdirectory!I did the simple steps:
./srcwith the correct package name, version,ngPacakge-config and just thepeerDependenciesng-packagr -p src/package.jsonngPackage-config in./src/package.jsonto be../distso the output is the same as in my other repos@Qarun-Qadir-Bissoondial
Don’t forget testing:
I too have been fighting this for far too long. Your setup has gotten me the closest with only one annoyance, that that
test.tsfile shows up in test coverage reports. A small price to pay though.I had a look at this, and it seems to be an Angular bug. I have opened an issue: https://github.com/angular/angular/issues/24225
It looks like it’s a problem with barrel exports.
@alan-agius4 I just pushed the commits to master, added one more commit to update the
README.mdwith the same issue description as above. Hope you can spot where the issue in metadata generation comes from. GL and thanks again!I am happy to see that everyone is a bit more happy now 😄
Thanks for the clarification about the src folders. Makes sense this way finally 😃 I will give the second package.json a try. Looks promising what @abbazabacto have done in the showcased commit 👌Thanks for this!
@alan-agius4 The primary endpoint should always be based on the name of the
package.jsonas this is the name of the node module upon publishing, even if thepublic_api.tsis placed somewhere else. (Actually when I tried to place thepublic_api.tssomewhere else, it failed on some of my own aliases, guess I kind of understand why this is happening now)The request is all about the secondary endpoints (and maybe also on the location of the main entry point), they are based on the paths relative to this
package.json. Which means that your development files should always be in the same file structure as you want the eventual package/bundle paths to be.I can understand that it become quite cumbersome to make this work on your end. Maybe it is just something we have to learn to live with.
I did updated a previous ngpackagr issue repo to demonstrate what a desired architecture would be https://github.com/abbazabacto/ngpackagr-issue:
src/index.tssrc/**./demo(Angular CLI) is using path alias to have examples demonstrating implementationYeah it’s exactly like @abbazabacto describes it. To draw a picture of it:
src/and files liketest.ts,README.mdare located one level higher.Thanks for your endless support @alan-agius4!
Okay, so from my understanding you want something like (Angular Material2)[https://github.com/angular/material2/tree/master/src/lib], IMHO, if you want something like that you should place a
package.jsonundersrcAnd trigger the build there. That is the most straight forward way to achieve the desired structure, and If you do this you wouldn’t even need to create path mappings yourselves asng-packagrhandles these.I will still try to find a solution for this feature/issue but from my end, I am a bit busy with other stuff right now. So it has to wait a couple of days, maybe weeks.
My worries is that basing the structure and naming of secondaries based on the
lib.entryPointis quite fragile. For instance, if you have something like the below it it will be hell to create the proper naming for 2nd and 3rd level entry point. And in this instance, why shouldng-packagrmake a distinction if the folder issrcor not? It will become rather complex to create the naming of the packages, and where they should be emitted.I don’t think that using
ngPackage.libis good enough. Especially for 3 level entrypoints.