Skip to content

Commit f624e1e

Browse files
committed
fix(router): fix routing
1 parent 52c330c commit f624e1e

File tree

9 files changed

+56
-29
lines changed

9 files changed

+56
-29
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
"dist": "pnpm run clear && esno scripts/prepare.ts",
3232
"serve": "vite",
3333
"serve:static": "http-server dist --cors",
34-
"serve:web": "run-p build:web serve:static",
34+
"serve:web": "VITE_SOURCEMAP=true VITE_WEB=true run-p build serve:static",
3535
"dev": "cross-env NODE_ENV=development run-p dist serve",
3636
"build": "cross-env NODE_ENV=production pnpm run dist && vue-tsc --noEmit && vite build",
37-
"build:web": "VITE_WEB=true pnpm run build",
37+
"build:web": "VITE_WEB=true VITE_BASE=/trakt-extension/ pnpm run build",
3838
"test:unit": "vitest run --environment jsdom --coverage",
3939
"test:watch": "vitest --environment jsdom",
4040
"typecheck": "vue-tsc --noEmit -p tsconfig.vitest.json --composite false",

src/components/AppView.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import { RouterLink, RouterView } from 'vue-router';
33
44
import HelloWorld from '~/components/HelloWorld.vue';
5+
import { useRouterStore } from '~/stores/router.store';
56
import { useI18n } from '~/utils';
67
78
const test = 'test Value';
89
910
const i18n = useI18n('global');
11+
const { baseName } = useRouterStore();
1012
</script>
1113

1214
<template>
@@ -21,8 +23,8 @@ const i18n = useI18n('global');
2123
<HelloWorld :msg="test" />
2224

2325
<nav>
24-
<RouterLink to="/">Home</RouterLink>
25-
<RouterLink to="/about">About</RouterLink>
26+
<RouterLink :to="`${baseName}/`">Home</RouterLink>
27+
<RouterLink :to="`${baseName}/about`">About</RouterLink>
2628
</nav>
2729
</div>
2830
</header>

src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { defineComponent } from '~/views/web';
22

3-
defineComponent();
3+
defineComponent({ baseUrl: import.meta.env.VITE_BASE });

src/router/index.ts

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import { createRouter as createVueRouter, createWebHashHistory } from 'vue-router';
22

3-
export const createRouter = (basename: string = import.meta.env.BASE_URL) =>
4-
createVueRouter({
5-
history: createWebHashHistory(basename),
6-
routes: [
7-
{
8-
path: '/',
9-
name: 'home',
10-
component: () => import('../components/HomeView.vue'),
11-
},
12-
{
13-
path: '/about',
14-
name: 'about',
15-
component: () => import('../components/AboutView.vue'),
16-
},
17-
],
3+
import { routes } from '~/router/routes';
4+
import { useRouterStore } from '~/stores/router.store';
5+
6+
export type RouterOptions = { baseName?: string; baseUrl?: string };
7+
export const createRouter = ({ baseName = '', baseUrl = import.meta.env.BASE_URL }: RouterOptions) => {
8+
useRouterStore().setBaseName(baseName);
9+
return createVueRouter({
10+
history: createWebHashHistory(baseUrl),
11+
routes: routes.map(r => ({ ...r, path: `${baseName}${r.path}` })),
1812
});
13+
};

src/router/routes.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { RouteRecordRaw } from 'vue-router';
2+
3+
export const routes: RouteRecordRaw[] = [
4+
{
5+
path: `/`,
6+
name: 'home',
7+
component: () => import('../components/HomeView.vue'),
8+
},
9+
{
10+
path: `/about`,
11+
name: 'about',
12+
component: () => import('../components/AboutView.vue'),
13+
},
14+
];

src/stores/router.store.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineStore } from 'pinia';
2+
import { ref } from 'vue';
3+
4+
export const useRouterStore = defineStore('router', () => {
5+
const baseName = ref('');
6+
const setBaseName = (name: string) => {
7+
baseName.value = name;
8+
};
9+
10+
return { baseName, setBaseName };
11+
});

src/views/common/init-vue-app.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { createVuetify } from 'vuetify';
66

77
import type { App, Component } from 'vue';
88

9+
import type { RouterOptions } from '~/router';
10+
911
import AppView from '~/components/AppView.vue';
1012
import { createRouter } from '~/router';
1113

@@ -14,19 +16,21 @@ import { isDarkTheme } from '~/utils';
1416
import 'vuetify/styles';
1517
import '~/styles/base.css';
1618

17-
export type InitVueAppOption = { basename?: string };
18-
export const initVueApp = (component: Component = AppView, { basename }: InitVueAppOption = {}) => {
19+
export type InitVueAppOption = RouterOptions;
20+
export const initVueApp = (component: Component = AppView, options: InitVueAppOption = {}) => {
1921
const app = createApp(component);
22+
2023
const pinia = createPinia();
24+
app.use(pinia);
25+
2126
const vuetify = createVuetify({
2227
theme: {
2328
defaultTheme: isDarkTheme() ? 'dark' : 'light',
2429
},
2530
});
26-
const router = createRouter(basename);
27-
28-
app.use(pinia);
2931
app.use(vuetify);
32+
33+
const router = createRouter(options);
3034
app.use(router);
3135

3236
return app;

src/views/web/define-component.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import AppWeb from '~/components/web/AppWeb.ce.vue';
2-
32
import { createElementInstance } from '~/views/common';
43

54
export enum WebComponents {
65
TraktExtension = 'wc-trakt-extension',
76
}
87

9-
export type DefineComponent = (component?: WebComponents) => void;
8+
export type DefineOption = { baseName?: string; baseUrl?: string };
9+
export type DefineComponent = (options?: DefineOption, component?: WebComponents) => void;
1010

11-
export function defineComponent(options: { basename?: string } = {}, component: WebComponents = WebComponents.TraktExtension): void {
11+
export function defineComponent(options: DefineOption = {}, component: WebComponents = WebComponents.TraktExtension): void {
1212
if (customElements.get(component)) {
1313
console.warn(`Custom element '${component}' is already defined.`);
1414
} else {

vite.config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { isDev, port, resolveParent } from './scripts/utils';
1111
import type { InputOption } from 'rollup';
1212

1313
const isWeb = !!process.env.VITE_WEB;
14+
const sourcemap = !!process.env.VITE_SOURCEMAP;
1415

1516
const getInput = (hmr: boolean): InputOption => {
1617
if (hmr) return { background: resolveParent('src/scripts/background/index.ts') };
@@ -69,7 +70,7 @@ export default defineConfig(({ command }) => ({
6970
},
7071
build: {
7172
outDir: resolveParent('dist'),
72-
sourcemap: isDev ? 'inline' : false,
73+
sourcemap: isDev || sourcemap ? 'inline' : false,
7374
rollupOptions: {
7475
input: getInput(isDev),
7576
output: {

0 commit comments

Comments
 (0)