Grunt JS file pattern in cwd

2.6k Views Asked by At

I am using grunt to build my project and have the following src structure:

app/src/client/pages/user/users.js
app/src/client/pages/user/users.html

app/src/client/pages/project/projects.js
app/src/client/pages/user/projects.html

Now, I am trying to build my project to look like this:

app/dist/client/users.html

I use the contrib-htmlmin plugin and my grunt config looks like this:

htmlmin: {
            options: {
                    removeComments: true,
                    collapseWhitespace: true
            },      
            partials: {
                files: [
                    {
                        expand: true,
                        cwd: "app/src/client/pages/*/",
                        dest: "app/dist/client/",
                        src: ["*.html"]
                    }
                ]
            }

But this is not working at all, no files are being minified. Any suggestions?

1

There are 1 best solutions below

1
On

As best I can tell, Grunt does not expand patterns in cwd, so your option

cwd: "app/src/client/pages/*/",

never gets converted to an array of matching directories.

You can follow my logic for this conclusion by starting at this line in the source. grunt.file.expandMapping (source here) doesn't call grunt.file.expand on your cwd pattern.

That doesn't mean you can't do it yourself. I've used the following pattern to accomplish something similar with grunt-contrib-sass when I have sass files spread out over several directories:

htmlmin: {
    options: {
            removeComments: true,
            collapseWhitespace: true
    },      
    partials: {
        files: grunt.file.expand(['app/src/client/pages/*/']).map(function(cwd) {
            return {
                expand: true,
                cwd: cwd,
                dest: "app/dist/client/",
                src: ["*.html"]
            };
        }),
    }