Javascript Grunt-是否可以将全局自定义函数传递给Grunt.loadTasks加载的外部任务?

Javascript Grunt-是否可以将全局自定义函数传递给Grunt.loadTasks加载的外部任务?,javascript,gruntjs,task,Javascript,Gruntjs,Task,当任务在同一个gruntfile.js中定义时,我有一些自定义函数可以很好地工作,但是我的gruntfile.js现在有2000多行,而且很难维护。是否可以全局使用函数,而不必在每个任务文件中重新定义它们?。当从外部任务调用它们时,这两个函数都不起作用。我只是得到了未定义的错误函数 我有下一个结构 gruntfile.js grunt/tasks/functions.js grunt/tasks/styles.js 文件内容如下: grunfile.js module.exports =

当任务在同一个gruntfile.js中定义时,我有一些自定义函数可以很好地工作,但是我的gruntfile.js现在有2000多行,而且很难维护。是否可以全局使用函数,而不必在每个任务文件中重新定义它们?。当从外部任务调用它们时,这两个函数都不起作用。我只是得到了未定义的错误函数

我有下一个结构

gruntfile.js

grunt/tasks/functions.js

grunt/tasks/styles.js
文件内容如下:

grunfile.js

module.exports = function(grunt) {
    require('jit-grunt')(grunt)

    function globalFunctionOne(param1) {
       console.log('yay it works from main file');
    }

    grunt.initConfig({});

    console.log(grunt.config());

    grunt.loadTasks('grunt/tasks')

    grunt.registerTask('default', ['sass:dist'];
}
module.exports = function(grunt) {
    function globalFunctionTwo(param1) {
        console.log('yay it works from partial file');
    }
}
function init(grunt) {
    "use strict";
    function loadConfig(pattern) {
        let config = {},
            fileName,
            fileData;

        grunt.file.expand(pattern).forEach(function (filePath) {
            fileName = filePath.split('/').pop().split('.')[0];
            fileData = require('./' + filePath)(grunt);
            config[fileName] = fileData;
        });

        return config;
    }

    function loadGrunt() {
        const config = {
            pkg: grunt.file.readJSON('package.json')
        };

        require('load-grunt-tasks')(grunt);

        if (grunt.file.exists('grunt/tasks')) {
            grunt.log.writeln('task directory found, loading tasks...');
            grunt.loadTasks('grunt/tasks');
        }

        grunt.util._.extend(config, loadConfig('grunt/configs/**/*.js'));

        grunt.initConfig(config);
    }
    loadGrunt();

}

module.exports = init;
module.exports = function (grunt) {
    "use strict";
    return {
        base: {
            options: {
                process: function (content) {
                    return grunt.template.process(content);
                }
            },
            src: 'grunt/templates/base.url.js',
            dest: 'www/source/config/base.url.js'
        },
        pluginManifest: {
            src: 'cordova/template/package.json',
            dest: '<%= grunt.task.current.args[0] %>/package.json'
        },
        splashScreens: {
            expand: true,
            cwd:"grunt/templates/",
            src: "screen/**",
            dest: '<%= create.dest %>/res/'
        }
    };
};
module.exports = function (grunt) {
  const helpers = require("../helpers.js)(grunt);
  return {
    options: {
      implementation: sass,
      includePaths: helpers.globalFunctionOne('dev'),
      outputStyle: 'expanded',
      sourceMap: true
    },
    dist: {
      files: {
        globalFunctionTwo('dist'),
      }
    }
  };
};
module.exports = function (grunt) {
  function globalFunctionOne(param1) {
    console.log('yay it works from main file');
  }

  function globalFunctionTwo(param1) {
    console.log('yay it works from partial file');
  }

  return {
    globalFunctionOne,
    globalFunctionTwo
  }; 
};
functions.js

module.exports = function(grunt) {
    require('jit-grunt')(grunt)

    function globalFunctionOne(param1) {
       console.log('yay it works from main file');
    }

    grunt.initConfig({});

    console.log(grunt.config());

    grunt.loadTasks('grunt/tasks')

    grunt.registerTask('default', ['sass:dist'];
}
module.exports = function(grunt) {
    function globalFunctionTwo(param1) {
        console.log('yay it works from partial file');
    }
}
function init(grunt) {
    "use strict";
    function loadConfig(pattern) {
        let config = {},
            fileName,
            fileData;

        grunt.file.expand(pattern).forEach(function (filePath) {
            fileName = filePath.split('/').pop().split('.')[0];
            fileData = require('./' + filePath)(grunt);
            config[fileName] = fileData;
        });

        return config;
    }

    function loadGrunt() {
        const config = {
            pkg: grunt.file.readJSON('package.json')
        };

        require('load-grunt-tasks')(grunt);

        if (grunt.file.exists('grunt/tasks')) {
            grunt.log.writeln('task directory found, loading tasks...');
            grunt.loadTasks('grunt/tasks');
        }

        grunt.util._.extend(config, loadConfig('grunt/configs/**/*.js'));

        grunt.initConfig(config);
    }
    loadGrunt();

}

module.exports = init;
module.exports = function (grunt) {
    "use strict";
    return {
        base: {
            options: {
                process: function (content) {
                    return grunt.template.process(content);
                }
            },
            src: 'grunt/templates/base.url.js',
            dest: 'www/source/config/base.url.js'
        },
        pluginManifest: {
            src: 'cordova/template/package.json',
            dest: '<%= grunt.task.current.args[0] %>/package.json'
        },
        splashScreens: {
            expand: true,
            cwd:"grunt/templates/",
            src: "screen/**",
            dest: '<%= create.dest %>/res/'
        }
    };
};
module.exports = function (grunt) {
  const helpers = require("../helpers.js)(grunt);
  return {
    options: {
      implementation: sass,
      includePaths: helpers.globalFunctionOne('dev'),
      outputStyle: 'expanded',
      sourceMap: true
    },
    dist: {
      files: {
        globalFunctionTwo('dist'),
      }
    }
  };
};
module.exports = function (grunt) {
  function globalFunctionOne(param1) {
    console.log('yay it works from main file');
  }

  function globalFunctionTwo(param1) {
    console.log('yay it works from partial file');
  }

  return {
    globalFunctionOne,
    globalFunctionTwo
  }; 
};
styles.js

module.exports = function(grunt) {
    require('jit-grunt')(grunt)

    function globalFunctionOne(param1) {
       console.log('yay it works from main file');
    }

    grunt.initConfig({});

    console.log(grunt.config());

    grunt.loadTasks('grunt/tasks')

    grunt.registerTask('default', ['sass:dist'];
}
module.exports = function(grunt) {
    function globalFunctionTwo(param1) {
        console.log('yay it works from partial file');
    }
}
function init(grunt) {
    "use strict";
    function loadConfig(pattern) {
        let config = {},
            fileName,
            fileData;

        grunt.file.expand(pattern).forEach(function (filePath) {
            fileName = filePath.split('/').pop().split('.')[0];
            fileData = require('./' + filePath)(grunt);
            config[fileName] = fileData;
        });

        return config;
    }

    function loadGrunt() {
        const config = {
            pkg: grunt.file.readJSON('package.json')
        };

        require('load-grunt-tasks')(grunt);

        if (grunt.file.exists('grunt/tasks')) {
            grunt.log.writeln('task directory found, loading tasks...');
            grunt.loadTasks('grunt/tasks');
        }

        grunt.util._.extend(config, loadConfig('grunt/configs/**/*.js'));

        grunt.initConfig(config);
    }
    loadGrunt();

}

module.exports = init;
module.exports = function (grunt) {
    "use strict";
    return {
        base: {
            options: {
                process: function (content) {
                    return grunt.template.process(content);
                }
            },
            src: 'grunt/templates/base.url.js',
            dest: 'www/source/config/base.url.js'
        },
        pluginManifest: {
            src: 'cordova/template/package.json',
            dest: '<%= grunt.task.current.args[0] %>/package.json'
        },
        splashScreens: {
            expand: true,
            cwd:"grunt/templates/",
            src: "screen/**",
            dest: '<%= create.dest %>/res/'
        }
    };
};
module.exports = function (grunt) {
  const helpers = require("../helpers.js)(grunt);
  return {
    options: {
      implementation: sass,
      includePaths: helpers.globalFunctionOne('dev'),
      outputStyle: 'expanded',
      sourceMap: true
    },
    dist: {
      files: {
        globalFunctionTwo('dist'),
      }
    }
  };
};
module.exports = function (grunt) {
  function globalFunctionOne(param1) {
    console.log('yay it works from main file');
  }

  function globalFunctionTwo(param1) {
    console.log('yay it works from partial file');
  }

  return {
    globalFunctionOne,
    globalFunctionTwo
  }; 
};

2000行?那是一个很长的文件。以下是一个gruntfile,您可以采用它将代码分解为更模块化的部分:

grunfile.js

module.exports = function(grunt) {
    require('jit-grunt')(grunt)

    function globalFunctionOne(param1) {
       console.log('yay it works from main file');
    }

    grunt.initConfig({});

    console.log(grunt.config());

    grunt.loadTasks('grunt/tasks')

    grunt.registerTask('default', ['sass:dist'];
}
module.exports = function(grunt) {
    function globalFunctionTwo(param1) {
        console.log('yay it works from partial file');
    }
}
function init(grunt) {
    "use strict";
    function loadConfig(pattern) {
        let config = {},
            fileName,
            fileData;

        grunt.file.expand(pattern).forEach(function (filePath) {
            fileName = filePath.split('/').pop().split('.')[0];
            fileData = require('./' + filePath)(grunt);
            config[fileName] = fileData;
        });

        return config;
    }

    function loadGrunt() {
        const config = {
            pkg: grunt.file.readJSON('package.json')
        };

        require('load-grunt-tasks')(grunt);

        if (grunt.file.exists('grunt/tasks')) {
            grunt.log.writeln('task directory found, loading tasks...');
            grunt.loadTasks('grunt/tasks');
        }

        grunt.util._.extend(config, loadConfig('grunt/configs/**/*.js'));

        grunt.initConfig(config);
    }
    loadGrunt();

}

module.exports = init;
module.exports = function (grunt) {
    "use strict";
    return {
        base: {
            options: {
                process: function (content) {
                    return grunt.template.process(content);
                }
            },
            src: 'grunt/templates/base.url.js',
            dest: 'www/source/config/base.url.js'
        },
        pluginManifest: {
            src: 'cordova/template/package.json',
            dest: '<%= grunt.task.current.args[0] %>/package.json'
        },
        splashScreens: {
            expand: true,
            cwd:"grunt/templates/",
            src: "screen/**",
            dest: '<%= create.dest %>/res/'
        }
    };
};
module.exports = function (grunt) {
  const helpers = require("../helpers.js)(grunt);
  return {
    options: {
      implementation: sass,
      includePaths: helpers.globalFunctionOne('dev'),
      outputStyle: 'expanded',
      sourceMap: true
    },
    dist: {
      files: {
        globalFunctionTwo('dist'),
      }
    }
  };
};
module.exports = function (grunt) {
  function globalFunctionOne(param1) {
    console.log('yay it works from main file');
  }

  function globalFunctionTwo(param1) {
    console.log('yay it works from partial file');
  }

  return {
    globalFunctionOne,
    globalFunctionTwo
  }; 
};
这个gruntfile的作用是:

  • 它动态创建一个配置对象,供grunt通过加载来使用 在
    项目根/grunt/configs
    中找到的每个
    .js
    文件。名字 每个文件的属性对应于grunt配置对象的键
  • 它从
    项目根/grunt/tasks
如果您的配置使用了
grunt contrib copy
,那么在您的项目中,您将有一个类似于以下内容的配置文件:

项目根/grunt/configs/copy.js

module.exports = function(grunt) {
    require('jit-grunt')(grunt)

    function globalFunctionOne(param1) {
       console.log('yay it works from main file');
    }

    grunt.initConfig({});

    console.log(grunt.config());

    grunt.loadTasks('grunt/tasks')

    grunt.registerTask('default', ['sass:dist'];
}
module.exports = function(grunt) {
    function globalFunctionTwo(param1) {
        console.log('yay it works from partial file');
    }
}
function init(grunt) {
    "use strict";
    function loadConfig(pattern) {
        let config = {},
            fileName,
            fileData;

        grunt.file.expand(pattern).forEach(function (filePath) {
            fileName = filePath.split('/').pop().split('.')[0];
            fileData = require('./' + filePath)(grunt);
            config[fileName] = fileData;
        });

        return config;
    }

    function loadGrunt() {
        const config = {
            pkg: grunt.file.readJSON('package.json')
        };

        require('load-grunt-tasks')(grunt);

        if (grunt.file.exists('grunt/tasks')) {
            grunt.log.writeln('task directory found, loading tasks...');
            grunt.loadTasks('grunt/tasks');
        }

        grunt.util._.extend(config, loadConfig('grunt/configs/**/*.js'));

        grunt.initConfig(config);
    }
    loadGrunt();

}

module.exports = init;
module.exports = function (grunt) {
    "use strict";
    return {
        base: {
            options: {
                process: function (content) {
                    return grunt.template.process(content);
                }
            },
            src: 'grunt/templates/base.url.js',
            dest: 'www/source/config/base.url.js'
        },
        pluginManifest: {
            src: 'cordova/template/package.json',
            dest: '<%= grunt.task.current.args[0] %>/package.json'
        },
        splashScreens: {
            expand: true,
            cwd:"grunt/templates/",
            src: "screen/**",
            dest: '<%= create.dest %>/res/'
        }
    };
};
module.exports = function (grunt) {
  const helpers = require("../helpers.js)(grunt);
  return {
    options: {
      implementation: sass,
      includePaths: helpers.globalFunctionOne('dev'),
      outputStyle: 'expanded',
      sourceMap: true
    },
    dist: {
      files: {
        globalFunctionTwo('dist'),
      }
    }
  };
};
module.exports = function (grunt) {
  function globalFunctionOne(param1) {
    console.log('yay it works from main file');
  }

  function globalFunctionTwo(param1) {
    console.log('yay it works from partial file');
  }

  return {
    globalFunctionOne,
    globalFunctionTwo
  }; 
};
项目根目录/grunt/helpers.js

module.exports = function(grunt) {
    require('jit-grunt')(grunt)

    function globalFunctionOne(param1) {
       console.log('yay it works from main file');
    }

    grunt.initConfig({});

    console.log(grunt.config());

    grunt.loadTasks('grunt/tasks')

    grunt.registerTask('default', ['sass:dist'];
}
module.exports = function(grunt) {
    function globalFunctionTwo(param1) {
        console.log('yay it works from partial file');
    }
}
function init(grunt) {
    "use strict";
    function loadConfig(pattern) {
        let config = {},
            fileName,
            fileData;

        grunt.file.expand(pattern).forEach(function (filePath) {
            fileName = filePath.split('/').pop().split('.')[0];
            fileData = require('./' + filePath)(grunt);
            config[fileName] = fileData;
        });

        return config;
    }

    function loadGrunt() {
        const config = {
            pkg: grunt.file.readJSON('package.json')
        };

        require('load-grunt-tasks')(grunt);

        if (grunt.file.exists('grunt/tasks')) {
            grunt.log.writeln('task directory found, loading tasks...');
            grunt.loadTasks('grunt/tasks');
        }

        grunt.util._.extend(config, loadConfig('grunt/configs/**/*.js'));

        grunt.initConfig(config);
    }
    loadGrunt();

}

module.exports = init;
module.exports = function (grunt) {
    "use strict";
    return {
        base: {
            options: {
                process: function (content) {
                    return grunt.template.process(content);
                }
            },
            src: 'grunt/templates/base.url.js',
            dest: 'www/source/config/base.url.js'
        },
        pluginManifest: {
            src: 'cordova/template/package.json',
            dest: '<%= grunt.task.current.args[0] %>/package.json'
        },
        splashScreens: {
            expand: true,
            cwd:"grunt/templates/",
            src: "screen/**",
            dest: '<%= create.dest %>/res/'
        }
    };
};
module.exports = function (grunt) {
  const helpers = require("../helpers.js)(grunt);
  return {
    options: {
      implementation: sass,
      includePaths: helpers.globalFunctionOne('dev'),
      outputStyle: 'expanded',
      sourceMap: true
    },
    dist: {
      files: {
        globalFunctionTwo('dist'),
      }
    }
  };
};
module.exports = function (grunt) {
  function globalFunctionOne(param1) {
    console.log('yay it works from main file');
  }

  function globalFunctionTwo(param1) {
    console.log('yay it works from partial file');
  }

  return {
    globalFunctionOne,
    globalFunctionTwo
  }; 
};