Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy compiled jade files to a destination folder using grunt

For a single page app that I'm working on, I have the following structure:

  • dist
    • css
    • js
    • lib
    • partials
    • index.html
  • src
    • css
    • js
    • lib
    • views
      • partials
      • index.jade

Directory dist will be used by the express server to serve the project. I have trivial grunt tasks (using grunt-contrib-clean, grunt-contrib-copy) for cleaning dist and copying src/css, src/js, src/lib to dist.

The problem lies with src/views. This directory contains jade files which need to be compiled to html files. After compilation I want them in dist (index.html in the dist root, partials as subdir).

At the moment I am using the grunt-contrib-jade task to compile and copy the jade files. I want to copy them to dist, since I don't want to add the compiled html files to source control. But now this is not really workable, since you have to specify every jade file (now there are only a few, but that will grow):

   jade: {
        compile: {
            options: {
                pretty: true
            },
            files: {
                // TODO make one line
                'dist/index.html': ['src/views/index.jade'],
                'dist/partials/banner.html': ['src/views/partials/banner.jade'],
                'dist/partials/dashboard.html': ['src/views/partials/dashboard.jade'],
                'dist/partials/navbar.html': ['src/views/partials/navbar.jade'],
                'dist/partials/transfer.html': ['src/views/partials/transfer.jade']
            }
        }
    },

Is there any way to use the grunt-contrib-jade task (or another one) with a directory filter? Like this:

   jade: {
        compile: {
            options: {
                pretty: true
            },
            dir: {
                'dist': ['src/views']
            }
        }
    }
like image 686
asgoth Avatar asked Dec 30 '12 10:12

asgoth


3 Answers

Little clarification from Grunt wiki - expand mapping:

grunt.file.expandMapping(patterns, dest [, options])

Note that while this method may be used to programmatically generate a files array for a multi task, the declarative syntax for doing this described in the "Building the files object dynamically" section of the Configuring tasks guide is preferred.

Assuming the above, configuration will look like this:

files: [ { 
  expand: true, 
  src: "**/*.jade", 
  dest: "dist/", 
  cwd: "src/views", 
  ext: '.html'
} ];

Same result with declarative configuration.

like image 84
AndreyM Avatar answered Oct 31 '22 17:10

AndreyM


I ended up upgrading to grunt 0.4 (which causes some other problems, but that I'll be able to handle).

With grunt version 0.4 it is possible to use grunt.file.expandMapping:

    jade: {
        compile: {
            options: {
                pretty: true
            },
            files: grunt.file.expandMapping(['**/*.jade'], 'dist/', {
                cwd: 'src/views',
                rename: function(destBase, destPath) {
                    return destBase + destPath.replace(/\.jade$/, '.html');
                }
            })

        }
    },
like image 10
asgoth Avatar answered Oct 31 '22 16:10

asgoth


If you want to change only the extension of the files from .jade to .html, another option would be using the flatten and ext parameters like so:

jade: {
    compile: {
        options: {
            data: { debug: false, title: 'My awesome application' }
        },
        files: grunt.file.expandMapping(['**/*.jade'], '<%= yeoman.dist %>/views', {
            cwd: '<%= yeoman.app %>/views',
            flatten: true,
            ext: '.html'
       })
    }
}

Or even better (as explained here):

jade: {
    compile: {
        options: {
            data: { debug: false, title: 'My awesome application' },
            pretty: true
        },
        files: [
            {
                expand: true,
                cwd: '<%= yeoman.app %>/views',
                src: ['**/*.jade'],
                dest: '<%= yeoman.dist %>/views',
                ext: '.html'
            }
        ]}
}

Thanks.

like image 3
Agustin Treceno Avatar answered Oct 31 '22 15:10

Agustin Treceno