Skip to content

Commit 4d712ef

Browse files
committed
fix: The repaired webpack and vue-cli fail to build when server is false
1 parent 8fd510a commit 4d712ef

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

packages/core/index.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const unplugin = createUnplugin<Options>(
4848
try {
4949
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
5050
// webpack 时 使用 id.includes('vue&type=style') 判断
51+
// webpack dev 和 build 都回进入这里
5152
if (id.endsWith('.vue')
5253
|| (id.includes('vue&type=style') && framework === 'webpack')) {
5354
const { descriptor } = parse(code)
@@ -62,7 +63,8 @@ const unplugin = createUnplugin<Options>(
6263
const variableName = getVariable(descriptor)
6364
vbindVariableList.set(id, matchVariable(vbindVariableListByPath, variableName))
6465

65-
if (!isServer)
66+
// vite、rollup、esbuild 打包生效
67+
if (!isServer && framework === 'webpack' && framework === 'rspack')
6668
mgcStr = injectCssOnBuild(mgcStr, injectCSSContent, descriptor)
6769
}
6870
}
@@ -112,17 +114,17 @@ const unplugin = createUnplugin<Options>(
112114
let mgcStr = new MagicString(code)
113115
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ?
114116
try {
117+
function injectCSSVarsFn(idKey: string) {
118+
const parseRes = parserCompiledSfc(code)
119+
const injectRes = injectCSSVars(vbindVariableList.get(idKey), isScriptSetup, parseRes, mgcStr)
120+
mgcStr = injectRes.mgcStr
121+
injectRes.vbindVariableList && vbindVariableList.set(id, injectRes.vbindVariableList)
122+
isHmring = false
123+
}
124+
115125
// transform in dev
116126
// 'vite' | 'rollup' | 'esbuild'
117127
if (isServer) {
118-
function injectCSSVarsFn(idKey: string) {
119-
const parseRes = parserCompiledSfc(code)
120-
const injectRes = injectCSSVars(vbindVariableList.get(idKey), isScriptSetup, parseRes, mgcStr)
121-
mgcStr = injectRes.mgcStr
122-
injectRes.vbindVariableList && vbindVariableList.set(id, injectRes.vbindVariableList)
123-
isHmring = false
124-
}
125-
126128
if (framework === 'vite'
127129
|| framework === 'rollup'
128130
|| framework === 'esbuild') {
@@ -137,19 +139,26 @@ const unplugin = createUnplugin<Options>(
137139
)
138140
}
139141
}
142+
}
140143

141-
if (framework === 'webpack') {
142-
if (id.includes('vue&type=script')) {
143-
const transId = id.split('?vue&type=script')[0]
144-
injectCSSVarsFn(transId)
145-
}
146-
const cssFMM = CSSFileModuleMap.get(id)
147-
if (cssFMM && cssFMM.sfcPath && cssFMM.sfcPath.size > 0) {
148-
const sfcPathIdList = setTArray(cssFMM.sfcPath)
149-
sfcPathIdList.forEach((v) => {
150-
mgcStr = injectCssOnServer(mgcStr, vbindVariableList.get(v), isHmring)
151-
})
152-
}
144+
// webpack dev 和 build 都回进入这里
145+
if (framework === 'webpack') {
146+
const { _module } = this
147+
148+
// 判断是否是热更新引起的执行
149+
const isHotUpdate = _module && _module.hot && _module.hot.data
150+
console.log(isHotUpdate)
151+
152+
if (id.includes('vue&type=script')) {
153+
const transId = id.split('?vue&type=script')[0]
154+
injectCSSVarsFn(transId)
155+
}
156+
const cssFMM = CSSFileModuleMap.get(id)
157+
if (cssFMM && cssFMM.sfcPath && cssFMM.sfcPath.size > 0) {
158+
const sfcPathIdList = setTArray(cssFMM.sfcPath)
159+
sfcPathIdList.forEach((v) => {
160+
mgcStr = injectCssOnServer(mgcStr, vbindVariableList.get(v), isHmring)
161+
})
153162
}
154163
}
155164

play/webpack/vue.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = defineConfig({
1111
alias: {
1212
'@': resolve(__dirname, './src'),
1313
},
14-
server: true,
14+
server: false,
1515
}),
1616
],
1717
},

0 commit comments

Comments
 (0)