使用 vite2.x + vue3 + ant-design-vue2 开发时,当引入了ant-design-vue 的 less 样式文件报以下异常:
// 引入官方提供的 less 样式入口文件
@import 'ant-design-vue/dist/antd.less';
// 全局主色
@primary-color: #354ab3;
下午2:08:46 [vite] Internal server error: Inline JavaScript is not enabled. Is it set in your options?
Plugin: vite:css
File: D:/my-vite-app/node_modules/ant-design-vue/lib/style/color/bezierEasing.less
107| // It is hacky way to make this function will be compiled preferentially by less
108| // resolve error: `ReferenceError: colorPalette is not defined`
109| // https://github.com/ant-design/ant-motion/issues/44
| ^
110| .bezierEasingMixin();
111|
at less (D:\my-vite-app\node_modules\vite\dist\node\chunks\dep-a5ddf8f9.js:18247:33)
at async compileCSS (D:\my-vite-app\node_modules\vite\dist\node\chunks\dep-a5ddf8f9.js:17943:34)
at async TransformContext.transform (D:\my-vite-app\node_modules\vite\dist\node\chunks\dep-a5ddf8f9.js:17631:50)
at async Object.transform (D:\my-vite-app\node_modules\vite\dist\node\chunks\dep-a5ddf8f9.js:45905:30)
at async transformRequest (D:\my-vite-app\node_modules\vite\dist\node\chunks\dep-a5ddf8f9.js:61479:29)
at async D:\my-vite-app\node_modules\vite\dist\node\chunks\dep-a5ddf8f9.js:61581:32
这时候需要在 vite.config.js 中添加以下配置:
// vite2.x
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true,
}
},
},
如果你的项目使用的是 vite1.x ,则配置方式跟 vite2.x 稍有不同,如下所示:
// vite1.x
cssPreprocessOptions: {
less: {
javascriptEnabled: true,
},
},