Skip to content

Commit f824b03

Browse files
committed
fix(vite): export Vite specific plugin
1 parent 53f8fc2 commit f824b03

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

packages/vite/build.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default defineBuildConfig({
1010
{ input: 'src/index' },
1111
{ input: 'src/vitesse' },
1212
{ input: 'src/vitepress' },
13+
{ input: 'src/vite' },
1314
],
1415
externals: [
1516
'@vueuse/schema-org',

packages/vite/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
"require": "./dist/index.cjs",
2929
"import": "./dist/index.mjs"
3030
},
31+
"./vite": {
32+
"types": "./dist/vite.d.ts",
33+
"require": "./dist/vite.cjs",
34+
"import": "./dist/vite.mjs"
35+
},
3136
"./vitesse": {
3237
"types": "./dist/vitesse.d.ts",
3338
"require": "./dist/vitesse.cjs",
@@ -78,6 +83,7 @@
7883
"unplugin-vue-components": "^0.22.4",
7984
"vite": "^3.0.8",
8085
"vite-ssg": "^0.20.2",
81-
"vitepress": "1.0.0-alpha.8"
86+
"vitepress": "1.0.0-alpha.8",
87+
"vue-router": "^4.1.4"
8288
}
8389
}

packages/vite/src/plugins/alias.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,6 @@ export const AliasRuntimePlugin = () => createUnplugin<AliasPluginOptions>((user
119119
}
120120
})
121121

122-
export const AliasRuntimePluginWebpack = (args: AliasPluginOptions) => AliasRuntimePlugin().webpack(args)
123-
export const AliasRuntimePluginVite = (args: AliasPluginOptions) => AliasRuntimePlugin().vite(args)
122+
export const AliasRuntimePluginWebpack = (args?: AliasPluginOptions) => AliasRuntimePlugin().webpack(args || {})
123+
export const AliasRuntimePluginVite = (args?: AliasPluginOptions) => AliasRuntimePlugin().vite(args || {})
124+
export const SchemaOrg = AliasRuntimePluginVite

packages/vite/src/vite.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { createSchemaOrg, resolveUserConfig } from '@vueuse/schema-org'
2+
import type { UserConfig } from '@vueuse/schema-org'
3+
import type { App } from 'vue'
4+
import { createHead } from '@vueuse/head'
5+
import type { Router } from 'vue-router'
6+
7+
export function installSchemaOrg(ctx: { app: App; router?: Router }, config: UserConfig) {
8+
const resolvedConfig = resolveUserConfig(config)
9+
10+
let head = ctx.app._context.provides.usehead
11+
if (!head) {
12+
head = createHead()
13+
ctx.app.use(head)
14+
}
15+
16+
const client = createSchemaOrg({
17+
updateHead(fn) {
18+
head.addHeadObjs(fn)
19+
if (typeof document !== 'undefined')
20+
head.updateDOM()
21+
},
22+
meta() {
23+
// const inferredMeta: Record<string, any> = {}
24+
25+
return {
26+
path: ctx.router?.currentRoute.value.path || '/',
27+
...resolvedConfig.meta,
28+
...ctx.router?.currentRoute.value.meta || {},
29+
}
30+
},
31+
})
32+
33+
ctx.app.use(client)
34+
35+
if (typeof document === 'undefined') {
36+
client.generateSchema()
37+
client.setupDOM()
38+
return
39+
}
40+
41+
if (ctx.router) {
42+
ctx.router.afterEach(() => {
43+
client.generateSchema()
44+
client.setupDOM()
45+
})
46+
}
47+
return client
48+
}

0 commit comments

Comments
 (0)