Skip to content

fix(component): apply the project-wide preserveWhitespaces option - #637

Open
edbzn wants to merge 1 commit into
voidzero-dev:mainfrom
edbzn:fix/global-preserve-whitespaces
Open

edbzn wants to merge 1 commit into
voidzero-dev:mainfrom
edbzn:fix/global-preserve-whitespaces

Conversation

@edbzn

@edbzn edbzn commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #631.

Problem

TransformOptions.preserveWhitespaces (ngc's angularCompilerOptions.preserveWhitespaces) was ignored by transformAngularFile and compileComponentHmrUpdate: compile_component_full parsed the template with the decorator's preserveWhitespaces only, so a project-wide true still collapsed whitespace. The Vite plugin had no way to set it. Found with Angular's ngtsc_spec.ts: "should take preserveWhitespaces config option into account".

Fix

Port ngtsc's parseTemplateDeclaration (compiler-cli/src/ngtsc/annotations/component/src/resources.ts): start from the project default and let the decorator override it.

  • ComponentMetadata.preserve_whitespaces is now Option<bool> (None when the decorator does not set it).
  • compile_component_full resolves decorator → options.preserve_whitespaces → false, and writes the result back so partial declarations (ɵɵngDeclareComponent) carry it as well. HMR updates go through the same function.
  • Vite plugin: new preserveWhitespaces option, passed to the transform (and so to HMR updates); documented in the README. The plugin does not read angularCompilerOptions from the tsconfig, so an explicit option is the smallest way in.
  • The template-only APIs (compileTemplate, deprecated compileForHmr) already used the option as the template's value; unchanged.

Checks

  • New tests/preserve_whitespaces_test.rs, porting the three ngtsc cases (default, project option, decorator overrides the option both ways) plus the partial declaration.
  • hmr_component_update_test::keeps_whitespace_the_project_option_preserves: the HMR update keeps whitespace and matches the transform's template.
  • Both new test files fail without the fix.
  • cargo test -p oxc_angular_compiler --all-features: 3522 passed, 0 failed.
  • cargo check -p oxc_angular_compiler_napi and tsc --noEmit on the Vite plugin pass.

🤖 Generated with Claude Code

`TransformOptions.preserveWhitespaces` is now the default for components
whose decorator does not set it, as in ngtsc's `parseTemplateDeclaration`.
The Vite plugin gets a `preserveWhitespaces` option, used for HMR updates too.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Project-wide preserveWhitespaces option is ignored

1 participant