Skip to content

Commit e9a3e03

Browse files
committed
feat: use ast to impl css inject on server
1 parent d61dbe1 commit e9a3e03

File tree

6 files changed

+545
-273
lines changed

6 files changed

+545
-273
lines changed

packages/core/index.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import MagicString from 'magic-string'
77
import { preProcessCSS } from './runtime/pre-process-css'
88
import { getVBindVariableListByPath } from './runtime/process-css'
99
import { initOption } from './option'
10-
import { getVariable, matchVariable } from './parser'
10+
import { getVariable, matchVariable, parserCompiledSfc } from './parser'
1111
import {
1212
injectCSSVars,
1313
injectCssOnBuild,
@@ -59,6 +59,7 @@ const unplugin = createUnplugin<Options>(
5959
} = getVBindVariableListByPath(descriptor, id, CSSFileModuleMap, isServer, userOptions.alias)
6060
const variableName = getVariable(descriptor)
6161
vbindVariableList.set(id, matchVariable(vbindVariableListByPath, variableName))
62+
6263
// TODO: webpack
6364
// 'vite' | 'rollup' | 'esbuild'
6465
if (!isServer && framework !== 'webpack' && framework !== 'rspack')
@@ -82,11 +83,13 @@ const unplugin = createUnplugin<Options>(
8283
vite: {
8384
// Vite plugin
8485
configResolved(config: ResolvedConfig) {
86+
// TODO
8587
if (userOptions.server !== undefined)
8688
isServer = userOptions.server
8789
else
8890
isServer = config.command === 'serve'
8991
},
92+
// TODO
9093
handleHotUpdate(hmr: HmrContext) {
9194
if (SUPPORT_FILE_REG.test(hmr.file)) {
9295
isHmring = true
@@ -114,15 +117,16 @@ const unplugin = createUnplugin<Options>(
114117
// 'vite' | 'rollup' | 'esbuild'
115118
if (isServer) {
116119
function injectCSSVarsFn(idKey: string) {
117-
const injectRes = injectCSSVars(code, vbindVariableList.get(idKey), isScriptSetup, framework)
118-
mgcStr = mgcStr.overwrite(0, mgcStr.length(), injectRes.code)
120+
const parseRes = parserCompiledSfc(code)
121+
const injectRes = injectCSSVars(vbindVariableList.get(idKey), isScriptSetup, parseRes, mgcStr)
122+
mgcStr = injectRes.mgcStr // .overwrite(0, mgcStr.length(), injectRes.code)
119123
injectRes.vbindVariableList && vbindVariableList.set(id, injectRes.vbindVariableList)
120124
isHmring = false
121125
}
122126

123-
if (framework === 'vite' ||
124-
framework === 'rollup' ||
125-
framework === 'esbuild') {
127+
if (framework === 'vite'
128+
|| framework === 'rollup'
129+
|| framework === 'esbuild') {
126130
if (id.endsWith('.vue'))
127131
injectCSSVarsFn(id)
128132

@@ -135,10 +139,11 @@ const unplugin = createUnplugin<Options>(
135139
}
136140
}
137141

142+
// TODO webpack
138143
if (framework === 'webpack') {
139144
if (id.includes('vue&type=script')) {
140145
const transId = id.split('?vue&type=script')[0]
141-
// todo 重复注入了
146+
// TODO 重复注入了
142147
injectCSSVarsFn(transId)
143148
}
144149
const cssFMM = CSSFileModuleMap.get(id)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`inject-cssvars > createCSSVarsObjCode > hasUseCssVars is false 1`] = `
4+
"import { useCssVars as _useCssVars } from \\"vue\\"
5+
const __default__ = {};const __injectCSSVars__ = () => {}
6+
7+
const __setup__ = __default__.setup
8+
__default__.setup = __setup__
9+
? (props, ctx) => { __injectCSSVars__(); return __setup__(props, ctx) }
10+
: __injectCSSVars__
11+
const _sfc_main = __default__
12+
function _sfc_render(){}"
13+
`;
14+
15+
exports[`inject-cssvars > createCSSVarsObjCode > hasUseCssVars is true 1`] = `
16+
"import { useCssVars as _useCssVars } from 'vue'
17+
const __injectCSSVars__ = () => {
18+
_useCssVars(_ctx => ({
19+
\\"color\\": (_ctx.color),
20+
21+
\\"229090c3-sassColor\\": (_ctx.sassColor)
22+
}))}
23+
const __setup__ = __default__.setup
24+
__default__.setup = __setup__
25+
? (props, ctx) => { __injectCSSVars__();return __setup__(props, ctx) }
26+
: __injectCSSVars__
27+
28+
const _sfc_main = __default__"
29+
`;
30+
31+
exports[`inject-cssvars > injectUseCssVarsSetup > hasUseCssVars is false 1`] = `
32+
"import { useCssVars as _useCssVars } from \\"vue\\"
33+
function setup() {
34+
_useCssVars((_ctx) => ({
35+
\\"1439c43b\\": color.value,
36+
}));}"
37+
`;
38+
39+
exports[`inject-cssvars > injectUseCssVarsSetup > hasUseCssVars is true 1`] = `
40+
"function setup() { _useCssVars((_ctx) => ({\\"1439c43b\\": color.value,
41+
\\"229090c3-sassColor\\": sassColor.value
42+
})); }"
43+
`;

0 commit comments

Comments
 (0)