-
-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(language-core): split inline css codegen into separate plugin
- Loading branch information
1 parent
ff4b7ea
commit fa4a4ba
Showing
8 changed files
with
89 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as CompilerDOM from '@vue/compiler-dom'; | ||
import { forEachElementNode } from './template'; | ||
import { enableAllFeatures } from './utils'; | ||
import type { Code } from '../types'; | ||
|
||
export function* generate(templateAst: NonNullable<CompilerDOM.RootNode>): Generator<Code> { | ||
for (const node of forEachElementNode(templateAst)) { | ||
for (const prop of node.props) { | ||
if ( | ||
prop.type === CompilerDOM.NodeTypes.DIRECTIVE | ||
&& prop.name === 'bind' | ||
&& prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION | ||
&& prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION | ||
&& prop.arg.content === 'style' | ||
&& prop.exp.constType === CompilerDOM.ConstantTypes.CAN_STRINGIFY | ||
) { | ||
const endCrt = prop.arg.loc.source[prop.arg.loc.source.length - 1]; // " | ' | ||
const start = prop.arg.loc.source.indexOf(endCrt) + 1; | ||
const end = prop.arg.loc.source.lastIndexOf(endCrt); | ||
const content = prop.arg.loc.source.substring(start, end); | ||
|
||
yield `x { `; | ||
yield [ | ||
content, | ||
'template', | ||
prop.arg.loc.start.offset + start, | ||
enableAllFeatures({ | ||
format: false, | ||
structure: false, | ||
}), | ||
]; | ||
yield ` }\n`; | ||
} | ||
} | ||
} | ||
} |
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
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
28 changes: 28 additions & 0 deletions
28
packages/language-core/lib/plugins/vue-template-inline-css.ts
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,28 @@ | ||
import { generate as generateInlineCss } from '../generators/inlineCss'; | ||
import type { VueLanguagePlugin } from '../types'; | ||
|
||
const plugin: VueLanguagePlugin = () => { | ||
|
||
return { | ||
|
||
version: 2, | ||
|
||
getEmbeddedCodes(_fileName, sfc) { | ||
if (sfc.template?.ast) { | ||
return [{ id: 'template_inline_css', lang: 'css' }]; | ||
} | ||
return []; | ||
}, | ||
|
||
resolveEmbeddedCode(_fileName, sfc, embeddedFile) { | ||
if (embeddedFile.id === 'template_inline_css') { | ||
embeddedFile.parentCodeId = 'template'; | ||
if (sfc.template?.ast) { | ||
embeddedFile.content.push(...generateInlineCss(sfc.template.ast)); | ||
} | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
export default plugin; |
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
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
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
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