How can I rename files with Grunt, based on the respective file's parent folder name?

前端 未结 5 828
借酒劲吻你
借酒劲吻你 2021-01-30 10:28

I have a the following structure:

src/
    modules/
        module1/
            js/
                main.js
            scss/
                main.scss
                 


        
5条回答
  •  半阙折子戏
    2021-01-30 11:08

    There's no need to use grunt-contrib-copy just for this any more, you can now take advantage of grunt.file.expandMapping which has options to just change the file extension, or to define a function that returns the output filename.

    Here's an example of a files object in a jade task for compiling .jade templates into .html files:

    files: [{
        expand: true, 
        src: "**/*.jade", 
        dest: "<%= distDir %>", 
        cwd: "<%= assetsDir %>/jade", 
        rename: function(dest, matchedSrcPath, options) {
            // return the destination path and filename:
            return (dest + matchedSrcPath).replace('.jade', '.html');
        }
    }]
    

    It would have been easier to use the ext: '.html' option instead of the rename option in this case, but I'm using rename here so you can see how it works.

    More info about the ext and rename (and other) options in the grunt.file docs. Some more examples here and here.

提交回复
热议问题