Javascript Webpack 4、postcss-loader 和 autoprefixer 插件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49782806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Webpack 4, postcss-loader and autoprefixer plugin
提问by doublea
I'm getting quite frustrated trying to get autoprefixer working.
试图让 autoprefixer 工作时,我感到非常沮丧。
Here is my webpack.config.js
这是我的webpack.config.js
const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const config = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.(scss)$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: "css-loader", options: {} },
{ loader: "postcss-loader", options: {} },
// {
// loader: "postcss-loader",
// options: {
// ident: "postcss",
// plugins: (loader) => [
// require('autoprefixer')({ browsers: ['defaults']})
// ]
// }
// },
{ loader: "sass-loader", options: {} }
]
},
{
test: /\.mp3$/,
use: {
loader: "file-loader"
}
}
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
}),
new MiniCssExtractPlugin({
filename: "main.css"
})
]
};
module.exports = config;
Here is my postcss.config.js
这是我的postcss.config.js
module.exports = {
plugins: [
require('autoprefixer')()
]
}
Currently my package.jsonis holding my browserslist options
目前我的package.json持有我的 browserslist 选项
As you can see I've tried using the webpack file to hold my config settings for postcss-loader and I've also tried creating a separate config file.
正如你所看到的,我已经尝试使用 webpack 文件来保存我的 postcss-loader 配置设置,我也尝试创建一个单独的配置文件。
I've tried moving the browserslist but I don't think that's the issue bc I run npx browserslistand I can see a list of browsers that were selected.
我试过移动浏览器列表,但我认为这不是我运行的问题npx browserslist,我可以看到选择的浏览器列表。
My postcss-loaderdeclaration in my webpack config comes after css-loaderand before sass-loader
我postcss-loader在 webpack 配置中的声明css-loader前后出现sass-loader
I also receive no errors in my console when I run webpack so not sure what is happening but could really use some help!
当我运行 webpack 时,我的控制台也没有收到任何错误,所以不确定发生了什么,但真的可以使用一些帮助!
回答by ynceonrudi
Not working but { browsers: ['defaults']}
不工作但是 { browsers: ['defaults']}
Try:
尝试:
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader?modules&importLoaders=1&localIdentName=[local]_[hash:base64:6]',
{
loader: 'postcss-loader',
options: {
plugins: () => [require('autoprefixer')({
'browsers': ['> 1%', 'last 2 versions']
})],
}
},
]
}
or
或者
// postcss.config.js
module.exports = {
plugins: [
require('autoprefixer')({
'browsers': ['> 1%', 'last 2 versions']
})
]
};
回答by altrugon
The big picture here is that you are actually missing the Autoprefixerpackage on your environment, so to fix the problem you have two solutions:
这里的大局是您实际上在您的环境中缺少Autoprefixer包,因此要解决该问题,您有两种解决方案:
Solution 1
解决方案1
npm install --save-dev postcss-loader autoprefixer
Now inside your postcss.config.jsfile you can add something like this:
现在在您的postcss.config.js文件中,您可以添加如下内容:
module.exports = {
plugins: [
require('autoprefixer')({
'browsers': ['> 1%', 'last 2 versions']
})
]
};
Solution 2 (recommended)
解决方案2(推荐)
This one comes from PostCSS Loader documentation, and since you are using this package perhaps this is the recommended way to go.
这个来自PostCSS Loader 文档,因为你正在使用这个包,所以这可能是推荐的方法。
postcss-preset-envincludes autoprefixer, so adding it separately is not necessary if you already use the preset.
postcss-preset-env包含autoprefixer,因此如果您已经使用了预设,则无需单独添加它。
As you can see in order to get Autoprefixer you just need to install PostCSS Preset Env.
正如您所看到的,为了获得 Autoprefixer,您只需要安装PostCSS Preset Env。
npm install --save-dev postcss-loader postcss-preset-env
Now get rid of your postcss.config.jsfile and move that configuration into your webpack.config.jsto make it look to something like this:
现在摆脱你的postcss.config.js文件并将该配置移动到你的webpack.config.js 中,使其看起来像这样:
...
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: "css-loader", options: {} },
{
loader: "postcss-loader",
options: {
ident: 'postcss',
plugins: [
require('autoprefixer')({
'browsers': ['> 1%', 'last 2 versions']
}),
]
}
},
{ loader: "sass-loader", options: {} }
]
}
]
...
I hope this helps, it also took me a long time to figure it out too ;)
我希望这会有所帮助,我也花了很长时间才弄明白;)
回答by dlemm
As mentioned above, you need to specify the browser-list.
But instead of adding this to your webpack.configand to the postcss-configyou can add the following code simply to the package.jsonafter the dependencies. Worked for me.
如上所述,您需要指定浏览器列表。但是webpack.config,postcss-config您可以将以下代码简单地添加到package.json依赖项之后,而不是将其添加到您的和。为我工作。
"browserslist": [
"> 1%",
"last 2 versions"
],
回答by u6176355
Try edit like this
尝试像这样编辑
webpack.config.js:
webpack.config.js:
{
test: /\.scss$/,
use: [
"style-loader", "css-loader", 'postcss-loader', 'resolve-url-loader', 'sass-loader?sourceMap'
]
},
postcss.config.js:
postcss.config.js:
require('autoprefixer')({browsers: ['last 10 versions']}),

