Skip to content

Commit 44863e4

Browse files
committed
vite(plugin): transform aliases in runtime to avoid ESM import transforms
1 parent aed49cb commit 44863e4

File tree

1 file changed

+75
-21
lines changed

1 file changed

+75
-21
lines changed

packages/vite/src/plugins.ts

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,104 @@
11
import { createUnplugin } from 'unplugin'
22
import { resolvePath } from 'mlly'
3-
import { dirname } from 'pathe'
3+
import { dirname, join } from 'pathe'
4+
import MagicString from 'magic-string'
45

56
export interface PluginOptions {
67
/**
78
* Should the runtime be swapped out with a mock one, used for SSR-only mode.
89
*/
910
mock?: boolean
1011
/**
11-
* Whether to use schema-dts types on define functions
12+
* Whether to use schema-dts types on define functions.
1213
*/
1314
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
1435
}
1536

1637
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+
1759
return {
1860
name: '@vueuse/schema-org:aliases',
1961
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+
},
2085
vite: {
2186
async config(config, ctx) {
87+
const { pkgDir, provider, runtime } = await fetchPaths()
88+
2289
config.resolve = config.resolve || {}
2390
config.resolve.alias = config.resolve.alias || {}
24-
args = args || {}
2591

2692
if (typeof args.mock === 'undefined')
2793
args.mock = !ctx.ssrBuild
28-
29-
const SchemaOrgPkg = '@vueuse/schema-org'
3094
// avoid unwanted behavior with different package managers
31-
3295
// @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
48102
},
49103
},
50104
}

0 commit comments

Comments
 (0)