最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to configure grunt-contrib-uglify to minify files while retaining directory structure - Stack Overflow

programmeradmin2浏览0评论


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


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

Share Improve this question edited Sep 4, 2013 at 12:17 Šime Vidas 186k65 gold badges288 silver badges391 bronze badges asked Sep 4, 2013 at 11:54 PaddyPaddy 3,6506 gold badges32 silver badges51 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

Set the flatten property to false.

There is a clear explanation on the grunt copy github readme

https://github./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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论