question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use baseUrl to have absolute imports in a library

See original GitHub issue

Type of Issue

[x] Bug Report
[ ] Feature Request

Description

I generated a lib with angular-cli, the sources are directly under the src directory.

If I set the baseUrl in the tsconfig of my lib to ./src and use absolute import in my lib (e.g., import { FormatService } from 'locale/format.service'; instead of import { FormatService } from './format.service'; in a file in the locale directory), the lib build fine, but then when angular tries to use it (it is configured via the paths in tsconfig.json), it fails with the following kind of error:

ERROR in Error during template compile of 'AppModule'
  Could not resolve locale/format.service relative to /home/victor/Codebases/frontend/target/dist-lib/my-lib.d.ts. in 'LocalizationModule'
    'LocalizationModule' contains the error at ../target/dist-lib/my-lib.ts(55,20).
target/dist-lib/locale/localization.module.d.ts(2,51): error TS2307: Cannot find module 'locale/format.service'.

How To Reproduce

Simply set a baseUrl pointing to the source directory and use absolute import instead of a relative one and use the lib in another app.

Expected Behaviour

This should work.

Version Information

$ node_modules/.bin/ng-packagr --version
ng-packagr:            3.0.0
@angular/compiler:     6.0.3
rollup:                0.59.4
tsickle:               0.29.0
typescript:            2.7.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
vvasabicommented, May 22, 2019

Ng-packagr itself doesn’t do code transformation per see

Hmm… why not? I made a proof of concept (https://github.com/vvasabi/ng-packagr/commit/4a9e689224be826f872d9d1d5801fa33f46d2eed) and was able to get absolute paths working in my library. This library, which has 2 entry points, compiles successfully itself and can be consumed correctly by both an application or another library.

I made this POC because TypeScript already said that they wouldn’t fix it and users could achieve that result using a custom transformer. If ng-packagr does not do this when it reads and before it sends source code to downstream tsickle, rollup, etc., where else can the transformation be done?

Granted, my POC has hacks specifically to work around issues in tsickle and may not be suitable for inclusion into ng-packagr itself. However, I wonder if there can be a way to plug in custom code transformations, so those who need absolute paths will simply enable a plugin. (Or, maybe put the rewrite logic behind an experimental flag…)

In my case, I am consolidating common code from applications to shared libraries. Having the ability to use absolute paths, which the applications do use, will make this task a whole lot easier. After all, it makes no sense for one (good) thing to work in applications but not in libraries. (Heck, IntelliJ will even highlight long, ugly relative import paths to suggest the shorter, cleaner and sexier absolute paths!)

I will really appreciate if the ng-packagr team can reconsider supporting absolute paths in libraries!

1reaction
alan-agius4commented, Sep 29, 2018

That is will make a library not valid as TypeScript will not rewire the paths when emitting, and thus the consumer will not be able to build when using the library and will have this issue.

See here: https://github.com/Microsoft/TypeScript/issues/9910#issuecomment-234729007

Paths mappings shouldn’t be used at a library level, at least for a library using the APF format.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't resolve absolute-path imports of library dependency ...
I tried taking the advice of the compiler warning, by editing the imports inside of SomeLibrary to use relative paths instead of absolute....
Read more >
Fixing absolute imports in TypeScript | by Tom McKenzie
Cute Feature: absolute imports with baseUrl · So now your module can't be imported into other modules · Best solution: Use the compiler...
Read more >
How Pros Get Rid of Relative Imports - DEV Community ‍ ‍
The easiest way to get rid of the long imports is to add baseUrl in the jsconfig.json (add jsconfig.json at the root level...
Read more >
Documentation - Module Resolution - TypeScript
A relative import is resolved relative to the importing file and cannot resolve to an ambient module declaration. You should use relative imports...
Read more >
Fix index.d.ts import paths with one bash command
Code editor can't resolve absolute import paths without baseUrl ... We can have relative paths for the root of the library, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found