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

ngcc with preserveWhitespaces #35871

Closed
tscislo opened this issue Mar 5, 2020 · 22 comments
Closed

ngcc with preserveWhitespaces #35871

tscislo opened this issue Mar 5, 2020 · 22 comments

Comments

@tscislo
Copy link

@tscislo tscislo commented Mar 5, 2020

I'm using Angular 9 with Ivy to compile my app. This app uses a bunch of libs which it compiles using ngcc form View Engine to Ivy. I want those libs to be compiled by ngcc with preserveWhitespaces: true, currently this does not happen. I tried setting preserveWhitespaces in tsconfig of my project, but it does not work.

When those libs are used as they are without ngcc compilation I get preserveWhitespaces correctly respected.

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Mar 5, 2020

Indeed you are right that we are not offering this option in ngcc...

I think the relevant place is where we create the options for the Angular compiler here:

const options: ts.CompilerOptions = {
allowJs: true,
maxNodeModuleJsDepth: Infinity, rootDir, ...pathMappings
};

We need to decide the best approach for supporting this. On the one hand we could just add preserveWhitespaces as an option to ngcc but perhaps we should be able to accept a tsconfig.json path and parse it ourselves...

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Mar 5, 2020

Tracking at FW-1972

@tscislo
Copy link
Author

@tscislo tscislo commented Mar 5, 2020

@petebacondarwin thanks for response! Is there any workaround I can use now for old libs which use preserveWhitespaces: true while I want to consume them in my Angular 9 app with Ivy enabled?

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Mar 5, 2020

If you have access to the original library code, could you try specifying it explicitly in the @Component() decorators. See https://angular.io/api/core/Component#preservewhitespaces

@tscislo
Copy link
Author

@tscislo tscislo commented Mar 5, 2020

Yeah, that works. However I do not have source code for every library that I use...

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Mar 5, 2020

I can't think of any other workaround until we fix the configuration issue.

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Mar 5, 2020

@tscislo - a quick question... if you turn off Ivy and consume these libraries does the preserve whitespaces option apply to the View Engine built libraries too? I kind of expected ViewEngine libraries to have been built already and so the settings of the consuming app would not affect them.

@tscislo
Copy link
Author

@tscislo tscislo commented Mar 5, 2020

So here is the thing:

  1. I have a lib project using Angular 8 where this "preserveWhitespaces" is not set explicitly, but apparently by default it builds those libs with preserveWhitespaces: true. Therefore when I consume it in my app with View Engine it works. What I set for preserveWhitespaces in my app (that consumes lib) is relevant only to my app I guess.
  2. When I forcefully set it per each component in my lib using preserveWhitespaces: true and consumed it in my app with Ivy it also works.

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Mar 5, 2020

Hmm, so this is tricky. It really should be the library author's decision on whether whitespace is preserved in their library's templates.

Also strange is that in V8 the default is still false: https://v8.angular.io/guide/angular-compiler-options#preservewhitespaces. So I am not sure how this is working in v8...

@alxhub
Copy link
Contributor

@alxhub alxhub commented Mar 5, 2020

@petebacondarwin the option is weirdly named - preserveWhitespaces: false is the desired/good state.

In Angular 8 (or Angular 9 with View Engine), libraries all use the application's defaultpreserveWhitespaces setting during the compilation of their templates.

@ngbot ngbot bot removed this from the needsTriage milestone Mar 5, 2020
@ngbot ngbot bot added this to the Backlog milestone Mar 5, 2020
@ngbot ngbot bot removed this from the needsTriage milestone Mar 5, 2020
@ngbot ngbot bot added this to the Backlog milestone Mar 5, 2020
@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Mar 7, 2020

OK so ngcc should accept an option to point to a tsconfig.json file (and programmatically to pass the config object directly) so that it will pull in the Angular compiler specific options.

@tscislo
Copy link
Author

@tscislo tscislo commented Mar 7, 2020

@petebacondarwin well by default ngcc shouldn't change the way the library was compiled either with or without preserveWhitespaces it should know what was set during initial compilation of those libs.

It should however be possible to override this by for example honoring consuming project's tsconfig.json file.

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Mar 7, 2020

As I understand it, in ViewEngine the application actually built the templates for components in libraries; and it used its own configuration to do this.

Since ngcc is supposed to make ViewEngine libraries available to Ivy, and it is also compiling the templates, I think the most "compatible" approach is to pass the application configuration through to ngcc.

@tscislo
Copy link
Author

@tscislo tscislo commented Mar 7, 2020

@petebacondarwin ok so if you're saying that for View Engine Angular compiler in consuming app also builds templates for components in libraries and uses its own config to do that then yes we should pass app configuration through to ngcc for Ivy compilation as well.
Is my understanding correct?

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Mar 7, 2020

That is what @alxhub explained to me :-)

petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Mar 21, 2020
Previously ngcc never preserved whitespaces but this is at odds
with how the ViewEngine compiler works. In ViewEngine, library
templates are recompiled with the current application's tsconfig
settings, which meant that whitespace preservation could be set
in the application tsconfig file.

This commit allows ngcc to use the `preserveWhitespaces` setting
from tsconfig when compiling library templates. One should be aware
that this disallows different projects with different tsconfig settings
to share the same node_modules folder, with regard to whitespace
preservation. But this is already the case in the current ngcc since
this configuration is hard coded right now.

Fixes angular#35871
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Mar 21, 2020
Previously ngcc never preserved whitespaces but this is at odds
with how the ViewEngine compiler works. In ViewEngine, library
templates are recompiled with the current application's tsconfig
settings, which meant that whitespace preservation could be set
in the application tsconfig file.

This commit allows ngcc to use the `preserveWhitespaces` setting
from tsconfig when compiling library templates. One should be aware
that this disallows different projects with different tsconfig settings
to share the same node_modules folder, with regard to whitespace
preservation. But this is already the case in the current ngcc since
this configuration is hard coded right now.

Fixes angular#35871
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Mar 23, 2020
Previously ngcc never preserved whitespaces but this is at odds
with how the ViewEngine compiler works. In ViewEngine, library
templates are recompiled with the current application's tsconfig
settings, which meant that whitespace preservation could be set
in the application tsconfig file.

This commit allows ngcc to use the `preserveWhitespaces` setting
from tsconfig when compiling library templates. One should be aware
that this disallows different projects with different tsconfig settings
to share the same node_modules folder, with regard to whitespace
preservation. But this is already the case in the current ngcc since
this configuration is hard coded right now.

Fixes angular#35871
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Mar 24, 2020
Previously ngcc never preserved whitespaces but this is at odds
with how the ViewEngine compiler works. In ViewEngine, library
templates are recompiled with the current application's tsconfig
settings, which meant that whitespace preservation could be set
in the application tsconfig file.

This commit allows ngcc to use the `preserveWhitespaces` setting
from tsconfig when compiling library templates. One should be aware
that this disallows different projects with different tsconfig settings
to share the same node_modules folder, with regard to whitespace
preservation. But this is already the case in the current ngcc since
this configuration is hard coded right now.

Fixes angular#35871
petebacondarwin added a commit to petebacondarwin/angular that referenced this issue Mar 24, 2020
Previously ngcc never preserved whitespaces but this is at odds
with how the ViewEngine compiler works. In ViewEngine, library
templates are recompiled with the current application's tsconfig
settings, which meant that whitespace preservation could be set
in the application tsconfig file.

This commit allows ngcc to use the `preserveWhitespaces` setting
from tsconfig when compiling library templates. One should be aware
that this disallows different projects with different tsconfig settings
to share the same node_modules folder, with regard to whitespace
preservation. But this is already the case in the current ngcc since
this configuration is hard coded right now.

Fixes angular#35871
@mhevery mhevery closed this in b8e9a30 Mar 24, 2020
mhevery added a commit that referenced this issue Mar 24, 2020
Previously ngcc never preserved whitespaces but this is at odds
with how the ViewEngine compiler works. In ViewEngine, library
templates are recompiled with the current application's tsconfig
settings, which meant that whitespace preservation could be set
in the application tsconfig file.

This commit allows ngcc to use the `preserveWhitespaces` setting
from tsconfig when compiling library templates. One should be aware
that this disallows different projects with different tsconfig settings
to share the same node_modules folder, with regard to whitespace
preservation. But this is already the case in the current ngcc since
this configuration is hard coded right now.

Fixes #35871

PR Close #36189
@tscislo
Copy link
Author

@tscislo tscislo commented Mar 25, 2020

@mhevery @petebacondarwin when (which version) this will be available in angular/compiler-cli?

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Mar 25, 2020

It is in the 9.1.0-rc.2 release: b08168b

@tscislo
Copy link
Author

@tscislo tscislo commented Mar 25, 2020

@petebacondarwin any idea when it will be final?

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Mar 25, 2020

This week, unless we see a serious issue.

@tscislo
Copy link
Author

@tscislo tscislo commented Apr 18, 2020

This works well as expected. Thanks for your help! @petebacondarwin @mhevery

@petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Apr 18, 2020

Great! Thanks for checking in.

@angular-automatic-lock-bot
Copy link

@angular-automatic-lock-bot angular-automatic-lock-bot bot commented May 19, 2020

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

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators May 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants