zoukankan      html  css  js  c++  java
  • grunt入门讲解4:如何创建task(任务)

    每当运行Grunt时, 你可以为其指定一个或多个任务, 这些任务用于告诉Grunt你想要它做什么事情。

    如果你没有指定一个任务,并且你已经定义一个名为 "default" 的任务,那么该任务将会默认被执行。

    任务别名

    如果指定了一个任务列表taskList,新任务名taskName将是这一个或多个指定任务的别名。当运行此 "任务别名" 时,在taskList 中指定的每个任务都会按照其出现的顺序依次执行。taskList参数必须是一个任务数组。grunt.registerTask(taskName, taskList)

    举个例子:

    grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);

    上面的任务别名案例中定义了一个 'default' 任务,如果运行Grunt时没有指定任何任务,它将自动执行'jshint'、'qunit'、'concat' 和 'uglify' 任务。

    还可以给任务指定参数。在下面的例子中,别名 "dist" 将执行 "concat" 和 "uglify" 两个任务,并且它们都带有一个 "dist" 参数:

    grunt.registerTask('dist', ['concat:dist', 'uglify:dist']);

    多任务

    当运行一个多任务时,Grunt会自动从项目的配置对象中查找同名属性。多任务可以有多个配置,并且可以使用任意命名的'targets'。举个例子:

    grunt.initConfig({
      log: {
        foo: [1, 2, 3],
        bar: 'hello world',
        baz: false
      }
    });
    
    grunt.registerMultiTask('log', 'Log stuff.', function() {
      grunt.log.writeln(this.target + ': ' + this.data);
    });

    如果通过grunt log:foo运行Grunt,它会输出foo: 1,2,3;如果通过grunt log:bar来运行Grunt, 它会输出bar: hello world。然而如果通过grunt log运行Grunt, 它会输出foo: 1,2,3,然后是bar: hello world,最后是baz: false(任务目标会按照指定的顺序进行处理)。

    "基本" 任务

    当一个基本任务执行时,Grunt并不会检查配置和环境 -- 它仅仅执行指定的任务函数,并传递任何使用冒号分割的参数作为函数的参数。举个例子:

    grunt.registerTask('foo', 'A sample task that logs stuff.', function(arg1, arg2) {
      if (arguments.length === 0) {
        grunt.log.writeln(this.name + ", no args");
      } else {
        grunt.log.writeln(this.name + ", " + arg1 + " " + arg2);
      }
    });

    如果执行 grunt foo:testing:123,将输出日志 foo, testing 123。 如果执行这个任务时不传递参数,只是执行 grunt foo,那么将输出日志 foo, no args

    自定义任务

    如果你的任务没有遵循 "多任务" 结构,那就可以使用自定义任务。比如:

    grunt.registerTask('default', 'My "default" task description.', function() {
      grunt.log.writeln('Currently running the "default" task.');
    });

    任务的其他特性

    在一个任务内部,你可以执行其他的任务。比如:

    grunt.registerTask('foo', 'My "foo" task.', function() {
      // Enqueue "bar" and "baz" tasks, to run after "foo" finishes, in-order.
      grunt.task.run('bar', 'baz');
      // Or:
      grunt.task.run(['bar', 'baz']);
    });

    任务也可以是异步的。比如:

    grunt.registerTask('asyncfoo', 'My "asyncfoo" task.', function() {
      // Force task into async mode and grab a handle to the "done" function.
      var done = this.async();
      // Run some sync stuff.
      grunt.log.writeln('Processing task...');
      // And some async stuff.
      setTimeout(function() {
        grunt.log.writeln('All done!');
        done();
      }, 1000);
    });

    任务也可以访问它们自身名称和参数。比如:

    grunt.registerTask('foo', 'My "foo" task.', function(a, b) {
      grunt.log.writeln(this.name, a, b);
    });
    
    // 用法:
    // grunt foo
    //   logs: "foo", undefined, undefined
    // grunt foo:bar
    //   logs: "foo", "bar", undefined
    // grunt foo:bar:baz
    //   logs: "foo", "bar", "baz"

    如果任务在执行时记录到任何错误,那么任务就会失败。当任务失败时,所有的后续任务都将终止。比如:

    grunt.registerTask('foo', 'My "foo" task.', function() {
      return false;
    });
    
    grunt.registerTask('bar', 'My "bar" task.', function() {
      // 如果"foo"任务运行失败或者没有运行则任务失败。
      grunt.task.requires('foo');
      // 如果"foo"任务运行成功则执行这里的代码。
      grunt.log.writeln('Hello, world.');
    });
    
    // 用法:
    // grunt foo bar
    //   没有输出,bar任务没有运行,因为"foo"失败,所以它后面的任务都将终止运行。
    // grunt bar
    //   没有输出,bar任务运行,但运动到一半就停止了,因为"foo"从未运行,所以foo任务失败,失败后,bar任务也就停止了,因此没有输出。

    注意 grunt.task.requires 并不会真正的运行其他任务,它仅仅检查其它任务是否已经执行,并且没有失败。

    任务还可以访问配置属性。比如:

    grunt.registerTask('foo', 'My "foo" task.', function() {
      // 记录属性值,如果属性未定义(undefined)则返回null。
      grunt.log.writeln('The meta.name property is: ' + grunt.config('meta.name'));
      // 同样的记录属性值,如果属性未定义(undefined)则返回null。
      grunt.log.writeln('The meta.name property is: ' + grunt.config(['meta', 'name']));
    });

    如果任务需要的配置属性不存在,其也会失败。比如:

    grunt.registerTask('foo', 'My "foo" task.', function() {
      // 如果缺少"meta.name"配置属性则任务失败。
      grunt.config.requires('meta.name');
      // 如果缺少"mata.name"配置属性则任务同样失败。
      grunt.config.requires(['meta', 'name']);
      // 根据情况记录日志。
      grunt.log.writeln('This will only log if meta.name is defined in the config.');
    });

    为什么我的异步task没有完成?
    如果发生这种情况,可能是由于你忘记调用 this.async 方法来告诉Grunt你的任务是异步的。为了简单起见,Grunt使用同步的编码风格,可以在task体中通过调用 this.async() 将其转换为异步的。

    注意,传递 false 给 done() 函数就会告诉Grunt你的任务已经失败。

    例如:

    grunt.registerTask('asyncme', 'My asynchronous task.', function() {
      var done = this.async();
      setTimeout(function() { 
        done(false); //代表asyncme任务失败
     }, 1000);
    });

    加油!

  • 相关阅读:
    MySQL Connection排查未授权用户(unauthenticated user)
    Git常用命令
    abc
    更新显卡驱动
    财务模块 采购、接收、应付会计分录和功能认识
    ebs界面颜色改变
    获取科目的描述 创建科目
    EBS总账(GL)模块常用表
    收款 付款 到总帐
    PO收料到入库的分析
  • 原文地址:https://www.cnblogs.com/chaojidan/p/4239098.html
Copyright © 2011-2022 走看看