Webpack SCSS和autoprefixer不能一起工作

时间:2016-07-06 07:22:49

标签: compilation sass webpack autoprefixer

使用以下webpack配置进行资产编译我无法使autoprefixer工作。提取的css没有得到所需的前缀。

var webpack = require('webpack'),
    precss       = require('precss'),
    autoprefixer = require('autoprefixer'),
    ExtractTextPlugin = require('extract-text-webpack-plugin'),
    path = require('path');



const sassLoaders = [
    'css-loader!autoprefixer-loader?browsers=last 2 version',
    'postcss-loader',
    'sass-loader?indentedSyntax=sass&includePaths[]=' + path.resolve(__dirname, '.')
]

const config = {
    entry: {
        //nsb: ['./js/nsb']
        dashboard: ['./js/dashboard']
    },
    module: {
        loaders: [
            {
                test: /\.sass$/,
                loader: ExtractTextPlugin.extract('style-loader', sassLoaders.join('!'))
            },
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract('css!sass')
            },
            {
                test:   /\.docs\.css$/,
                loader: "style-loader!css-loader!postcss-loader?pack=cleaner"
            },
            {
                test:   /\.css$/,
                loader: "style-loader!css-loader!postcss-loader"
            },

            { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" },
            { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" }
        ]
    },
    postcss: function () {
        return {
            defaults: [precss, autoprefixer],
            cleaner:  [autoprefixer({ browsers: [] })]
        };
    },
    output: {
        filename: '[name].js',
        path: path.join(__dirname, './build'),
        publicPath: '/bundles/dashboard/build/'
    },
    amd: {jQuery: true},
    plugins: [
        new ExtractTextPlugin('[name].css'),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        }),
        new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js', Infinity)
    ],
    /* postcss: [
        autoprefixer({
            browsers: ['last 2 versions']
        })
    ],
    */
    resolve: {
        alias: {
            jquery: 'node_modules/jquery/dist/jquery.js',
            magnificPopup: 'node_modules/maginific-popup/dist/jquery.magnific-popup.js' //JQuery Plugin
        },

        modulesDirectories: ['./js', 'node_modules'],
        extensions: ['', '.js', '.sass'],
        root: [path.join(__dirname, './')]
    }
}

module.exports = config;

2 个答案:

答案 0 :(得分:0)

css-loader附带了自己的autoprefixer配置,您需要禁用它才能使配置正常工作。因此,无论您拥有css-loader,都需要添加以禁用css-loader autoprefixer。

css-loader?-autoprefixer

可以找到更多信息here& here

所以你的配置看起来像这样

var webpack = require('webpack'),
  precss       = require('precss'),
  autoprefixer = require('autoprefixer'),
  ExtractTextPlugin = require('extract-text-webpack-plugin'),
  path = require('path');



const sassLoaders = [
  'css-loader?-autoprefixer!autoprefixer-loader?browsers=last 2 version',
  'postcss-loader',
  'sass-loader?indentedSyntax=sass&includePaths[]=' + path.resolve(__dirname, '.')
]

const config = {
  entry: {
    //nsb: ['./js/nsb']
    dashboard: ['./js/dashboard']
  },
  module: {
    loaders: [
      {
        test: /\.sass$/,
        loader: ExtractTextPlugin.extract('style-loader', sassLoaders.join('!'))
      },
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract('css!sass')
      },
      {
        test:   /\.docs\.css$/,
        loader: "style-loader!css-loader?-autoprefixer!postcss-loader?pack=cleaner"
      },
      {
        test:   /\.css$/,
        loader: "style-loader!css-loader?-autoprefixer!postcss-loader"
      },

      { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" },
      { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" }
    ]
  },
  postcss: function () {
    return {
      defaults: [precss, autoprefixer],
      cleaner:  [autoprefixer({ browsers: [] })]
    };
  },
  output: {
    filename: '[name].js',
    path: path.join(__dirname, './build'),
    publicPath: '/bundles/dashboard/build/'
  },
  amd: {jQuery: true},
  plugins: [
    new ExtractTextPlugin('[name].css'),
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery"
    }),
    new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js', Infinity)
  ],
  /* postcss: [
    autoprefixer({
    browsers: ['last 2 versions']
    })
    ],
    */
  resolve: {
    alias: {
      jquery: 'node_modules/jquery/dist/jquery.js',
      magnificPopup: 'node_modules/maginific-popup/dist/jquery.magnific-popup.js' //JQuery Plugin
    },

    modulesDirectories: ['./js', 'node_modules'],
    extensions: ['', '.js', '.sass'],
    root: [path.join(__dirname, './')]
  }
}

module.exports = config;

另外,我认为您可以删除autoprefixer-loadersassLoaders,因为您使用的是PostCSS和autoprefixer。

答案 1 :(得分:0)

您可以尝试以下代码,注意在css-loader和postcss-loader之间添加“?-autoprefixer”。

var webpack = require('webpack'),
precss       = require('precss'),
autoprefixer = require('autoprefixer'),
ExtractTextPlugin = require('extract-text-webpack-plugin'),
path = require('path');
const sassLoaders = [
'css-loader!autoprefixer-loader?browsers=last 2 version',
'postcss-loader',
'sass-loader?indentedSyntax=sass&includePaths[]=' + path.resolve(__dirname, 
'.')
]

const config = {
entry: {
    //nsb: ['./js/nsb']
    dashboard: ['./js/dashboard']
},
module: {
    loaders: [
        {
            test: /\.sass$/,
            loader: ExtractTextPlugin.extract('style-loader', sassLoaders.join('!'))
        },
        {
            test: /\.scss$/,
            loader: ExtractTextPlugin.extract('css!sass')
        },
        {
            test:   /\.docs\.css$/,
            loader: "style-loader!css-loader?-autoprefixer!postcss-loader?pack=cleaner"
        },
        {
            test:   /\.css$/,
            loader: "style-loader!css-loader?-autoprefixer!postcss-loader"
        },

        { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" },
        { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" }
    ]
},
postcss: function () {
    return {
        defaults: [precss, autoprefixer],
        cleaner:  [autoprefixer({ browsers: [] })]
    };
},
output: {
    filename: '[name].js',
    path: path.join(__dirname, './build'),
    publicPath: '/bundles/dashboard/build/'
},
amd: {jQuery: true},
plugins: [
    new ExtractTextPlugin('[name].css'),
    new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery"
    }),
    new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js', Infinity)
],
/* postcss: [
    autoprefixer({
        browsers: ['last 2 versions']
    })
],
*/
resolve: {
    alias: {
        jquery: 'node_modules/jquery/dist/jquery.js',
        magnificPopup: 'node_modules/maginific-popup/dist/jquery.magnific-popup.js' //JQuery Plugin
    },

    modulesDirectories: ['./js', 'node_modules'],
    extensions: ['', '.js', '.sass'],
    root: [path.join(__dirname, './')]
}
}

module.exports = config;

还有一点需要注意:在我项目的根目录中,我还有一个postcss.config.js,内容如下:

module.exports = {
parser: 'postcss-scss',
plugins: [
    require('autoprefixer'),
]
}