How to configure grunt-contrib-uglify to minify fi

2020-07-10 09:04发布

问题:


In case I have multiple subdirectories under 'js' directory in the example Gruntfile posted below, and want to retain the subdirectories under a different destination directory, how do I do it?

For e.g.

module.exports = function (grunt) {
    grunt.initConfig({

       // define source files and their destinations
       uglify: {
           files: { 
               src: 'js/**/*.js',  // source files mask
               dest: 'minJs/',    // destination folder
               expand: true,    // allow dynamic building
               flatten: true,   // remove all unnecessary nesting
           }
       }
    });

    // load plugins
    grunt.loadNpmTasks('grunt-contrib-uglify');

    // register at least this one task
    grunt.registerTask('default', [ 'uglify' ]);
};

In this case, I have shown */.js, but even if I explicitly specify a single subdirectory like js/xyz/*.js then also it is not copying the directory structure, instead it seems to put the files within subdirectory under minJs/ folder in the example. What am I missing here? Please help.

Thanks,
Paddy

回答1:

Set the flatten property to false.

There is a clear explanation on the grunt copy github readme

https://github.com/gruntjs/grunt-contrib-copy

Excerpt:

$ grunt copy
Running "copy:main" (copy) task
Created 1 directories, copied 1 files

Done, without errors.
$ tree -I node_modules

.
├── Gruntfile.js
├── dest
│   └── src
│       ├── a
│       └── subdir
└── src
    ├── a
    └── subdir
        └── b

5 directories, 4 files
Flattening the filepath output:

copy: {
  main: {
    expand: true,
    cwd: 'src/',
    src: '**',
    dest: 'dest/',
    flatten: true,
    filter: 'isFile',
  },
},
$ grunt copy
Running "copy:main" (copy) task
Copied 2 files

Done, without errors.
$ tree -I node_modules
.
├── Gruntfile.js
├── dest
│   ├── a
│   └── b
└── src
    ├── a
    └── subdir
        └── b

3 directories, 5 files