使用 antd Row 组件时 vite 提示引入 row 组件 css 发生错误:
[plugin:vite:import-analysis] Failed to resolve import "antd/lib/row/style/index.less" from "src/views/Form/Base/Project/index.tsx". Does the file exist?
/Users/wucheng085/Development/WorkSpace/luban/construct-luban-award-frontend/src/views/Form/Base/Project/index.tsx:2:9
1 | import 'antd/lib/steps/style/index.less'
2 | import 'antd/lib/row/style/index.less'
| ^
3 | import 'antd/lib/col/style/index.less'
4 |
我特地去看了下,的确没有 index.less:
ls node_modules/antd/lib/row/style/
css.js index.d.ts index.js
所以这里要把 import ‘antd/lib/row/style/index.less’ 改成 import ‘antd/lib/row/style/css.js’。
我试着改全局的 vitePluginImp 配置。
不行的话,我在重写方法,匹配 Row/Col 去定义。
第一个方法出错了,意思就是说不支持在 js 中引入 .css:
react.development.js:1309 Uncaught Error: Dynamic require of "/Users/wucheng085/Development/WorkSpace/luban/construct-luban-award-frontend/node_modules/.pnpm/antd@4.16.7_react-dom@17.0.2+react@17.0.2/node_modules/antd/lib/style/index.css" is not supported
at __require (chunk-R6I3GLEQ.js?v=9a889dec:11)
at node_modules/.pnpm/antd@4.16.7_react-dom@17.0.2+react@17.0.2/node_modules/antd/lib/avatar/style/css.js (css.js:3)
at __require2 (chunk-R6I3GLEQ.js?v=9a889dec:14)
at dep:antd_lib_avatar_style_css_js:1
__require @ chunk-R6I3GLEQ.js?v=9a889dec:11
node_modules/.pnpm/antd@4.16.7_react-dom@17.0.2+react@17.0.2/node_modules/antd/lib/avatar/style/css.js @ css.js:3
__require2 @ chunk-R6I3GLEQ.js?v=9a889dec:14
(anonymous) @ dep:antd_lib_avatar_style_css_js:1
看了 vite-plugin-imp 的 API,最终改成了这样解决,完美:
vitePluginImp({
optimize: true,
libList: [
{
libName: ‘antd’,
libDirectory: ‘es’,
style: (name) => antd/es/${name}/style
}
]
})