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

标签 javascript gruntjs uglifyjs


如果我在下面发布的示例 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' ]);
};

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

谢谢,
水稻

最佳答案

将 flatten 属性设置为 false。

在grunt copy github readme上有明确的解释

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

关于javascript - 如何配置 grunt-contrib-uglify 以在保留目录结构的同时缩小文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18613164/

相关文章:

javascript - uglifyjs 压缩但不破坏函数或变量名

javascript - 在 Grunt uglifyjs 中禁止来自控制台的 WARN 语句

javascript - jQuery HTML5 范围 slider 中的实时输出

directory -/** 和/* 在 Grunt 中的目录导航方面有何不同?

javascript - 使用 html-webpack-plugin 将对象传递给 ejs 加载器

angularjs - 使用 Travis CI 测试使用 SASS 的 AngularJS 应用程序

node.js - 我对 Gruntjs 目标做错了什么?

javascript - UglifyJS 属性重整

javascript - 该值未定义

javascript - 如何将选择/下拉值传递给表单2?