Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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 拆分文件_Javascript_Gruntjs - Fatal编程技术网

Javascript 拆分文件

Javascript 拆分文件,javascript,gruntjs,Javascript,Gruntjs,我的GrunFile现在变得相当大,我想将它拆分为多个文件。我在谷歌上搜索了很多,做了很多实验,但都没能成功 我想要这样的东西: module.exports = function(grunt) { require('load-grunt-config')(grunt); // define some alias tasks here }; Grunfile.js module.exports = function(grunt) { grunt.initConfig({

我的GrunFile现在变得相当大,我想将它拆分为多个文件。我在谷歌上搜索了很多,做了很多实验,但都没能成功

我想要这样的东西:

module.exports = function(grunt) {
  require('load-grunt-config')(grunt);

  // define some alias tasks here
};
Grunfile.js

module.exports = function(grunt) {
  grunt.initConfig({
    concat: getConcatConfiguration()
  });
}
module.exports = function(grunt) {
  grunt.initConfig({
    concat: require('grunt/concat')(grunt);
  });
};
functions.js

function getConcatConfiguration() {
  // Do some stuff to generate and return configuration
}

如何将functions.js加载到Gruntfile.js中?

如何执行:

您需要导出concat配置,并在GrunFile(basic node.js)中要求它

我建议将所有特定于任务的配置放在一个以配置命名的文件中(在本例中,我将其命名为
concat.js

此外,我将
concat.js
移动到一个名为
grunt

Grunfile.js

module.exports = function(grunt) {
  grunt.initConfig({
    concat: getConcatConfiguration()
  });
}
module.exports = function(grunt) {
  grunt.initConfig({
    concat: require('grunt/concat')(grunt);
  });
};
grunt/concat.js

module.exports = function getConcatConfiguration(grunt) {
  // Do some stuff to generate and return configuration
};

你应该如何做:

已经有人创建了名为的模块。这正是你想要的

继续,将所有内容(如上所述)放入单独的文件中,放入您选择的位置(默认文件夹ist
grunt

那么您的标准GrunFile应该如下所示:

module.exports = function(grunt) {
  require('load-grunt-config')(grunt);

  // define some alias tasks here
};

你回答的第一部分不起作用。这是我使用的代码和我得到的错误:我认为你答案的第二部分不适用于我的情况。我不想创建Grunt任务,我想创建可以使用的JavaScript函数。我阅读了文档,但找不到如何动态生成配置(使用函数)并将其返回值分配给“concat”属性。我已经找到了它。您需要将concat.js的第一行替换为“exports.getConcatConfiguration=function(){”现在出现了下一个问题:concat.js中没有grunt变量。我忘了在require中添加大括号,请立即查看我编辑的答案。您需要调用所需函数并将grunt作为参数传递。但我强烈建议使用-module!对于第一个示例,它现在可以工作了,谢谢!我想使用load grunt配置模块,但是我不明白它是如何工作的(就像我在前面的评论中说的)。