我想通过 vite 获取 npm 模块中的原始 css 字符串。
根据 vite 手册,
https://vitejs.dev/guide/assets.html#importing-asset-as-string
它说我们可以通过在标识符末尾加上“?raw”来获取原始字符串。
所以我试试这个:
import style from "swiper/css/bundle?raw";
但这显示错误如下:
[vite] Internal server error: Missing "./css/bundle?raw" export in "swiper" package
如果我使用这个:
import style from "swiper/css/bundle";
没有错误,但 css 不只是作为字符串加载,而是作为 bundle css 处理。
这不好,因为我想在我的基于 lit 的 Web 组件中使用这个 css。
有没有办法通过vite获取css作为原始字符串?
最佳答案
Evan You(Vite.js 和 Vue.js 创建者)has added the inline
toggle修复了 the problem of styles also being added to the main CSS bundle when importing .
import style from "swiper/css/bundle.css?inline";
此切换可防止您的 CSS 也被捆绑到您的主 CSS 包中,作为将其导入字符串的副作用。
关于css - 通过 vite 导入获取原始字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70982306/