diff options
Diffstat (limited to 'src/optimize/styles.ts')
-rw-r--r-- | src/optimize/styles.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/optimize/styles.ts b/src/optimize/styles.ts new file mode 100644 index 000000000..b654ca7d1 --- /dev/null +++ b/src/optimize/styles.ts @@ -0,0 +1,51 @@ +import type { Ast, TemplateNode } from '../compiler/interfaces'; +import type { Optimizer } from './types' +import { transformStyle } from '../style.js'; + +export default function({ filename, fileID }: { filename: string, fileID: string }): Optimizer { + const classNames: Set<string> = new Set(); + let stylesPromises: any[] = []; + + return { + visitors: { + html: { + Element: { + enter(node) { + for(let attr of node.attributes) { + if(attr.name === 'class') { + for(let value of attr.value) { + if(value.type === 'Text') { + const classes = value.data.split(' '); + for(const className in classes) { + classNames.add(className); + } + } + } + } + } + } + } + }, + css: { + Style: { + enter(node: TemplateNode) { + const code = node.content.styles; + const typeAttr = node.attributes && node.attributes.find(({ name }: { name: string }) => name === 'type'); + stylesPromises.push( + transformStyle(code, { + type: (typeAttr.value[0] && typeAttr.value[0].raw) || undefined, + classNames, + filename, + fileID, + }) + ); // TODO: styles needs to go in <head> + } + } + } + }, + async finalize() { + const styles = await Promise.all(stylesPromises); // TODO: clean this up + console.log({ styles }); + } + }; +}
\ No newline at end of file |