niucloud/uni-app/scripts/vite-plugin-uni-full-global.cjs
wangchen14709853322 5ed9a0ba81 v2.0-beta-20260626
v2框架公测版测试流程请看v2.0-beta.md
2026-07-01 12:25:30 +08:00

34 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* H5 分构建 Coretree-shaking 模式下 window.uni 仅为 {}addon 动态页无法调用 navigateTo 等 API。
* 将 pages-json 生成的 window.uni = {} 替换为完整 uni 对象挂载。
*/
function createUniFullGlobalPlugin() {
const emptyGlobalRe =
/import \{ upx2px, getApp \} from '@dcloudio\/uni-h5';(\w+)\.uni = \{\};(\w+)\.wx = \{\};(\w+)\.rpx2px = upx2px/
const fullGlobal = `import { uni, upx2px, getApp, getCurrentPages, UniServiceJSBridge, UniViewJSBridge } from '@dcloudio/uni-h5'
$1.getApp = getApp
$1.getCurrentPages = getCurrentPages
$1.wx = uni
$1.uni = uni
$1.UniViewJSBridge = UniViewJSBridge
$1.UniServiceJSBridge = UniServiceJSBridge
$1.rpx2px = upx2px
$1.__setupPage = (com)=>setupPage(com)`
return {
name: 'uni-full-global',
enforce: 'post',
transform(code) {
if (!code.includes('.uni = {}') || !emptyGlobalRe.test(code)) return null
console.log('[uni-full-global] mount full uni on window (split build)')
return {
code: code.replace(emptyGlobalRe, fullGlobal),
map: null
}
}
}
}
module.exports = { createUniFullGlobalPlugin }