Files
lucky_shop/main.js
ZF sun 50a1c1c695 feat(i18n): 实现remote-only打包模式,优化首屏加载和产物体积
1. 新增remote-only模式下的空语言包stub
2. 新增缓存有效期配置并对齐后端Cache::set 600s
3. 重构语言包加载逻辑,适配remote-only模式
4. 添加webpack替换插件,排除本地lang目录减少产物体积
5. 优化Vue挂载时机,等待语言包就绪后再渲染首屏
6. 改进缓存策略,新增内存缓存TTL校验和语种缓存淘汰
7. 修复语言包缓存key生成逻辑,支持uniacid和appType隔离
2026-06-30 14:04:52 +08:00

85 lines
2.2 KiB
JavaScript

// #ifdef H5
import './common/js/pc'
// #endif
import Vue from 'vue'
import App from './App'
import store from './store'
import Util from './common/js/util.js'
import Http from './common/js/http.js'
import Lang from './common/js/lang.js'
import Config from './common/js/config.js'
import EventBus from './common/js/event-bus.js'
import DomEventBridge from './common/js/dom-event-bridge.js'
import globalConfig from './common/js/golbalConfig.js';
import {
uniStorage
} from './common/js/storage.js'
import configExternal, { langConfig } from './common/js/config-external.js'
Vue.prototype.$store = store //挂在vue
Vue.config.productionTip = false
Vue.prototype.$util = Util;
Vue.prototype.$api = Http;
Vue.prototype.$langConfig = Lang; //语言包对象
Vue.prototype.$lang = Lang.lang; //解析语言包
Vue.prototype.$config = Config;
// #ifdef H5
EventBus.setDomBridge(DomEventBridge)
// #endif
Vue.prototype.$eventBus = EventBus;
Vue.mixin(globalConfig);
// 远端 i18n 翻译热更新:复用项目原有"componentRefresh"机制(同 diy.js 下拉刷新)
// lang.js 在翻译覆写更新时 commit('setComponentRefresh'),
// golbalConfig.js 已把 componentRefresh 注册为全局 computed,
// 在此 watch 它并 $forceUpdate,触发所有页面内 {{$lang('xxx')}} 重新求值
Vue.mixin({
watch: {
componentRefresh() {
this.$forceUpdate();
}
}
});
App.mpType = 'app';
// 重写存储,增加前缀
uniStorage();
// 组件已改为按需引入,不再全局注册
async function bootstrap() {
const isRemoteOnly = langConfig && langConfig.source === 'remote-only';
if (isRemoteOnly) {
// remote-only 模式下必须等 bundle 就绪后再挂载 Vue,避免首屏组件在翻译就绪前渲染
let _hideLoading = false;
try {
uni.showLoading({ title: '加载中...', mask: true });
_hideLoading = true;
} catch (e) {}
try {
await Lang.boot();
} catch (e) {
console.warn('[i18n] remote-only boot fail', e);
} finally {
if (_hideLoading) {
try { uni.hideLoading(); } catch (e) {}
}
}
}
const app = new Vue({
...App,
store
});
app.$mount();
}
bootstrap()