forked from analogjs/analog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vite-plugin-angular): process/transform styles before Angular com…
…pilation (analogjs#167) Closes analogjs#142
- Loading branch information
1 parent
a480a59
commit 5d8c435
Showing
3 changed files
with
77 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 0 additions & 78 deletions
78
packages/vite-plugin-angular/src/lib/component-assets-plugin.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { CompilerHost } from '@angular/compiler-cli'; | ||
import { normalizePath } from '@ngtools/webpack/src/ivy/paths'; | ||
import * as ts from 'typescript'; | ||
|
||
export function augmentHostWithResources( | ||
host: ts.CompilerHost, | ||
transform: ( | ||
code: string, | ||
id: string, | ||
options?: { ssr?: boolean } | ||
) => ReturnType<any> | null, | ||
options: { | ||
inlineStylesExtension?: string; | ||
} = {} | ||
) { | ||
const resourceHost = host as CompilerHost; | ||
|
||
resourceHost.readResource = function (fileName: string) { | ||
const filePath = normalizePath(fileName); | ||
|
||
const content = (this as any).readFile(filePath); | ||
if (content === undefined) { | ||
throw new Error('Unable to locate component resource: ' + fileName); | ||
} | ||
|
||
return content; | ||
}; | ||
|
||
resourceHost.transformResource = async function (data, context) { | ||
// Only style resources are supported currently | ||
if (context.type !== 'style') { | ||
return null; | ||
} | ||
|
||
if (options.inlineStylesExtension) { | ||
// Resource file only exists for external stylesheets | ||
const filename = | ||
context.resourceFile ?? | ||
`${context.containingFile.replace( | ||
/\.ts$/, | ||
`.${options?.inlineStylesExtension}` | ||
)}`; | ||
|
||
let stylesheetResult; | ||
|
||
try { | ||
stylesheetResult = await transform(data, `${filename}?direct`); | ||
} catch (e) { | ||
console.error(`${e}`); | ||
} | ||
|
||
return { content: stylesheetResult?.code || '' }; | ||
} | ||
|
||
return null; | ||
}; | ||
} |