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

Absolute or Relative templateUrls for Angular Components aot

Open JBoothUA opened this issue 9 years ago • 4 comments

angular/cli: 1.5.3 angular-devkit/build-optimizer: 0.0.33 angular-devkit/core: 0.0.20 angular-devkit/schematics: 0.0.36 ngtools/json-schema: 1.1.0 ngtools/webpack: 1.8.3 schematics/angular: 0.1.5 typescript: 2.6.1 webpack-bundle-analyzer: 2.9.1 webpack-merge: 4.1.1 webpack: 3.8.1 WINDOWS 10

I've been stuck trying to get AOT working with my Webpack Unit Tests. (ngtools/webpack). OR at least along-side my unit tests....

Can anyone help me at all? Or have any examples?

The main issue I'm seeing seems to be with the component's templates. The component tests all say 'Failed to load xxx.html'. During the karma unit tests..

I am trying to unit test with relative (aot) urls in the source code. but what's the trick here without 2 urls in the source code? (absolute urls for jit unit tests and relative urls for aot production)??

What am I missing?

I am using webpack with aot NOT angular-cli for a use project and this is a blocker..

JBoothUA avatar Nov 25 '17 06:11 JBoothUA

I don't have any example for you to follow but the loader works in webpack, so if you're using webpack in karma then it should work too.

However, you mention AOT a fair few times but there is not support for unit tests in AOT yet (see https://github.com/angular/angular-cli/pull/8007 for info).

We only replace template paths with require calls for JIT. So if you're trying to use AOT in unit tests I'd expect you to get those errors, because unit tests are not being ran with AOT and when a component is loaded, it will fail to load the template.

If you can put up a repro using JIT where unit tests are not running correctly, I'm happy to take a look.

filipesilva avatar Nov 27 '17 09:11 filipesilva

Thank you so much! I think you can hopefully get me on the right track with 1 more question.

I unfortunately was coming to the same conclusion that karma unit tests do not support AoT, but I'm still confused on the best practice here..

I had to change my source code to have relative urls everywhere for each component's style and template url. that's source code changes that were required to run the app with AoT (as far as I understood).

so do I now need another set of source code with absolute paths to run the unit tests? Or how do I run with relative template urls for the app and absolute template urls for unit testing, hopefully I dont need to have both urls in the source code?

as far as repro steps, I think you have foreseen my problem. i am trying to unit test with relative (aot) urls in the source code. but what's the trick here without 2 urls in the source code? (absolute urls for jit unit tests and relative urls for aot production)

JBoothUA avatar Nov 27 '17 17:11 JBoothUA

i needed angular2-template-loader to pull this off

JBoothUA avatar Dec 01 '17 23:12 JBoothUA

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.