Skip to content

Commit a6bedd4

Browse files
committed
feat: more complete webpack, vue-cli support
1 parent 8e087eb commit a6bedd4

File tree

10 files changed

+38
-26
lines changed

10 files changed

+38
-26
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@
5959
"lint:fix": "eslint --fix ./ --ext .vue,.js,.ts,.jsx,.tsx,.json ",
6060
"dev": "pnpm run --filter @unplugin-vue-cssvars/build dev",
6161
"build": "pnpm run clean && pnpm run --filter @unplugin-vue-cssvars/build build",
62+
"play:vite:server": "pnpm run --filter @unplugin-vue-cssvars/play-vite server",
6263
"play:vite:build": "pnpm run --filter @unplugin-vue-cssvars/play-vite build",
63-
"play:vite:server": "pnpm run --filter @unplugin-vue-cssvars/play-vite dev",
6464
"play:vite:preview": "pnpm run --filter @unplugin-vue-cssvars/play-vite preview",
65+
"play:webpack:server": "pnpm run --filter @unplugin-vue-cssvars/play-webpack server",
6566
"play:webpack:build": "pnpm run --filter @unplugin-vue-cssvars/play-webpack build",
66-
"play:webpack:server": "pnpm run --filter @unplugin-vue-cssvars/play-webpack dev",
67+
"play:webpack:preview": "pnpm run --filter @unplugin-vue-cssvars/play-webpack preview",
6768
"release": "bumpp package.json --commit --push --tag",
6869
"clean": "rimraf dist",
6970
"prepare": "npx simple-git-hooks",

packages/core/index.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ const unplugin = createUnplugin<Options>(
4747
let mgcStr = new MagicString(code)
4848
try {
4949
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
50-
if (id.endsWith('.vue')) {
50+
// webpack 时 使用 id.includes('vue&type=style') 判断
51+
if (id.endsWith('.vue')
52+
|| (id.includes('vue&type=style') && framework === 'webpack')) {
5153
const { descriptor } = parse(code)
5254
const lang = descriptor?.script?.lang ?? 'js'
5355
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
@@ -60,12 +62,11 @@ const unplugin = createUnplugin<Options>(
6062
const variableName = getVariable(descriptor)
6163
vbindVariableList.set(id, matchVariable(vbindVariableListByPath, variableName))
6264

63-
// TODO: webpack
64-
// 'vite' | 'rollup' | 'esbuild'
65-
if (!isServer && framework !== 'webpack' && framework !== 'rspack')
65+
if (!isServer)
6666
mgcStr = injectCssOnBuild(mgcStr, injectCSSContent, descriptor)
6767
}
6868
}
69+
6970
return {
7071
code: mgcStr.toString(),
7172
get map() {
@@ -117,7 +118,7 @@ const unplugin = createUnplugin<Options>(
117118
function injectCSSVarsFn(idKey: string) {
118119
const parseRes = parserCompiledSfc(code)
119120
const injectRes = injectCSSVars(vbindVariableList.get(idKey), isScriptSetup, parseRes, mgcStr)
120-
mgcStr = injectRes.mgcStr // .overwrite(0, mgcStr.length(), injectRes.code)
121+
mgcStr = injectRes.mgcStr
121122
injectRes.vbindVariableList && vbindVariableList.set(id, injectRes.vbindVariableList)
122123
isHmring = false
123124
}
@@ -137,11 +138,9 @@ const unplugin = createUnplugin<Options>(
137138
}
138139
}
139140

140-
// TODO webpack
141141
if (framework === 'webpack') {
142142
if (id.includes('vue&type=script')) {
143143
const transId = id.split('?vue&type=script')[0]
144-
// TODO 重复注入了
145144
injectCSSVarsFn(transId)
146145
}
147146
const cssFMM = CSSFileModuleMap.get(id)

packages/core/inject/__test__/__snapshots__/inject-cssvars.spec.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`inject-cssvars > createCSSVarsObjCode > hasUseCssVars is false 1`] = `
3+
exports[`inject-cssvars > injectUseCssVarsOption > hasUseCssVars is false 1`] = `
44
"import { useCssVars as _useCssVars } from \\"vue\\"
55
const __default__ = {};const __injectCSSVars__ = () => {}
66
@@ -12,7 +12,7 @@ const __default__ = {};const __injectCSSVars__ = () => {}
1212
function _sfc_render(){}"
1313
`;
1414

15-
exports[`inject-cssvars > createCSSVarsObjCode > hasUseCssVars is true 1`] = `
15+
exports[`inject-cssvars > injectUseCssVarsOption > hasUseCssVars is true 1`] = `
1616
"import { useCssVars as _useCssVars } from 'vue'
1717
const __injectCSSVars__ = () => {
1818
_useCssVars(_ctx => ({

packages/core/inject/__test__/inject-cssvars.spec.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,15 @@ describe('inject-cssvars', () => {
159159
expect(result).toContain('color.value')
160160
expect(result).toContain('"color":')
161161
})
162+
163+
test('avoid repeated injections', () => {
164+
const mockCode = '\n"color": color.value,'
165+
const vbindVariableList = [{ value: 'color', has: true, hash: 'color', isRef: true }]
166+
const result = createCSSVarsObjCode(vbindVariableList as any, true, new MagicString(mockCode))
167+
expect(result).not.toBeTruthy()
168+
})
162169
})
163-
describe('createCSSVarsObjCode', () => {
170+
describe('injectUseCssVarsOption', () => {
164171
test('hasUseCssVars is false', () => {
165172
const mockContent = 'const _sfc_main = {};function _sfc_render(){}'
166173
const mgcStr = new MagicString(mockContent)

packages/core/inject/inject-css.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function injectCssOnServer(
1616
if (!vbVar.hash && isHmring)
1717
vbVar.hash = hash(vbVar.value + vbVar.has)
1818

19-
mgcStr = mgcStr.replaceAll(`v-bind-m(${vbVar.value})`, `var(--${vbVar.hash})`)
19+
vbVar.hash && (mgcStr = mgcStr.replaceAll(`v-bind-m(${vbVar.value})`, `var(--${vbVar.hash})`))
2020
})
2121
return mgcStr
2222
}
@@ -38,7 +38,7 @@ export function injectCssOnBuild(
3838
const scoped = value.scoped ? 'scoped' : ''
3939
resCode = `<style lang="${lang}" ${scoped}> ${injectCssCode}\n${transformInjectCSS(value.content, parseImports(value.content).imports)} </style>`
4040
})
41-
mgcStr = removeStyleTagsAndContent(mgcStr)
41+
resCode && (mgcStr = removeStyleTagsAndContent(mgcStr))
4242
return mgcStr.prependRight(mgcStr.length(), resCode)
4343
}
4444

packages/core/inject/inject-cssvars.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function injectCSSVarsOnServer(
3232
) {
3333
let resMgcStr = mgcStr
3434
const hasUseCssVars = parserRes.hasCSSVars
35-
const cssvarsObjectCode = createCSSVarsObjCode(vbindVariableList, isScriptSetup)
35+
const cssvarsObjectCode = createCSSVarsObjCode(vbindVariableList, isScriptSetup, resMgcStr)
3636
// 1
3737
if (isScriptSetup) {
3838
// 1.1
@@ -115,6 +115,7 @@ export function injectUseCssVarsOption(
115115
export function createCSSVarsObjCode(
116116
vbindVariableList: TMatchVariable,
117117
isScriptSetup: boolean,
118+
mgcStr?: MagicStringBase,
118119
) {
119120
let resCode = ''
120121
vbindVariableList.forEach((vbVar) => {
@@ -131,8 +132,13 @@ export function createCSSVarsObjCode(
131132
// ref 用.value
132133
varStr = vbVar.isRef ? `${vbVar.value}.value` : varStr
133134
}
134-
resCode = `"${hashVal}": ${varStr},\n ${resCode}`
135+
resCode = `\n "${hashVal}": ${varStr},${resCode}`
135136
})
137+
138+
// 避免 webpack 重复注入
139+
if (mgcStr && mgcStr.toString().includes(resCode))
140+
return ''
141+
136142
return resCode
137143
}
138144

packages/core/parser/__test__/parser-compiled-sfc.spec.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,7 @@ describe('parserCompiledSfc', () => {
181181
test('should parse setupBodyNode when an Identifier named setup is encountered', () => {
182182
const code = `
183183
const comp = {
184-
setup() {
185-
console.log('Setting up...');
186-
}
184+
setup() {}
187185
}
188186
`
189187
const res = parserCompiledSfc(code)

play/vite/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "module",
55
"version": "0.0.0",
66
"scripts": {
7-
"dev": "vite --host",
7+
"server": "vite --host",
88
"build": "vite build",
99
"preview": "vite preview --host"
1010
},

play/webpack/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"private": true,
44
"version": "0.1.0",
55
"scripts": {
6-
"dev": "vue-cli-service serve",
7-
"build": "vue-cli-service build"
6+
"server": "vue-cli-service serve",
7+
"build": "vue-cli-service build",
8+
"preview": "cd dist && npx http-server -p 8081"
89
},
910

1011
"devDependencies": {

play/webpack/src/App.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<script setup lang="ts">
2-
import { reactive, ref } from 'vue'
3-
import Comp from '../../comp.vue'
4-
const color = ref('blue')
5-
const appAsd = () => 'red'
2+
import { ref } from 'vue'
3+
const color = ref('red')
4+
const appAsd = () => 'green'
65
const fooColor = appAsd()
76
</script>
87

@@ -16,5 +15,6 @@ const fooColor = appAsd()
1615
@import '@/assets/css/foo.css';
1716
#foo{
1817
background: v-bind(fooColor);
18+
width: 300px;
1919
}
2020
</style>

0 commit comments

Comments
 (0)