|
1 | 1 | import { createUnplugin } from 'unplugin' |
2 | 2 | import { resolvePath } from 'mlly' |
3 | | -import { dirname, join } from 'pathe' |
| 3 | +import { dirname } from 'pathe' |
4 | 4 |
|
5 | 5 | 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 |
7 | 14 | } |
8 | 15 |
|
9 | 16 | export const schemaOrgSwapAliases = () => createUnplugin<PluginOptions>((args) => { |
10 | 17 | return { |
11 | | - name: '@vueuse/schema-org:ssr-mock-plugin', |
| 18 | + name: '@vueuse/schema-org:aliases', |
12 | 19 | enforce: 'pre', |
13 | 20 | vite: { |
14 | | - async config(config) { |
| 21 | + async config(config, ctx) { |
15 | 22 | config.resolve = config.resolve || {} |
16 | 23 | config.resolve.alias = config.resolve.alias || {} |
| 24 | + args = args || {} |
| 25 | + |
| 26 | + if (typeof args.mock === 'undefined') |
| 27 | + args.mock = !ctx.ssrBuild |
17 | 28 |
|
18 | 29 | const SchemaOrgPkg = '@vueuse/schema-org' |
19 | 30 | // avoid unwanted behavior with different package managers |
| 31 | + |
20 | 32 | // @ts-expect-error untyped |
21 | 33 | config.resolve.alias[SchemaOrgPkg] = dirname(await resolvePath(SchemaOrgPkg)) |
22 | 34 |
|
23 | 35 | if (args?.mock) { |
| 36 | + const mockPath = await resolvePath(`${SchemaOrgPkg}/runtime/mock`) |
24 | 37 | // @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 |
26 | 39 | // @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 |
28 | 41 | } |
29 | 42 | else { |
30 | 43 | // @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'}`) |
32 | 45 | // @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`) |
34 | 47 | } |
35 | 48 | }, |
36 | 49 | }, |
|
0 commit comments