Skip to content

Commit

Permalink
example: add lazy-loading (#135)
Browse files Browse the repository at this point in the history
* example: add lazy-loading

closes #129

* updates
  • Loading branch information
kazupon committed Oct 4, 2020
1 parent 9c8c409 commit a7d2147
Show file tree
Hide file tree
Showing 31 changed files with 6,969 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/lazy-loading/vite/.gitignore
@@ -0,0 +1,4 @@
node_modules
.DS_Store
dist
*.local
12 changes: 12 additions & 0 deletions examples/lazy-loading/vite/index.html
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lazy loading exaple</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions examples/lazy-loading/vite/package.json
@@ -0,0 +1,18 @@
{
"name": "lazy-loading-vite",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"vue": "^3.0.0",
"vue-i18n": "^9.0.0-beta.4",
"vue-router": "^4.0.0-beta.13"
},
"devDependencies": {
"@vue/compiler-sfc": "^3.0.0",
"vite": "^1.0.0-rc.4"
}
}
Empty file.
57 changes: 57 additions & 0 deletions examples/lazy-loading/vite/src/App.vue
@@ -0,0 +1,57 @@
<template>
<nav>
<div class="navigation">
<router-link :to="{ name: 'home', params: { locale } }">
{{ $t('navigations.home') }}
</router-link>
|
<router-link :to="{ name: 'about', params: { locale } }">
{{ $t('navigations.about') }}
</router-link>
</div>
<form class="language">
<label>{{ $t('labels.language') }}</label>
<select v-model="locale">
<option value="en">en</option>
<option value="ja">ja</option>
</select>
</form>
</nav>
<router-view></router-view>
</template>

<script>
import { ref, watch, defineComponent } from 'vue'
import { useRoute, useRouter } from 'vue-router'
export default defineComponent({
name: 'App',
setup() {
const locale = ref('en')
const router = useRouter()
const route = useRoute()
// when change the locale, go to locale route
watch(locale, val => {
router.push({
name: route.name,
params: { locale: val }
})
})
return { locale }
}
})
</script>

<style scoped>
nav {
display: inline-flex;
}
.navigation {
margin-right: 1rem;
}
.language label {
margin-right: 1rem;
}
</style>
Empty file.
Empty file.
28 changes: 28 additions & 0 deletions examples/lazy-loading/vite/src/i18n.ts
@@ -0,0 +1,28 @@
import { createI18n } from 'vue-i18n'
import type { I18n, Locale } from 'vue-i18n'

import en from './locales/en'

export function setupI18n(locale: Locale = 'en'): I18n {
const i18n = createI18n({
locale,
fallbackLocale: 'en',
messages: {
en
}
}) as I18n
setI18nLanguage(i18n, locale)
return i18n
}

export function setI18nLanguage(i18n: I18n, locale: Locale): void {
i18n.global.locale.value = locale
/**
* NOTE:
* If you need to specify the language setting for headers, such as the `fetch` API, set it here.
* The following is an example for axios.
*
* axios.defaults.headers.common['Accept-Language'] = locale
*/
document.querySelector('html').setAttribute('lang', locale)
}
6 changes: 6 additions & 0 deletions examples/lazy-loading/vite/src/index.css
@@ -0,0 +1,6 @@
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
}
13 changes: 13 additions & 0 deletions examples/lazy-loading/vite/src/locales/en.js
@@ -0,0 +1,13 @@
export default {
pages: {
home: 'This page is home page',
about: 'This page is about page'
},
navigations: {
home: 'Home',
about: 'About'
},
labels: {
language: 'Languages'
}
}
13 changes: 13 additions & 0 deletions examples/lazy-loading/vite/src/locales/ja.js
@@ -0,0 +1,13 @@
export default {
pages: {
home: 'このページはホームです。',
about: 'このページはアバウトページです。'
},
navigations: {
home: 'ホーム',
about: 'アバウト'
},
labels: {
language: '言語'
}
}
13 changes: 13 additions & 0 deletions examples/lazy-loading/vite/src/main.ts
@@ -0,0 +1,13 @@
import { createApp } from 'vue'
import App from './App.vue'
import './index.css'
import { setupRouter } from './router'
import { setupI18n } from './i18n'

const i18n = setupI18n()
const router = setupRouter(i18n)

const app = createApp(App)
app.use(i18n)
app.use(router)
app.mount('#app')
3 changes: 3 additions & 0 deletions examples/lazy-loading/vite/src/pages/About.vue
@@ -0,0 +1,3 @@
<template>
<h2>{{ $t('pages.about') }}</h2>
</template>
3 changes: 3 additions & 0 deletions examples/lazy-loading/vite/src/pages/Home.vue
@@ -0,0 +1,3 @@
<template>
<h2>{{ $t('pages.home') }}</h2>
</template>
59 changes: 59 additions & 0 deletions examples/lazy-loading/vite/src/router.ts
@@ -0,0 +1,59 @@
import { createRouter, createWebHistory } from 'vue-router'
import { setI18nLanguage } from './i18n'
import type { Router, RouteRecordRaw } from 'vue-router'
import type { I18n, Locale } from 'vue-i18n'

import Home from './pages/Home.vue'
import About from './pages/About.vue'

export function setupRouter(i18n: I18n): Router {
const SUPPORT_LOCALES = ['en', 'ja']
const { global: composer } = i18n

// setup routes
const routes: RouteRecordRaw[] = [
{
path: '/:locale/',
name: 'home',
component: Home
},
{
path: '/:locale/about',
name: 'about',
component: About
},
{
path: '/:pathMatch(.*)*',
redirect: () => `/${composer.locale.value}`
}
]

// create router instance
const router = createRouter({
history: createWebHistory(),
routes
})

// navigation guards
router.beforeEach(async (to, from, next) => {
const locale = (to.params as any).locale as Locale

// check locale
if (!SUPPORT_LOCALES.includes(locale)) {
return false
}

// load locale messages
if (!composer.availableLocales.includes(locale)) {
const messages = await import(`./locales/${locale}`)
composer.setLocaleMessage(locale, messages.default)
}

// set i18n language
setI18nLanguage(i18n, locale)

return next()
})

return router
}
76 changes: 76 additions & 0 deletions examples/lazy-loading/vite/tsconfig.json
@@ -0,0 +1,76 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"lib": ["dom", "esnext"], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
"types": [
"./node_modules/@types",
"./node_modules/vite/dist",
"./node_modules/vue-i18n/dist"
], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */

"resolveJsonModule": true
},
"exclude": ["node_modules", "dist"]
}

0 comments on commit a7d2147

Please sign in to comment.