-
Notifications
You must be signed in to change notification settings - Fork 113
/
bootstrap.ts
78 lines (66 loc) · 2.11 KB
/
bootstrap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import '@tok/ui/styles/global.scss';
import { DefinePresetsPlugin } from '@tok/generation/plugins/definePresets';
import { FormStatePlugin } from '@tok/generation/plugins/formState';
import { ThemePlugin } from '@tok/generation/plugins/theme';
import { TokI18nPlugin } from '@tok/i18n';
import { AlertsPlugin } from '@tok/ui/plugins/alerts';
import { CurrencyPlugin } from '@tok/ui/plugins/currency';
import { createApp } from 'vue';
import {
createRouter,
createWebHistory,
RouteLocationNormalized,
RouteRecordRaw,
} from 'vue-router';
import { BootstrapConfig } from './defineConfig';
type _App = Parameters<typeof createApp>[0];
export function bootstrap<T extends BootstrapConfig<any>>(
App: _App,
config: T
) {
const { fallback, ...asyncLocales } = config.locale || {};
const i18nOptions = {
default: fallback || 'en',
asyncLocales,
};
const pages = config.pages.map((config, index) => {
return {
path: index === 0 ? '/' : config.path || `/${index}`,
component: () => import('@tok/generation/presets/Route.vue'),
meta: {
config,
},
};
});
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL || '/'),
routes: ([] as RouteRecordRaw[]).concat(pages).concat({
path: '/not-found',
alias: '/:catchAll(.*)*',
redirect: '/',
}),
});
router.afterEach((to: RouteLocationNormalized) => {
if (to.params.savedPosition) {
return;
}
window.scrollTo({ top: 0 });
});
router.onError((error, to) => {
const dynamicallyImportsError =
error.message.includes('Failed to fetch dynamically imported module') ||
error.message.includes('Importing a module script failed');
if (error.name === 'ChunkLoadError' || dynamicallyImportsError) {
location.href = to.fullPath;
}
});
return createApp(App)
.use(router)
.use(AlertsPlugin)
.use(TokI18nPlugin, i18nOptions)
.use(ThemePlugin, config.theme || 'auto')
.use(FormStatePlugin)
.use(DefinePresetsPlugin, config.definePresets || {})
.use(CurrencyPlugin, config.currencyConfig || {})
.mount('#app');
}