Grunt:如何动态构建文件对象 [英] Grunt: How to build the files object dynamically

查看:127
本文介绍了Grunt:如何动态构建文件对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在这里错过一些非常简单的事情。我正在尝试编写一个处理文件的函数任务。 Grunt API文档提到您可以[动态构建文件对象],但出于某种原因,我无法使其工作。我的Gruntfile.js文件的简化版本如下所示:

  module.exports = function(grunt){
grunt.initConfig({
proj:{
build:{
files:[{
expand:true,
cwd:'src',
src :['** / *。js'],
dest:'dist'
}]
}
}
});

grunt.registerTask('proj',function(){
var files = grunt.config('proj.build.files');
console.log(files) ;
});
};

我希望日志显示从src目录到dist目录的文件映射列表。实际得到的记录是配置中的对象proj.build.files,如下所示:

 运行proj:build任务
[{expand:true,cwd:'src',src:['** / *。js'],dest:'dist'}]

完成,没有错误。

API文档仅针对其他任务讨论这种类型的配置。我试图通过uglify任务来查看如何检索文件映射,但我无法弄清楚。

这是我发现的动态构建Grunt任务文件集的解决方法:

  uglify:{
app:{
文件:[{
src:'{<%= _prefixSrc(pkg.target,pkg.resources.js)%>}',//注意括号!
dest:'<%= pkg.target%> min /<%= pkg.name%> .min.js'
}]
}
} ,
_prefixSrc:函数(前缀,文件){
返回files.map(函数(文件){
返回前缀+文件;
});
},

请参阅GitHub上的此问题/功能请求并随时发表评论如果您觉得它有用: https://github.com/gruntjs/grunt/issues/1307


I must be missing something very simple here. I'm trying to write a function task that deals with files. The Grunt API docs mention that you can [Build the files object dynamically], but for some reason I can't get this to work. A simplified version of my Gruntfile.js file looks like this:

module.exports = function(grunt) {
    grunt.initConfig({
        proj: {
            build: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: ['**/*.js'],
                    dest: 'dist'
                }]
            }
        }
    });

    grunt.registerTask('proj', function(){
        var files = grunt.config('proj.build.files');
        console.log(files);
    });
};

I expect the log to show a list of file mappings from the src directory to the dist directory. What actually gets logged is the object proj.build.files from the config, like this:

Running "proj:build" task
[ { expand: true, cwd: 'src', src: [ '**/*.js' ], dest: 'dist' } ]

Done, without errors.

The API docs only talk about this type of configuration in terms of other tasks. I tried looking through the uglify task to see how the file mappings are retrieved, but I couldn't figure it out.

解决方案

Here is the workaround I found to dynamically build filesets for Grunt tasks:

uglify: {
    app: {
        files: [{
            src: '{<%= _prefixSrc(pkg.target, pkg.resources.js) %>}', // Note the brackets!
            dest: '<%= pkg.target %>min/<%= pkg.name %>.min.js'
        }]
    }
},
_prefixSrc: function(prefix, files) {
    return files.map(function(file){
        return prefix + file;
    });
},

See also this issue/feature request on GitHub and feel free to comment it if you find it useful: https://github.com/gruntjs/grunt/issues/1307

这篇关于Grunt:如何动态构建文件对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆