refactor(core): do not remove `templateUrl` when resolving (#28055)
When we resolve a component `templateUrl` we copy the contents of the resolved template file into the `template` property. Previously we would then remove the `templateUrl` to indicate that the component has been resolved. But this meant that we no longer had access to the URL of the original template file. This is essential for diagnostics messages about the template compilation. Now the existence of the `template` property overrides the existence of `templateUrl`, which allows us to keep the `templateUrl` property. PR Close #28055
This commit is contained in:
parent
8d15dd8b70
commit
8c3f1717a8
|
@ -6,7 +6,7 @@
|
|||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import { Component } from './directives';
|
||||
import {Component} from './directives';
|
||||
|
||||
|
||||
/**
|
||||
|
@ -42,7 +42,7 @@ import { Component } from './directives';
|
|||
* contents of the resolved URL. Browser's `fetch()` method is a good default implementation.
|
||||
*/
|
||||
export function resolveComponentResources(
|
||||
resourceResolver: (url: string) => (Promise<string | { text(): Promise<string> }>)): Promise<null> {
|
||||
resourceResolver: (url: string) => (Promise<string|{text(): Promise<string>}>)): Promise<null> {
|
||||
// Store all promises which are fetching the resources.
|
||||
const urlFetches: Promise<string>[] = [];
|
||||
|
||||
|
@ -62,7 +62,6 @@ export function resolveComponentResources(
|
|||
if (component.templateUrl) {
|
||||
cachedResourceResolve(component.templateUrl).then((template) => {
|
||||
component.template = template;
|
||||
component.templateUrl = undefined;
|
||||
});
|
||||
}
|
||||
const styleUrls = component.styleUrls;
|
||||
|
@ -92,12 +91,14 @@ export function maybeQueueResolutionOfComponentResources(metadata: Component) {
|
|||
}
|
||||
|
||||
export function componentNeedsResolution(component: Component): boolean {
|
||||
return !!(component.templateUrl || component.styleUrls && component.styleUrls.length);
|
||||
return !!(
|
||||
(component.templateUrl && !component.template) ||
|
||||
component.styleUrls && component.styleUrls.length);
|
||||
}
|
||||
export function clearResolutionOfComponentResourcesQueue() {
|
||||
componentResourceResolutionQueue.clear();
|
||||
}
|
||||
|
||||
function unwrapResponse(response: string | { text(): Promise<string> }): string | Promise<string> {
|
||||
function unwrapResponse(response: string | {text(): Promise<string>}): string|Promise<string> {
|
||||
return typeof response == 'string' ? response : response.text();
|
||||
}
|
||||
|
|
|
@ -64,7 +64,6 @@ Did you run and wait for 'resolveComponentResources()'?`.trim());
|
|||
compileComponent(MyComponent, metadata);
|
||||
await resolveComponentResources(testResolver);
|
||||
expect(MyComponent.ngComponentDef).toBeDefined();
|
||||
expect(metadata.templateUrl).toBe(undefined);
|
||||
expect(metadata.template).toBe('content');
|
||||
expect(resourceFetchCount).toBe(1);
|
||||
});
|
||||
|
@ -127,7 +126,6 @@ Did you run and wait for 'resolveComponentResources()'?`.trim());
|
|||
compileComponent(MyComponent, metadata);
|
||||
await resolveComponentResources(fetch);
|
||||
expect(MyComponent.ngComponentDef).toBeDefined();
|
||||
expect(metadata.templateUrl).toBe(undefined);
|
||||
expect(metadata.template).toBe('response for test://content');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue