Skip to content

Commit

Permalink
feat: update main scripts
Browse files Browse the repository at this point in the history
use typescript and vue 3; add service worker script
  • Loading branch information
rudnovd committed Dec 22, 2020
1 parent e16692d commit 432e9c4
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 50 deletions.
9 changes: 0 additions & 9 deletions src/i18n.js

This file was deleted.

19 changes: 19 additions & 0 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createI18n } from 'vue-i18n'

const i18n = createI18n({
legacy: false,
locale: 'en',
availableLocales: ['en', 'ru'],
})

export function setI18nLanguage(locale: string): void {
i18n.global.locale.value = locale
document.querySelector('html')?.setAttribute('lang', locale)
}

export async function loadLocaleMessages(locale: string): Promise<void> {
const messages = await import(/* webpackChunkName: "locale-[request]" */ `./locales/${locale}.json`)
i18n.global.setLocaleMessage(locale, messages.default)
}

export default i18n
41 changes: 0 additions & 41 deletions src/main.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createApp } from 'vue'
import App from '@/App.vue'
import i18n from '@/i18n'
import router from '@/router'
import store from '@/store'
import '@/registerServiceWorker'

createApp(App).use(i18n).use(router).use(store).mount('body')
31 changes: 31 additions & 0 deletions src/registerServiceWorker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable no-console */

import { register } from 'register-service-worker'

if (process.env.NODE_ENV === 'production') {
register(`${process.env.BASE_URL}service-worker.js`, {
ready() {
console.log(
'App is being served from cache by a service worker.\n' + 'For more details, visit https://goo.gl/AFskqB'
)
},
registered() {
console.log('Service worker has been registered.')
},
cached() {
console.log('Content has been cached for offline use.')
},
updatefound() {
console.log('New content is downloading.')
},
updated() {
console.log('New content is available; please refresh.')
},
offline() {
console.log('No internet connection found. App is running in offline mode.')
},
error(error) {
console.error('Error during service worker registration:', error)
},
})
}
62 changes: 62 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import i18n from '@/i18n'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes: [
{
path: '/',
component: () => import(/* webpackChunkName: 'Home' */ '@/views/Home.vue'),
meta: {
title: i18n.global.t('pages.Home'),
},
},
{
path: '/damage',
component: () => import(/* webpackChunkName: 'DamageCalculator' */ '@/views/DamageCalculator.vue'),
meta: {
title: i18n.global.t('pages.DamageCalculator'),
},
},
{
path: '/creatures',
component: () => import(/* webpackChunkName: 'CreaturesLibrary' */ '@/views/CreaturesLibrary.vue'),
meta: {
title: i18n.global.t('pages.CreaturesLibrary'),
disabled: true,
},
},
{
path: '/:pathMatch(.*)*',
name: 'not-found',
component: () => import(/* webpackChunkName: 'Error' */ '@/views/Error.vue'),
meta: {
title: i18n.global.t('pages.Error'),
},
},
],
scrollBehavior(to, from, savedPosition) {
return new Promise((resolve) => {
if (savedPosition) return resolve(savedPosition)
else if (to.hash) return resolve({ el: to.hash })
else return resolve({ left: 0, top: 0 })
})
},
})

router.resolve({
name: 'not-found',
params: { pathMatch: ['not', 'found'] },
}).href

router.beforeEach((to, from, next) => {
if (to.meta.disabled) next({ path: '/' })

next()
})

router.afterEach((to) => {
if (to.meta.title) document.title = to.meta.title
})

export default router
7 changes: 7 additions & 0 deletions src/shims-vue.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-explicit-any */
declare module '*.vue' {
import type { DefineComponent } from 'vue'
const component: DefineComponent<{}, {}, any>
export default component
}
20 changes: 20 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createStore } from 'vuex'
import { CalculatorInstance } from '@/models/Calculator'

export default createStore({
state: {
calculators: [] as Array<CalculatorInstance>,
activeIndex: 0,
},
actions: {
rememberCalculators({ commit }, payload: { calculators: Array<CalculatorInstance> }) {
commit('REMEMBER_CALCULATORS', { calculators: payload.calculators })
},
},
mutations: {
REMEMBER_CALCULATORS(state, payload: { calculators: Array<CalculatorInstance> }) {
state.calculators = payload.calculators
},
},
strict: process.env.NODE_ENV === 'development',
})

0 comments on commit 432e9c4

Please sign in to comment.