在webpack中添加autoprefixer

时间:2018-03-22 08:21:50

标签: webpack sass

我的webpack.config.js文件:

var path = require("path"),
    src = path.resolve(__dirname, "src"),
    dist = path.resolve(__dirname, "dist"),
    webpack = require("webpack");

var HtmlWebpackPlugin = require("html-webpack-plugin");
var ExtractPlugin = require("extract-text-webpack-plugin"),
    CleanWebpackPlugin = require("clean-webpack-plugin"),
    CopyWebpackPlugin = require("copy-webpack-plugin");

var extractCssPlugin = new ExtractPlugin({
  filename: "css/main.min.css"
});

var extractHtmlPlugin = new ExtractPlugin({
  filename: "[name].html"
});

module.exports = {
  entry: {
    bundle: src + "/app/app.js"
  },
  output: {
    path: dist,
    filename: 'js/[name].min.js'
  },
  devtool: 'source-map',
  module: {
    rules: [
      {
                test: /\.js?/,
                include: src,
                loader: "babel-loader",
                query:{
                    presets: ["react", "es2015", "stage-2"]
                }
            },
      {
          test: /\.pug$/,
          loaders: ['file-loader?name=[name].html', 'pug-html-loader?pretty&exports=false']
      },
      {
        test: /\.scss$/,
        use: extractCssPlugin.extract({
          use:[
            {
              loader: "css-loader",
              options: {sourceMap: true, minimize: true}
            },
            {
              loader: "sass-loader",
              options: {sourceMap: true}
            },
            "postcss-loader"
          ]
        })
      }
    ]
  },

  plugins: [
    cleanPlugin,
    copyPlugin,
    extractCssPlugin,
    extractHtmlPlugin,
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    })
  ]
}

我在src / css / postcss.config.js中的postcss.config.js

module.exports = {
  plugins: [require("autoprefixer")]
};

的package.json

{
  "name": "test_project",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build:prod": "webpack -p",
    "build:dev": "webpack -d"
  },
  "browserslist": ["last 2 versions"],
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "autoprefixer": "^8.0.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "clean-webpack-plugin": "^0.1.17",
    "copy-webpack-plugin": "^4.3.1",
    "css-loader": "^0.28.9",
    "cssnano": "^3.10.0",
    "extract-loader": "^2.0.1",
    "extract-text-webpack-plugin": "^3.0.2",
    "file-loader": "^1.1.6",
    "html-webpack-plugin": "^2.30.1",
    "node-sass": "^4.7.2",
    "postcss-cssnext": "^3.1.0",
    "postcss-loader": "^2.1.0",
    "pug-html-loader": "^1.1.5",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.19.1",
    "webpack": "^3.10.0",
    "webpack-dev-server": "^2.11.0"
  },
  "dependencies": {
    "postcss-import": "^11.1.0",
    "precss": "^3.1.2"
  }
}

只需将main.scss编译为webpack中的main.css,就可以将autoprefixer添加到我的css代码中。我确实尝试过使用post-css loader,但我无法正确使用它。

1 个答案:

答案 0 :(得分:1)

加载程序的顺序对于webpack非常重要 目前,您首先在使用postcss-loader从javascript加载的所有根文件中添加前缀 然后使用sass-loader解决scss文件中的导入问题 这意味着所有二级进口都不会有前缀。

这是一个简单的解决方法。只需将sass-loader放在首位,您还需要将sourceMap添加到postcss-loader中。

use:[
  {
    loader: "css-loader",
    options: {sourceMap: true, minimize: true}
  },
  {
    loader: "postcss-loader",
    options: {sourceMap: true}
  },
  {
    loader: "sass-loader",
    options: {sourceMap: true}
  },
]