angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Direct template loading in AngularWebpackPlugin results in empty component templates

Open reduckted opened this issue 10 months ago • 7 comments

Command

build

Is this a regression?

  • [x] Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

18.3.0

Description

A bugfix in webpack 5.96.0 has broken direct template loading. https://github.com/webpack/webpack/issues/19235#issuecomment-2688250063

When using webpack 5.96.0 or higher, if you set the directTemplateLoading option to false in the AngularWebpackPlugin, component templates end up being empty.

Note: I've said that this is a regression even though technically nothing in the Angular CLI has changed, however if you are updating the Angular CLI regularly, you are probably also updating webpack regularly, so you will encounter this bug.

Minimal Reproduction

Start with this repository: https://github.com/reduckted/repro-webpack-template

This is the setup:

  • Contains AppComponent that has a template with the content "hello world".
  • A custom Angular builder is used to modify the webpack configuration and set AngularWebpackPlugin.options.directTemplateLoading to false.
  • A custom loader is to the webpack configuration that replaces all occurrences of "world" with "you" in HTML files.

When this works, the AppComponent's template will end up containing "hello you".

Start by cloning the repository:

git clone https://github.com/reduckted/repro-webpack-template.git
cd repro-webpack-template

Switch to the ng-webpack595 branch (this uses [email protected]). and build:

git checkout ng-webpack595
npm i
npm run build

Open dist/repro-webpack-template/main.js and search for "AppComponent_Template". You will see that it looks like this:

template: function AppComponent_Template(rf, ctx) {
  if (rf & 1) {
    _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵtext"](0, "hello you\n");
  }
},

This works correctly and has replaced "world" with "you".

Now switch to the ng-webpack598 branch (this uses [email protected]). and build:

git checkout ng-webpack598
npm i
npm run build

Open dist/repro-webpack-template/main.js again and search for "AppComponent_Template". You will see that it looks like this:

template: function AppComponent_Template(rf, ctx) {},

The template is completely empty.

Exception or Error


Your Environment

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 19.2.0
Node: 22.13.1
Package Manager: npm 11.1.0
OS: win32 x64

Angular: 19.2.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1902.0
@angular-devkit/build-angular   19.2.0
@angular-devkit/core            19.2.0
@angular-devkit/schematics      19.2.0
@ngtools/webpack                19.2.0
@schematics/angular             19.2.0
rxjs                            7.8.2
typescript                      5.7.3
zone.js                         0.15.0

Anything else relevant?

As @alexander-akait says here: https://github.com/webpack/webpack/issues/19235#issuecomment-2688250063

it was a bug fix in 5.96.0 and unfortunately your code relied on buggy behavior.

Thankfully, the fix is really simple, and @alexander-akait even provided an example of how to fix it. :heart:

This: https://github.com/angular/angular-cli/blob/cb078c276861ce6fb6aaebc589b3c44fcb854c38/packages/ngtools/webpack/src/resource_loader.ts#L129

Needs to change to:

return `data:application/javascript,export { default } from "${filePath}?${NG_COMPONENT_RESOURCE_QUERY}";`;

reduckted avatar Feb 28 '25 11:02 reduckted

Is the suggested change backwards compatible with earlier versions of Webpack? The peer dependency range is currently "webpack": "^5.54.0" and cannot be updated outside of a major release.

clydin avatar Feb 28 '25 14:02 clydin

@clydin The peer dependency range is currently "webpack": "^5.54.0"

Anything less than webpack 5.62.0 doesn't seem to work because the Angular CLI sets experiments.backCompat, but that property doesn't exist before 5.62.0 which causes schema validation to fail and the build hangs.

An unhandled exception occurred: Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
 - configuration.experiments has an unknown property 'backCompat'. These properties are valid:
   object { asset?, asyncWebAssembly?, buildHttp?, cacheUnaffected?, futureDefaults?, layers?, lazyCompilation?, outputModule?, syncWebAssembly?, topLevelAwait? }
   -> Enables/Disables experiments (experimental features with relax SemVer compatibility).

https://github.com/angular/angular-cli/blob/3f2ab3a43de35b0fa5c9613c49bf3ade0031e736/packages/angular_devkit/build_angular/src/tools/webpack/configs/common.ts#L440-L445

Changed in https://github.com/angular/angular-cli/commit/789ddfaeb0fcbc9aab1581384b88c3618e606c4b which even references the release https://github.com/webpack/webpack/releases/tag/v5.62.0

reduckted avatar Mar 01 '25 02:03 reduckted

Is the suggested change backwards compatible with earlier versions of Webpack?

@alexander-akait can hopefully confirm this, but from my testing, using the fix with webpack 5.62.0 works fine.

reduckted avatar Mar 01 '25 03:03 reduckted

This fix will work for any versions, data:application/javascript was supported a lot of time and will work with "webpack": "^5.54.0"

alexander-akait avatar Mar 03 '25 12:03 alexander-akait

@alan-agius4 Any blockers to resolve it or simply no one sent the fix?

alexander-akait avatar Mar 27 '25 00:03 alexander-akait

We've been having the same issue, so the fix is a true blessing for us.

We've written a patch to fix this until it is fixed in an official release, but we would like to know what version will include it.

This issue has been opened for 2 months, and we have a fix, so why is there no PR ? Could our solution have side effects if added to the official package ?

chloedulompont avatar Apr 25 '25 08:04 chloedulompont

@alan-agius4 friendly ping

alexander-akait avatar Apr 25 '25 10:04 alexander-akait