javascript - 如何让 'grunt less' 自动运行 autoprefixer?

标签 javascript less gruntjs vendor-prefix

我有一个带有 less 和 autoprefixer 的工作 Gruntfile。我也有 'grunt watch' 工作正常。

在我使用 autoprefixer 之前,我使用较少的 mixin 作为 vendor 前缀。运行 'grunt less' 将构建带有我所有前缀的工作 CSS。

现在我有了 autoprefixer,但如果我想一次性构建我的样式,我现在必须先运行“grunt less”然后运行“grunt autoprefixer”才能使用带前缀的 CSS。

我如何修改 'grunt less' 使其能够正常工作,再次减少前缀?

I've read the docs, and I know I could add an additional task to do both these things .然而:

  • 'grunt less' 现在没有可用的输出。任务应始终产生可用的输出。
  • 我不想告诉其他人“grunt less”不会产生可用的输出
  • 不需要额外的任务来替换不起作用的任务

即,我只想减少生成工作 CSS(带前缀)

module.exports = function(grunt) {

  // Load Grunt tasks declared in the package.json file
  require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

  // Configure Grunt
  grunt.initConfig({

    less: {
      development: {
        options: {
          paths: ["./less"],
          yuicompress: false
        },
        files: {
          "./public/css/style.css": "./public/less/style.less"
        }
      }
    },

    autoprefixer: {
      development: {
        browsers: ['last 2 version', 'ie 9'],
        expand: true,
        flatten: true,
        src: 'public/css/*.css',
        dest: 'public/css'
      }
    },

    watch: {
      less: {
        files: ["./public/less/*"],
        tasks: ["less", "autoprefixer:development"],
        options: {
          livereload: true
        }
      }
    },

  });


};

最佳答案

使用 autoprefixer 作为 LESS 的插件, 你必须安装 npm-package less-plugin-autoprefix:

npm install grunt-contrib-less less-plugin-autoprefix --save-dev

Gruntfile.js

module.exports = function(grunt) {
  "use strict";
  var gruntConfig = {
    less : {
      options : {
        plugins : [ new (require('less-plugin-autoprefix'))({browsers : [ "last 2 versions" ]}) ]
      },
      main : {
        files: {
          'src/css/desktop/bootstrap-theme.css' : 'src/less/desktop/bootstrap-theme.less',
          'src/css/desktop/company.css' : 'src/less/desktop/company.less',
          'src/css/desktop/index.css' : 'src/less/desktop/index.less',
          'src/css/desktop/login.css' : 'src/less/desktop/login.less'
        }
      }
    }
  };

  grunt.initConfig(gruntConfig);
  grunt.loadNpmTasks('grunt-contrib-less');
  grunt.registerTask('default', [ 'less' ]);
};

关于javascript - 如何让 'grunt less' 自动运行 autoprefixer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20374839/

相关文章:

css - 链接到 LESS css 时拒绝访问受限 URI

css - 支持 RTL 的通用 DotLess .box-shadow 混合?

html - yeoman webapp 子文件夹错误的 css 路径

gruntjs - 更改 grunt jshint 的选项

javascript - 如何排除特定元素的 Jquery mouseup

javascript - 关于使用数组的高阶函数进行正则表达式全局搜索的问题

javascript - 将过滤器应用于 observableArray?

css - Mixin 场景,尝试转换 css

javascript - 硫化 polymer 一次性绑定(bind) src 属性

javascript - 中心 d3.js JavaScript 绘图