如何在vue-loader

时间:2017-03-17 13:17:51

标签: javascript webpack vue.js postcss

我正在尝试使用Vue2,并在我的应用中遇到使用丢失网格(http://lostgrid.org/)的问题。 Lost-grid是一个postcss网格系统,我已经能够在webpack2的反应应用程序中多次使用它。

如何在vue-loader中使用lost-grid i?我已经安装了“失去的”#39;通过npm,并尝试了添加postcss加载器的不同方法。似乎没什么用,文档也没有用。

这是webpack.base.conf.js的业务部分(正如我所说,所有css加载都是通过vue-loader运行的):

module: {
    rules: [
      {
        test: /\.(js|vue)$/,
        loader: 'eslint-loader',
        enforce: "pre",
        include: [resolve('src'), resolve('test')],
        options: {
          formatter: require('eslint-friendly-formatter')
        }
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {

...

vue-loader.conf.js看起来像:

var utils = require('./utils')
var config = require('../config')
var isProduction = process.env.NODE_ENV === 'production'

module.exports = {
  loaders: utils.cssLoaders({
    sourceMap: isProduction
      ? config.build.productionSourceMap
      : config.dev.cssSourceMap,
    extract: isProduction
  })
}

utils.js看起来像这样:

var path = require('path')
var config = require('../config')
var ExtractTextPlugin = require('extract-text-webpack-plugin')

exports.assetsPath = function (_path) {
  var assetsSubDirectory = process.env.NODE_ENV === 'production'
    ? config.build.assetsSubDirectory
    : config.dev.assetsSubDirectory
  return path.posix.join(assetsSubDirectory, _path)
}

exports.cssLoaders = function (options) {
  options = options || {}

  var cssLoader = {
    loader: 'css-loader',
    options: {
      minimize: process.env.NODE_ENV === 'production',
      sourceMap: options.sourceMap
    }
  }

  // generate loader string to be used with extract text plugin
  function generateLoaders (loader, loaderOptions) {
    var loaders = [cssLoader]
    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }

    // Extract CSS when that option is specified
    // (which is the case during production build)
    if (options.extract) {
      return ExtractTextPlugin.extract({
        use: loaders,
        fallback: 'vue-style-loader'
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }
  }

  // http://vuejs.github.io/vue-loader/en/configurations/extract-css.html
  return {
    css: generateLoaders(),
    postcss: generateLoaders(''),
    less: generateLoaders('less'),
    sass: generateLoaders('sass', { indentedSyntax: true }),
    scss: generateLoaders('sass'),
    stylus: generateLoaders('stylus'),
    styl: generateLoaders('stylus')
  }
}

// Generate loaders for standalone style files (outside of .vue)
exports.styleLoaders = function (options) {
  var output = []
  var loaders = exports.cssLoaders(options)
  for (var extension in loaders) {
    var loader = loaders[extension]
    output.push({
      test: new RegExp('\\.' + extension + '$'),
      use: loader
    })
  }
  return output
}

现在的问题是......我在哪里放什么?任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

将以下行添加到build /

中的vue-loader.config.js文件中
postcss: [
    require('autoprefixer')({
        browsers: ['last 7 versions']
    })
]

所以它看起来像这样:

 module.exports = {
  loaders: utils.cssLoaders({
    sourceMap: isProduction ? config.build.productionSourceMap : config.dev.cssSourceMap,
    extract: isProduction
  }),
  postcss: [
    require('autoprefixer')({
      browsers: ['last 7 versions']
    })
  ]
}

答案 1 :(得分:1)

require('lost')应该像上面那样添加到postcss数组

答案 2 :(得分:-1)

“browserslist”:[         “> 1%”,         “最后2个版本”,         “不是< = 8”     ]

在package.json中有定义,你可以看到https://github.com/ai/browserslist#config-file