Grunt: How to build the files object dynamically

前端 未结 2 1911
陌清茗
陌清茗 2021-01-23 06:25

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 dynamic

相关标签:
2条回答
  • 2021-01-23 06:56

    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

    0 讨论(0)
  • 2021-01-23 07:04

    You can use rename function to change file name is files object like below...

    build: {
      files: [{
        expand: true,
        cwd: 'src',
        src: ['**/*.js'],
        dest: 'dist',
        rename: function(dest, src) {
          /* 
             rename logic
             you will have access to src and dest name and can return desirect name from this function.
          */
          return src+123;
        }
     }]
    }
    
    0 讨论(0)
提交回复
热议问题