(
host: ts.CompilerHost,
resourceLoader: WebpackResourceLoader,
options: {
directTemplateLoading?: boolean;
inlineStyleFileExtension?: string;
} = {},
)
| 15 | import { normalizePath } from './paths'; |
| 16 | |
| 17 | export function augmentHostWithResources( |
| 18 | host: ts.CompilerHost, |
| 19 | resourceLoader: WebpackResourceLoader, |
| 20 | options: { |
| 21 | directTemplateLoading?: boolean; |
| 22 | inlineStyleFileExtension?: string; |
| 23 | } = {}, |
| 24 | ): void { |
| 25 | const resourceHost = host as CompilerHost; |
| 26 | |
| 27 | resourceHost.readResource = function (fileName: string) { |
| 28 | const filePath = normalizePath(fileName); |
| 29 | |
| 30 | if ( |
| 31 | options.directTemplateLoading && |
| 32 | (filePath.endsWith('.html') || filePath.endsWith('.svg')) |
| 33 | ) { |
| 34 | const content = this.readFile(filePath); |
| 35 | if (content === undefined) { |
| 36 | throw new Error('Unable to locate component resource: ' + fileName); |
| 37 | } |
| 38 | |
| 39 | resourceLoader.setAffectedResources(filePath, [filePath]); |
| 40 | |
| 41 | return Promise.resolve(content); |
| 42 | } else { |
| 43 | return resourceLoader.get(filePath); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | resourceHost.resourceNameToFileName = function (resourceName: string, containingFile: string) { |
| 48 | return path.join(path.dirname(containingFile), resourceName); |
| 49 | }; |
| 50 | |
| 51 | resourceHost.getModifiedResourceFiles = function () { |
| 52 | return resourceLoader.getModifiedResourceFiles(); |
| 53 | }; |
| 54 | |
| 55 | resourceHost.transformResource = async function (data, context) { |
| 56 | // Only inline style resources are supported currently |
| 57 | if (context.resourceFile || context.type !== 'style') { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | if (options.inlineStyleFileExtension) { |
| 62 | const content = await resourceLoader.process( |
| 63 | data, |
| 64 | options.inlineStyleFileExtension, |
| 65 | context.type, |
| 66 | context.containingFile, |
| 67 | ); |
| 68 | |
| 69 | return { content }; |
| 70 | } |
| 71 | |
| 72 | return null; |
| 73 | }; |
| 74 | } |
no test coverage detected