Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Grunt:当函数不能拆分为不同的任务时,如何依次运行一个函数、一个任务和另一个函数?_Javascript_Node.js_Gruntjs_Scheduled Tasks - Fatal编程技术网

Javascript Grunt:当函数不能拆分为不同的任务时,如何依次运行一个函数、一个任务和另一个函数?

Javascript Grunt:当函数不能拆分为不同的任务时,如何依次运行一个函数、一个任务和另一个函数?,javascript,node.js,gruntjs,scheduled-tasks,Javascript,Node.js,Gruntjs,Scheduled Tasks,我正在使用Grunt生成一个构建。我不熟悉Grunt、javascript和nodejs,因此欢迎任何新的观点 我的GrunFile中的一些任务依赖于插件(例如用于javascript缩小的“uglify”),而其他一些工作流最适合自己编写javascript函数 我经常遇到需要执行以下操作的情况:(1)执行javascript函数(2)在该函数之后立即运行grunt任务(3)执行另一个javascript函数。它需要按照这个顺序发生。然而,由于Grunt只是一个任务调度器,它最终运行(1)、排

我正在使用Grunt生成一个构建。我不熟悉Grunt、javascript和nodejs,因此欢迎任何新的观点

我的GrunFile中的一些任务依赖于插件(例如用于javascript缩小的“uglify”),而其他一些工作流最适合自己编写javascript函数

我经常遇到需要执行以下操作的情况:(1)执行javascript函数(2)在该函数之后立即运行grunt任务(3)执行另一个javascript函数。它需要按照这个顺序发生。然而,由于Grunt只是一个任务调度器,它最终运行(1)、排队(2)、运行(3),然后在(1)和(3)完成后作为最后一步运行(2)

下面是一个假设的自定义任务的非常简单的用例,以便更好地解释这种情况

grunt.task.registerTask('minifyJS', function() {

    jsFilepathMapping = configureUglifiy();
    /** note - configureUglify is needed because the minification filepaths
        are generated on the fly, I do not know them before the script runs
        and more than that, there are so many that it would be really bulky
        to create init targets for each single minification file that needs
        to be generated.*/

    grunt.task.run('uglify');

    updateJsScriptTags(jsFilepathMapping); // update the <script> tags in my HTML

});
vs

我发现这会使事情变得更不可读,而不是一个简单的函数,它接受要使用的参数,并且可以强制使用特定的结构。如果我在一个任务中修改某个全局参数中的一组属性,而这些属性稍后会在另一个任务中使用,那么从另一个角度来看,似乎更容易出现问题。更麻烦的是,共享的属性名是硬编码的。。我知道这是一个非常简单的用例,但是开始想象一组更复杂的函数,它们依赖于多个参数,这些参数可能是复杂的数据类型,这就是我关注的地方

作为一个简短的总结:除了实现函数/插件任务/函数/插件任务的顺序排序,而不必将函数本身转换为自定义任务,还有其他方法吗?

因此,作为一个简短的总结:除了实现函数/插件任务/函数/插件任务的顺序排序,而不必将函数本身转换为自定义任务,还有其他方法吗

简短回答:否,要维护执行顺序,需要使用grunt任务而不是普通JavaScript函数定义顺序

任务列表
数组中定义的每个任务的顺序执行顺序排序。例如,当使用
grunt.registerTask
时:

grunt.registerTask('foo',['a','b','c']);
给出上面的伪示例,运行
foo
任务将首先运行Task
a
,然后运行Task
b
,依此类推(即Task
b
直到Task
a
完成才会运行;Task
c
直到Task
b
完成才会运行)

然而,这并不意味着不能将普通JavaScript函数与grunt结合使用


带解决方案的长答案:

在Grunt任务之间共享数据似乎没有什么好办法

您可以使用
对象
来存储数据。第一个示例中的伪代码意味着您希望
configureUglifiy
函数:

  • 动态配置
    Uglify
    任务
  • 返回由
    configureUglifiy
    自身生成的数据(对象
  • 然后将返回的数据作为参数传递给
    updatejscripttags
    函数
  • 因此,不要从
    configureUglifiy
    函数返回
    对象。您可以将其存储在另一个
    对象中,然后在
    updatejscripttags
    函数中访问该对象

    在下面的要点中,请注意
    共享的
    对象,该对象的属性/键名为
    jsFilepathMapping
    。我们将使用这个对象来存储动态生成的数据,这些数据可以在另一个任务中访问

    Gruntfile.js(伪代码)

    module.exports=函数(grunt){
    "严格使用",;
    var共享={
    jsFilepathMapping:{}//
    
    /** 
        documentation which defines what the argment filepathMapping is
    
        Furthermore, since the function takes an argument, the context is
        immediately clearer just looking at the function declaration.
    */
    function updateJsScriptTags(filepathMapping) {
    
        // do stuff ...
        for ( key in filepathMapping ) { // oh cool!  i know what this arg does, my documentation nicely explains it, and its structure too
             // lots of stuff
        }
    }
    
    ...
    // and elsewhere in the script, where it's being invoked:
    
    var aMapping = someFunc();
    updateJsScriptTags(aMapping);
    
    grunt.task.registerTask('updateJsScriptTags', 'update js tags', function() {
    
       // do stuff.
       ...
    
        // many lines later:
        grunt.options('filepathMapping') // Oh, what is this attribute?  Let me go look around the rest of the script to find out where it comes from
    
    }
    
    ...
    // and ultimately, where it's being invoked.
    grunt.task.run('someTask'); // global options param gets modified somewhere in here, but you'd never know it looking at this line of code
    grunt.task.run('updateJsScriptTags'); // this task will depend on that modification