[开发问题]The legacy JS API is deprecated and will be removed in Dart Sass

3,111 阅读1分钟

问题

升级了NodeJS版本到v20.17.0(也可能其他原因)之后,Vite项目启动时出现警告:

Deprecation Warning: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0. (弃用警告:旧版JS API已弃用,并将在Dart Sass 2.0.0中删除。)

 More info: https: //sass-lang.com/d/legacy-js-api

解决

vite.config.js文件中增加以下配置:

css: {
    preprocessorOptions: {
        scss: {
            api: 'modern-compiler', // or 'modern'
        },
    },
},

如果只想让弃用警告静音,可以使用silenceDeprecations选项:

css: {
    preprocessorOptions: {
        scss: {
            silenceDeprecations: ['legacy-js-api']
        },
    },
},

这两种解决方案都适用于Vite 5.4.6和Sass 1.79.1。