Search code examples
gruntjsuglifyjsgrunt-contrib-uglify

Multiple destination directories using Uglify


If I have the following file/directory structure:

somemodule/
└── amd
└── src
    └── foo.js
someothermodule/
└── amd
└── src
    └── bar.js

Am I able to make grunt-contrib-uglify produce the following minified output?

somemodule/
└── amd
└── build
    └── foo.min.js
└── src
    └── foo.js
someothermodule/
└── amd
└── build
    └── bar.min.js
└── src
    └── bar.js

So far I have tried the following Grunt config:

grunt.initConfig({
    uglify: {
        dynamic_mappings: {
            files: [
                {
                    expand: true,
                    src: ['**/amd/src/*.js', '!**/node_modules/**'],
                    dest: 'build/',
                    ext: '.min.js',
                    extDot: 'first'
                },
            ],
        }
    }
});

Which gives me something, but not quite what I want (the output is placed in the build/ directory which is in the root of my project):

build/
├── somemodule
│   └── amd
│       └── src
│           └── foo.min.js
└── someothermodule
    └── amd
        └── src
            └── bar.min.js

I'm a little bit stuck from where to go to from here - any pointers would be appreciated. Perfectly happy to elaborate on anything if needed.

-Dave


Solution

  • Managed to find out what I wanted to do based on this SO question: How to grunt-uglify multiple script files while keeping folder structure

    I wound up with the following rename function:

    files: grunt.file.expandMapping(['**/amd/src/*.js', '!**/node_modules/**'], '', {
               rename: function(destBase, destPath) {
                   destPath = destPath.replace('src', 'build');
                   destPath = destPath.replace('.js', '.min.js');
                   return destPath;
               }
    })