Skip to content

Commit 68532aa

Browse files
feat: babel-plugin-import unsupport vant@v4 (#325)
* feat: babel-plugin-import is not supported until vant4 * feat: judge vant version --------- Co-authored-by: zhangyuang <leonin0829@gmail.com>
1 parent 0b4adba commit 68532aa

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

packages/plugin-react/src/tools/vite.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { build, UserConfig } from 'vite'
22
import {
33
loadConfig, chunkNamePlugin, rollupOutputOptions, manifestPlugin, commonConfig,
4-
asyncOptimizeChunkPlugin, getOutputPublicPath, judgeAntd
4+
asyncOptimizeChunkPlugin, getOutputPublicPath, getPkgMajorVersion
55
} from 'ssr-common-utils'
66
import react from '@vitejs/plugin-react'
77
import { createStyleImportPlugin, AndDesignVueResolve, VantResolve, ElementPlusResolve, NutuiResolve, AntdResolve } from 'ssr-vite-plugin-style-import'
88
const { getOutput, reactServerEntry, reactClientEntry, viteConfig, supportOptinalChaining, isDev, define, babelOptions, optimize } = loadConfig()
99
const { clientOutPut, serverOutPut } = getOutput()
10-
const isAntd5 = judgeAntd() === 5
10+
const isAntd5 = getPkgMajorVersion('antd') === 5
1111
const extraInclude = ([] as string[]).concat(isAntd5 ? ['react-is'] : [])
1212

1313
const styleImportConfig = {

packages/plugin-react18/src/tools/vite.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { build, UserConfig } from 'vite'
22
import {
33
loadConfig, chunkNamePlugin, rollupOutputOptions, manifestPlugin, commonConfig,
4-
asyncOptimizeChunkPlugin, getOutputPublicPath, judgeAntd
4+
asyncOptimizeChunkPlugin, getOutputPublicPath, getPkgMajorVersion
55
} from 'ssr-common-utils'
66
import react from '@vitejs/plugin-react'
77
import { createStyleImportPlugin, AndDesignVueResolve, VantResolve, ElementPlusResolve, NutuiResolve, AntdResolve } from 'ssr-vite-plugin-style-import'
88
const { getOutput, react18ServerEntry, react18ClientEntry, viteConfig, supportOptinalChaining, isDev, define, babelOptions, optimize } = loadConfig()
99
const { clientOutPut, serverOutPut } = getOutput()
10-
const isAntd5 = judgeAntd() === 5
10+
const isAntd5 = getPkgMajorVersion('antd') === 5
1111
const extraInclude = ([] as string[]).concat(isAntd5 ? ['react-is'] : [])
1212

1313
const styleImportConfig = {

packages/utils/src/server/judge.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { coerce } from 'semver'
22
import { getPkgJson } from './cwd'
33

4-
export const judgeAntd = () => {
5-
const antdVersion = getPkgJson().dependencies?.['antd'] ?? getPkgJson().devDependencies?.['antd']
4+
export const getPkgMajorVersion = (pkgName: string) => {
5+
const antdVersion = getPkgJson().dependencies?.[pkgName] ?? getPkgJson().devDependencies?.[pkgName]
66
return coerce(antdVersion)?.major
77
}

packages/utils/src/server/webpack/common-chain.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { getImageOutputPath } from '../parse'
77
import { loadModuleFromFramework, judgeFramework, getCwd } from '../cwd'
88
import { loadConfig } from '../loadConfig'
99
import { logWarning } from '../log'
10-
import { judgeAntd } from '../judge'
10+
import { getPkgMajorVersion } from '../judge'
1111
import { asyncChunkMap } from '../build-utils'
1212
import { nameSpaceBuiltinModules } from '../static'
1313

14-
const antdVersion = judgeAntd()
14+
const [antdVersion, vantVersion] = [getPkgMajorVersion('antd'), getPkgMajorVersion('vant')]
1515
const isAntd4 = antdVersion === 4
1616
if (antdVersion === 5) {
1717
logWarning('Check antd@5.x has been installed, antd@4.x is more recommend in ssr environment')
@@ -64,14 +64,6 @@ const addBabelLoader = (chain: Rule<Module>, envOptions: any, isServer: boolean)
6464
corejs: false
6565
}
6666
],
67-
[
68-
loadModuleFromFramework('babel-plugin-import'),
69-
{
70-
libraryName: 'vant',
71-
libraryDirectory: 'lib',
72-
style: true
73-
}, 'vant'
74-
],
7567
[
7668
loadModuleFromFramework('babel-plugin-import'),
7769
{
@@ -81,6 +73,18 @@ const addBabelLoader = (chain: Rule<Module>, envOptions: any, isServer: boolean)
8173
}, 'ant-design-vue'
8274
]
8375
]
76+
if (vantVersion && vantVersion < 4) {
77+
plugins.push(
78+
[
79+
loadModuleFromFramework('babel-plugin-import'),
80+
{
81+
libraryName: 'vant',
82+
libraryDirectory: 'lib',
83+
style: true
84+
}, 'vant'
85+
]
86+
)
87+
}
8488
if (framework === 'ssr-plugin-vue3') {
8589
plugins.push(loadModuleFromFramework('@vue/babel-plugin-jsx'))
8690
}

0 commit comments

Comments
 (0)