如何配置grunt-contrib-uglify以在保留目录结构的同时缩小文件 [英] How to configure grunt-contrib-uglify to minify files while retaining directory structure

查看:174
本文介绍了如何配置grunt-contrib-uglify以在保留目录结构的同时缩小文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



例如

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

//定义源文件及其目的地
uglify:{
文件:{
src:'js / ** / *。 js',//源文件掩码
dest:'minJs /',//目标文件夹
展开:true,//允许动态构建
flatten:true,//删除所有不必要的嵌套
}
}
});

//载入插件
grunt.loadNpmTasks('grunt-contrib-uglify');

//注册至少这一项任务
grunt.registerTask('default',['uglify']);
};

在这种情况下,我显示了* / .js,但甚至如果我明确指定了一个像js / xyz / *。js这样的单独子目录,那么它也不会复制目录结构,相反,它似乎将文件放在minJs /文件夹下的子目录中。我在这里错过了什么?请帮助。



谢谢,


Paddy

解决方案

将flatten属性设置为false。



有关Grunt副本github自述文件的明确说明



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



节选:

  $ grunt copy 
运行copy:主(复制)任务
创建1个目录,复制1个文件

完成,没有错误。
$ tree -I node_modules


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

5个目录,4个文件
压扁文件路径输出:

复制:{
main:{
展开:true,
cwd:'src /',
src:'**',
dest:'dest /',
flatten:true,
filter:'isFile',
},
},
$ grunt copy
运行copy:main(复制)任务
复制2个文件

完成,没有错误。
$ tree -I node_modules

├──Gruntfile.js
├──dest
│├──a
│└──b
└──src
├─ ─a
└──子目录
└──b

3个目录,5个文件



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

解决方案

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

这篇关于如何配置grunt-contrib-uglify以在保留目录结构的同时缩小文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆