(
resourceResolver: (url: string) => Promise<string | {text(): Promise<string>; status?: number}>,
)
| 47 | * contents of the resolved URL. Browser's `fetch()` method is a good default implementation. |
| 48 | */ |
| 49 | export async function resolveComponentResources( |
| 50 | resourceResolver: (url: string) => Promise<string | {text(): Promise<string>; status?: number}>, |
| 51 | ): Promise<void> { |
| 52 | const currentQueue = componentResourceResolutionQueue; |
| 53 | componentResourceResolutionQueue = new Map(); |
| 54 | |
| 55 | // Cache so that we don't fetch the same resource more than once. |
| 56 | const urlCache = new Map<string, Promise<string>>(); |
| 57 | |
| 58 | // Helper to dedupe resource fetches |
| 59 | function cachedResourceResolve(url: string): Promise<string> { |
| 60 | const promiseCached = urlCache.get(url); |
| 61 | if (promiseCached) { |
| 62 | return promiseCached; |
| 63 | } |
| 64 | |
| 65 | const promise = resourceResolver(url).then((response) => unwrapResponse(url, response)); |
| 66 | urlCache.set(url, promise); |
| 67 | |
| 68 | return promise; |
| 69 | } |
| 70 | |
| 71 | const resolutionPromises = Array.from(currentQueue).map(async ([type, component]) => { |
| 72 | if (component.styleUrl && component.styleUrls?.length) { |
| 73 | throw new Error( |
| 74 | '@Component cannot define both `styleUrl` and `styleUrls`. ' + |
| 75 | 'Use `styleUrl` if the component has one stylesheet, or `styleUrls` if it has multiple', |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | const componentTasks: Promise<void>[] = []; |
| 80 | |
| 81 | if (component.templateUrl) { |
| 82 | componentTasks.push( |
| 83 | cachedResourceResolve(component.templateUrl).then((template) => { |
| 84 | component.template = template; |
| 85 | }), |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | const styles = |
| 90 | typeof component.styles === 'string' ? [component.styles] : (component.styles ?? []); |
| 91 | component.styles = styles; |
| 92 | |
| 93 | let {styleUrl, styleUrls} = component; |
| 94 | if (styleUrl) { |
| 95 | styleUrls = [styleUrl]; |
| 96 | component.styleUrl = undefined; |
| 97 | } |
| 98 | |
| 99 | if (styleUrls?.length) { |
| 100 | const allFetched = Promise.all(styleUrls.map((url) => cachedResourceResolve(url))).then( |
| 101 | (fetchedStyles) => { |
| 102 | styles.push(...fetchedStyles); |
| 103 | component.styleUrls = undefined; |
| 104 | }, |
| 105 | ); |
| 106 |
no test coverage detected
searching dependent graphs…