Skip to content

Commit e4876d6

Browse files
committed
fix(vite): improved integration API
1 parent 154dbc0 commit e4876d6

File tree

5 files changed

+62
-21
lines changed

5 files changed

+62
-21
lines changed

packages/vite/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"magic-string": "^0.26.2",
6868
"mlly": "^0.5.12",
6969
"pathe": "^0.3.4",
70+
"schema-dts": "^1.1.0",
7071
"unplugin": "^0.9.0"
7172
},
7273
"devDependencies": {

packages/vite/src/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
import { RootSchemas, schemaOrgComponents } from '@vueuse/schema-org'
22
import type { SchemaOrgResolverFn } from './types'
33

4+
export interface MetaInput {
5+
host: string
6+
url?: string
7+
path?: string
8+
currency?: string
9+
image?: string
10+
inLanguage?: string
11+
title?: string
12+
description?: string
13+
datePublished?: string
14+
dateModified?: string
15+
/**
16+
* @deprecated use `language`
17+
*/
18+
defaultLanguage?: string
19+
/**
20+
* @deprecated use `currency`
21+
*/
22+
defaultCurrency?: string
23+
/**
24+
* @deprecated use `host`
25+
*/
26+
canonicalHost?: string
27+
/**
28+
* @deprecated use `url` or `path`
29+
*/
30+
canonicalUrl?: string
31+
}
32+
433
export interface SchemaOrgResolverOptions {
534
/**
635
* prefix for headless ui components used in templates

packages/vite/src/plugins.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
11
import { createUnplugin } from 'unplugin'
22
import { resolvePath } from 'mlly'
3-
import { dirname, join } from 'pathe'
3+
import { dirname } from 'pathe'
44

55
export interface PluginOptions {
6-
mock: boolean
6+
/**
7+
* Should the runtime be swapped out with a mock one, used for SSR-only mode.
8+
*/
9+
mock?: boolean
10+
/**
11+
* Whether to use schema-dts types on define functions
12+
*/
13+
full?: boolean
714
}
815

916
export const schemaOrgSwapAliases = () => createUnplugin<PluginOptions>((args) => {
1017
return {
11-
name: '@vueuse/schema-org:ssr-mock-plugin',
18+
name: '@vueuse/schema-org:aliases',
1219
enforce: 'pre',
1320
vite: {
14-
async config(config) {
21+
async config(config, ctx) {
1522
config.resolve = config.resolve || {}
1623
config.resolve.alias = config.resolve.alias || {}
24+
args = args || {}
25+
26+
if (typeof args.mock === 'undefined')
27+
args.mock = !ctx.ssrBuild
1728

1829
const SchemaOrgPkg = '@vueuse/schema-org'
1930
// avoid unwanted behavior with different package managers
31+
2032
// @ts-expect-error untyped
2133
config.resolve.alias[SchemaOrgPkg] = dirname(await resolvePath(SchemaOrgPkg))
2234

2335
if (args?.mock) {
36+
const mockPath = await resolvePath(`${SchemaOrgPkg}/runtime/mock`)
2437
// @ts-expect-error untyped
25-
config.resolve.alias['#vueuse/schema-org/provider'] = '@vueuse/schema-org/runtime/mock'
38+
config.resolve.alias['#vueuse/schema-org/provider'] = mockPath
2639
// @ts-expect-error untyped
27-
config.resolve.alias['#vueuse/schema-org/runtime'] = '@vueuse/schema-org/runtime/mock'
40+
config.resolve.alias['#vueuse/schema-org/runtime'] = mockPath
2841
}
2942
else {
3043
// @ts-expect-error untyped
31-
config.resolve.alias['#vueuse/schema-org/provider'] = await resolvePath(join(SchemaOrgPkg, 'lite'))
44+
config.resolve.alias['#vueuse/schema-org/provider'] = await resolvePath(`${SchemaOrgPkg}/${args?.full ? 'full' : 'simple'}`)
3245
// @ts-expect-error untyped
33-
config.resolve.alias['#vueuse/schema-org/runtime'] = await resolvePath(join(SchemaOrgPkg, 'runtime'))
46+
config.resolve.alias['#vueuse/schema-org/runtime'] = await resolvePath(`${SchemaOrgPkg}/runtime`)
3447
}
3548
},
3649
},

packages/vite/src/vitepress.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
// @ts-expect-error untyped
2-
import type { ProviderOptions } from '@vueuse/schema-org'
31
import { createSchemaOrg } from '@vueuse/schema-org'
42
import type { EnhanceAppContext } from 'vitepress'
53
import { createHead } from '@vueuse/head'
64
import { watch } from 'vue-demi'
5+
import type { MetaInput } from './'
76

8-
export function installSchemaOrg(ctx: EnhanceAppContext, options: ProviderOptions) {
7+
export function installSchemaOrg(ctx: EnhanceAppContext, meta: MetaInput) {
98
// check if `createHead` has already been done
109
let head = ctx.app._context.provides.usehead
1110
if (!head) {
1211
head = createHead()
1312
ctx.app.use(head)
1413
}
1514

16-
const schemaOrg = createSchemaOrg({
17-
...options,
15+
const client = createSchemaOrg({
1816
meta() {
1917
return {
2018
...ctx.siteData.value,
19+
...meta,
2120
}
2221
},
2322
updateHead(fn) {
@@ -27,10 +26,10 @@ export function installSchemaOrg(ctx: EnhanceAppContext, options: ProviderOption
2726
})
2827

2928
watch(() => ctx.router.route.data.relativePath, () => {
30-
// @todo
29+
client.generateSchema()
3130
})
3231

33-
ctx.app.use(schemaOrg)
34-
schemaOrg.setupDOM()
35-
return schemaOrg
32+
ctx.app.use(client)
33+
client.setupDOM()
34+
return client
3635
}

packages/vite/src/vitesse.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
// @ts-expect-error untyped
2-
import type { ProviderOptions } from '@vueuse/schema-org'
31
import { createSchemaOrg } from '@vueuse/schema-org'
42
import type { ViteSSGContext } from 'vite-ssg'
53
import { watch } from 'vue-demi'
4+
import type { MetaInput } from './'
65

7-
export function installSchemaOrg(ctx: ViteSSGContext, options: ProviderOptions) {
6+
export function installSchemaOrg(ctx: ViteSSGContext, meta: MetaInput) {
87
const ssr = !ctx.isClient
98

109
const client = createSchemaOrg({
@@ -25,7 +24,7 @@ export function installSchemaOrg(ctx: ViteSSGContext, options: ProviderOptions)
2524

2625
return {
2726
path: ctx.router.currentRoute.value.path,
28-
...options.meta ?? {},
27+
...meta ?? {},
2928
...ctx.router.currentRoute.value.meta,
3029
}
3130
},

0 commit comments

Comments
 (0)