Skip to content

Commit 3f22826

Browse files
author
xuanzebin
committed
fix: 修复 vite 编译 h5 时没有注入 history 的问题
1 parent ec39f99 commit 3f22826

File tree

1 file changed

+8
-2
lines changed
  • packages/taro-vite-runner/src/h5

1 file changed

+8
-2
lines changed

packages/taro-vite-runner/src/h5/entry.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,15 @@ export default function (viteCompilerContext: ViteH5CompilerContext): PluginOpti
142142
})
143143
const [, pxtransformOption] = __postcssOption.find(([name]) => name === 'postcss-pxtransform') || []
144144
const pxTransformConfig = pxtransformOption?.config || {}
145+
const routerMode = appConfig?.router?.mode || 'hash'
146+
const historyCreator = routerMode === 'browser' ? 'createBrowserHistory' : routerMode === 'multi' ? 'createMpaHistory' : 'createHashHistory'
147+
const appMountHandler = appConfig.tabBar ? 'handleAppMountWithTabbar' : 'handleAppMount'
148+
145149

146150
return [
147151
setReconciler,
148152
'import { initPxTransform } from "@tarojs/taro"',
149-
`import { ${routerCreator} } from "@tarojs/router"`,
153+
`import { ${routerCreator}, ${historyCreator}, ${appMountHandler} } from "@tarojs/router"`,
150154
`import component from "${app.scriptPath}"`,
151155
'import { window } from "@tarojs/runtime"',
152156
`import { ${creator} } from "${creatorLocation}"`,
@@ -158,7 +162,9 @@ export default function (viteCompilerContext: ViteH5CompilerContext): PluginOpti
158162
routesConfig,
159163
execBeforeCreateWebApp || '',
160164
`var inst = ${creator}(component, ${frameworkArgs})`,
161-
`${routerCreator}(inst, config, ${importFrameworkName})`,
165+
`var history = ${historyCreator}({ window })`,
166+
`${appMountHandler}(config, history)`,
167+
`${routerCreator}(history, inst, config, ${importFrameworkName})`,
162168
'initPxTransform({',
163169
` designWidth: ${pxTransformConfig.designWidth},`,
164170
` deviceRatio: ${JSON.stringify(pxTransformConfig.deviceRatio)},`,

0 commit comments

Comments
 (0)