|
1 | 1 | import { createUnplugin } from 'unplugin' |
2 | 2 | import { resolvePath } from 'mlly' |
3 | | -import { dirname } from 'pathe' |
| 3 | +import { dirname, join } from 'pathe' |
| 4 | +import MagicString from 'magic-string' |
4 | 5 |
|
5 | 6 | export interface PluginOptions { |
6 | 7 | /** |
7 | 8 | * Should the runtime be swapped out with a mock one, used for SSR-only mode. |
8 | 9 | */ |
9 | 10 | mock?: boolean |
10 | 11 | /** |
11 | | - * Whether to use schema-dts types on define functions |
| 12 | + * Whether to use schema-dts types on define functions. |
12 | 13 | */ |
13 | 14 | full?: boolean |
| 15 | + /** |
| 16 | + * Path to a real custom runtime (not mocked). |
| 17 | + */ |
| 18 | + runtimePath?: string |
| 19 | + /** |
| 20 | + * Path to a real custom provider (not mocked). |
| 21 | + */ |
| 22 | + providerPath?: string |
| 23 | + /** |
| 24 | + * Scan files from this root directory (ignoring node_modules). |
| 25 | + */ |
| 26 | + transformPaths?: string[] |
| 27 | +} |
| 28 | + |
| 29 | +const SchemaOrgPkg = '@vueuse/schema-org' |
| 30 | + |
| 31 | +interface AliasPaths { |
| 32 | + pkgDir: string |
| 33 | + provider: string |
| 34 | + runtime: string |
14 | 35 | } |
15 | 36 |
|
16 | 37 | export const schemaOrgSwapAliases = () => createUnplugin<PluginOptions>((args) => { |
| 38 | + let paths: AliasPaths |
| 39 | + const fetchPaths = async () => { |
| 40 | + if (paths) |
| 41 | + return paths |
| 42 | + const pkgDir = dirname(await resolvePath(SchemaOrgPkg)) |
| 43 | + let provider, runtime |
| 44 | + if (args?.mock) { |
| 45 | + provider = runtime = await resolvePath(`${SchemaOrgPkg}/runtime/mock`) |
| 46 | + } |
| 47 | + else { |
| 48 | + provider = args?.providerPath || await resolvePath(`${SchemaOrgPkg}/${args?.full ? 'full' : 'simple'}`) |
| 49 | + runtime = args?.runtimePath || await resolvePath(`${SchemaOrgPkg}/runtime`) |
| 50 | + } |
| 51 | + paths = { |
| 52 | + pkgDir, |
| 53 | + provider, |
| 54 | + runtime, |
| 55 | + } |
| 56 | + return paths |
| 57 | + } |
| 58 | + |
17 | 59 | return { |
18 | 60 | name: '@vueuse/schema-org:aliases', |
19 | 61 | enforce: 'pre', |
| 62 | + async buildStart() { |
| 63 | + await fetchPaths() |
| 64 | + }, |
| 65 | + transformInclude(id) { |
| 66 | + if (id.startsWith(join(paths.pkgDir, 'runtime'))) |
| 67 | + return true |
| 68 | + for (const p of args?.transformPaths || []) { |
| 69 | + if (id.startsWith(p)) |
| 70 | + return true |
| 71 | + } |
| 72 | + return false |
| 73 | + }, |
| 74 | + transform(code) { |
| 75 | + // swap out aliases for real paths |
| 76 | + const s = new MagicString(code) |
| 77 | + s.replace('#vueuse/schema-org/provider', paths.provider) |
| 78 | + s.replace('#vueuse/schema-org/runtime', paths.runtime) |
| 79 | + |
| 80 | + return { |
| 81 | + code: s.toString(), |
| 82 | + map: s.generateMap(), |
| 83 | + } |
| 84 | + }, |
20 | 85 | vite: { |
21 | 86 | async config(config, ctx) { |
| 87 | + const { pkgDir, provider, runtime } = await fetchPaths() |
| 88 | + |
22 | 89 | config.resolve = config.resolve || {} |
23 | 90 | config.resolve.alias = config.resolve.alias || {} |
24 | | - args = args || {} |
25 | 91 |
|
26 | 92 | if (typeof args.mock === 'undefined') |
27 | 93 | args.mock = !ctx.ssrBuild |
28 | | - |
29 | | - const SchemaOrgPkg = '@vueuse/schema-org' |
30 | 94 | // avoid unwanted behavior with different package managers |
31 | | - |
32 | 95 | // @ts-expect-error untyped |
33 | | - config.resolve.alias[SchemaOrgPkg] = dirname(await resolvePath(SchemaOrgPkg)) |
34 | | - |
35 | | - if (args?.mock) { |
36 | | - const mockPath = await resolvePath(`${SchemaOrgPkg}/runtime/mock`) |
37 | | - // @ts-expect-error untyped |
38 | | - config.resolve.alias['#vueuse/schema-org/provider'] = mockPath |
39 | | - // @ts-expect-error untyped |
40 | | - config.resolve.alias['#vueuse/schema-org/runtime'] = mockPath |
41 | | - } |
42 | | - else { |
43 | | - // @ts-expect-error untyped |
44 | | - config.resolve.alias['#vueuse/schema-org/provider'] = await resolvePath(`${SchemaOrgPkg}/${args?.full ? 'full' : 'simple'}`) |
45 | | - // @ts-expect-error untyped |
46 | | - config.resolve.alias['#vueuse/schema-org/runtime'] = await resolvePath(`${SchemaOrgPkg}/runtime`) |
47 | | - } |
| 96 | + config.resolve.alias[SchemaOrgPkg] = pkgDir |
| 97 | + // @ts-expect-error untyped |
| 98 | + config.resolve.alias['#vueuse/schema-org/provider'] = provider |
| 99 | + // @ts-expect-error untyped |
| 100 | + config.resolve.alias['#vueuse/schema-org/runtime'] = runtime |
| 101 | + return config |
48 | 102 | }, |
49 | 103 | }, |
50 | 104 | } |
|
0 commit comments