【发布时间】:2023-02-03 02:38:41
【问题描述】:
我想要达到的目标
在所有全局样式表和组件范围样式上的 Astro 项目的 npm run build 上设置自动 autoprefixer 以支持其他浏览器和旧浏览器(大约 2016 年)。
我的构建
- Astro v1.9.1
- 通过 SCSS 编译的 CSS
- 一些 Astro 集成(imagetools、prefetch、compress、NetlifyCMS 是我认为与此问题唯一相关的部分)
我采取的步骤
- 构建了一个 Astro 项目,该项目使用
/src/styles文件夹中的全局样式以及 Astro 组件中的范围样式 - 跑
npm install autoprefixer - 使用以下代码based on the docs创建了一个
.postcss.config.cjs:module.exports = { plugins: [ require('autoprefixer'), ], }; - 跑
npm run build我所期望的
- 为了使用不同的供应商前缀编译我的 CSS 以支持浏览器
- 我在
src/styles/global.css中添加text-size-adjust: 100%;的主要测试是将-webkit-text-size-adjust: 100%;添加到我在dist/assets(构建文件夹)中编译的CSS
我还尝试了什么
- 创建一个
.postcssrc.json其中包含
{ "map": true, "plugins": { "autoprefixer": {} } }- 为我的
astro.config.mjs添加额外的 Vite 配置:
import autoprefixer from "autoprefixer"; export default defineConfig({ vite: { postcss: { plugins: [ autoprefixer({}), // add options if needed ], }, }, })- 将一些浏览器列表条件添加到我的
package.json以设置自动前缀的条件
{ "browserslist": [ "last 2 versions", "not dead", "> 0.2%" ] }结果与结论
- 然而我仍然没有在我的项目中发生自动前缀
- 我也在努力在线或在 Astro Discord 服务器中寻找答案 - 这让我想知道:人们真的不再使用 autoprefixer 了吗?人们如何支持其他供应商和旧浏览器?
非常感谢您的帮助,我很喜欢 Astro!
【问题讨论】:
-
.postcss.config.cjs开头的点是错字吗?因为我认为它不应该在那里。得到它与sass一起工作@加载配置为.json或.cjs,即使我没有真正做任何特别的事情,也可以随意询问我的设置。我也很惊讶地看到几乎没有关于这个话题的讨论 -
哦,非常好,这不是错字,我实际上将文件命名为...感谢您指出这一点!我实际上已经让 autoprefixer 在另一个项目中正常工作,但很快就会重新访问它(因为某些属性没有为我经常需要的旧浏览器添加前缀)。
标签: css postcss autoprefixer astrojs astro