Skip to content

Commit 9cd01e2

Browse files
chengzhuo5chengzhuoantfu
authored
fix(vant): supports SSR (#318)
Co-authored-by: chengzhuo <[email protected]> Co-authored-by: Anthony Fu <[email protected]>
1 parent fb7538f commit 9cd01e2

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/core/resolvers/vant.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { ComponentResolver, SideEffectsInfo } from '../../types'
22
import { kebabCase } from '../utils'
33

4+
const isServer = Boolean(process.env.SSR || process.env.SSG || process.env.VITE_SSR || process.env.VITE_SSG)
5+
const moduleType = isServer ? 'lib' : 'es'
6+
47
export interface VantResolverOptions {
58
/**
69
* import style css or less along with components
@@ -13,16 +16,16 @@ export interface VantResolverOptions {
1316
function getSideEffects(dirName: string, options: VantResolverOptions): SideEffectsInfo | undefined {
1417
const { importStyle = true } = options
1518

16-
if (!importStyle)
19+
if (!importStyle || isServer)
1720
return
1821

1922
if (importStyle === 'less')
20-
return `vant/es/${dirName}/style/less`
23+
return `vant/${moduleType}/${dirName}/style/less`
2124

2225
if (importStyle === 'css')
23-
return `vant/es/${dirName}/style/index`
26+
return `vant/${moduleType}/${dirName}/style/index`
2427

25-
return `vant/es/${dirName}/style/index`
28+
return `vant/${moduleType}/${dirName}/style/index`
2629
}
2730

2831
/**
@@ -38,7 +41,7 @@ export function VantResolver(options: VantResolverOptions = {}): ComponentResolv
3841
const partialName = name.slice(3)
3942
return {
4043
importName: partialName,
41-
path: 'vant/es',
44+
path: `vant/${moduleType}`,
4245
sideEffects: getSideEffects(kebabCase(partialName), options),
4346
}
4447
}

0 commit comments

Comments
 (0)