如何配置grunt-contrib-uglify以在保留目录结构的同时缩小文件

Pad*_*ddy 6 javascript uglifyjs gruntjs


如果我在下面发布的示例Gruntfile中的'js'目录下有多个子目录,并且想要将子目录保留在不同的目标目录下,我该怎么做?

例如

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' ]);
};
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我已经显示了*/ .js,但即使我明确指定了一个像js/xyz/*.js这样的子目录,那么它也不是复制目录结构,而是将它们放在minJs下的子目录中/示例中的文件夹.我在这里错过了什么?请帮忙.

谢谢,
帕迪

Nic*_*ray 5

将flatten属性设置为false.

关于grunt copy github自述文件有一个明确的解释

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

摘抄:

$ 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
Run Code Online (Sandbox Code Playgroud)

  • 谢谢@Nicholas,想知道这些东西是否对所有的贡献插件都是通用的,如果是这样的话,他们不应该放在一个共同的地方而不是在一个单独的contrib插件的文档中.我总是遇到这些通用选项的困难,因为相应的任务没有特定于这些 - 这些可能是grunt本身常见而不是特定的任何插件.无论如何,谢谢你的答案. (2认同)
  • @Paddy - 是的我同意,它在http://gruntjs.com/configuring-tasks页面上提到,但复制插件提供了它的工作原理的最佳解释 (2认同)