简体   繁体   English

如何使'grunt less'自动运行autoprefixer?

[英]How can I make 'grunt less' automatically run autoprefixer?

I have a working Gruntfile with less and autoprefixer. 我有一个工作Gruntfile少和autoprefixer。 I also have 'grunt watch' working fine. 我也有'咕噜咕噜'的工作正常。

Before I was using autoprefixer, I was using less mixins for vendor prefixes. 在我使用autoprefixer之前,我使用较少的mixins作为供应商前缀。 Running 'grunt less' would build working CSS with all my prefixes. 运行'grunt less'将构建包含所有前缀的工作CSS。

Now I have autoprefixer, but if I want to do a once-off build of my styles, I now have to run 'grunt less' then 'grunt autoprefixer' to get working CSS with prefixes. 现在我有autoprefixer,但是如果我想要一次性构建我的样式,我现在必须运行'grunt less'然后'grunt autoprefixer'来获得带有前缀的CSS。

How can I modify 'grunt less' so it build working, prefixes less again? 如何修改'grunt less'以使其构建工作,前缀更少?

I've read the docs, and I know I could add an additional task to do both these things . 我已经阅读了文档,我知道我可以添加额外的任务来完成这两件事 However: 然而:

  • 'grunt less' now doesn't have usable output. 'grunt less'现在没有可用的输出。 A task should always produce usable output. 任务应始终生成可用的输出。
  • I don't want to have to tell other people that 'grunt less' doesn't produce usable output 我不想告诉其他人'咕噜咕噜'不会产生可用的输出
  • An additional task should not be required to replace one that doesn't work 不需要额外的任务来替换不起作用的任务

Ie, I just want grunt less to produce working CSS (with prefixes) . 也就是说, 我只想减少咕噜声来制作有效的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
        }
      }
    },

  });


};

For using autoprefixer as plugin for LESS , you must install npm-package less-plugin-autoprefix : 要使用autoprefixer作为LESS的插件,必须安装npm-package less-plugin-autoprefix

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

Gruntfile.js 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' ]);
};

Grunt can't do what you describe in the question as tasks do not know about each other inherently. Grunt无法完成您在问题中描述的内容,因为任务本身并不了解彼此。 You have to glue tasks together using aliases, (easy) or functions (for when you want a bit more control), but there's no way of modifying the way one of these tasks behaves without changing the source. 您必须使用别名,(简单)或函数(当您需要更多控制时)将任务粘合在一起,但是无法在不更改源的情况下修改其中一个任务的行为方式。

As an example, you could fork grunt-contrib-less and add the code to run the auto prefixing directly into the task, around here: https://github.com/gruntjs/grunt-contrib-less/blob/master/tasks/less.js#L69 - inject this line here https://github.com/nDmitry/grunt-autoprefixer/blob/master/tasks/autoprefixer.js#L56 and then use your fork instead of the official plugin. 举个例子,你可以派grunt-contrib-less并添加代码来直接运行自动前缀到这里的任务: https//github.com/gruntjs/grunt-contrib-less/blob/master/tasks /less.js#L69 - 在这里注入这一行https://github.com/nDmitry/grunt-autoprefixer/blob/master/tasks/autoprefixer.js#L56然后使用你的fork而不是官方插件。

The easiest and best way is to register a new task that does the job of these two tasks but in the one command, ie: 最简单和最好的方法是注册一个新任务,完成这两个任务的工作但在一个命令中,即:

grunt.registerTask('buildless', ['less', 'autoprefixer']);

I do this with all my own tasks - SASS compilation, JS concat + uglify, webfont generation etc. Just tell the others in your team to run those tasks and not grunt less or grunt autoprefixer on their own. 我用自己的所有任务完成这项任务 - SASS编译,JS concat + uglify,webfont生成等。只需告诉团队中的其他人运行这些任务,而不是自己grunt lessgrunt autoprefixer

Better still, use my Grunt plugin available tasks. 更好的是, 使用我的Grunt插件可用任务。 With this (and the filter config) you will be able to produce a trimmed down list of tasks whenever someone runs grunt availabletasks although I prefer to alias this with tasks for quicker typing. 有了这个(以及过滤器配置),只要有人运行grunt availabletasks任务,您就可以生成一个精简的任务列表,尽管我更喜欢将其替换为快速键入的tasks If you're like me and have been bitten by the automation bug (and have registered loads of plugins in your Gruntfile), this can really help a newcomer to the project with which tasks should be run. 如果你像我一样并且被自动化错误所困扰(并且已经在Gruntfile中注册了大量的插件),这确实可以帮助新项目运行任务的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM