【问题标题】:Setting the destination folder with uglify in Grunt在 Grunt 中使用 uglify 设置目标文件夹
【发布时间】:2016-05-31 14:33:25
【问题描述】:

使用如下所示的 Grunt 代码和文件夹结构,当我运行 grunt uglify 时,输出结果是在 \min 文件夹中创建子文件夹结构 \js\src,其中最深的文件夹 (src)将包含缩小的文件。但我希望在 \min 文件夹根目录中创建缩小文件。

如果我将dest 参数值设置为空:``,文件将创建在与src 文件夹相同的文件夹中。

如果我将dest 参数值设置为://js/min/js/min/,则不会创建任何内容。

如何在min 文件夹的根目录中直接生成缩小文件?

module.exports = function(grunt){ // 1

grunt.initConfig({
  uglify: {
    my_target: {
      files: [{
          expand: true,
          src: 'js/src/*.js',
          dest: 'js/min/',
          ext : '.min.js',
      }]
    }
  }
});

  grunt.loadNpmTasks('grunt-contrib-uglify'); //https://www.npmjs.com/package/grunt-contrib-uglify


  grunt.registerTask('default', function() { // 4
      grunt.log.writeln('Hello, from the default grunt task!'); // 5
  });

}

【问题讨论】:

    标签: gruntjs uglifyjs2


    【解决方案1】:

    Grunt 文档中有一个关于 building the files object dynamically. 的相关部分

    特别感兴趣的是“cwd”属性:

    所有 src 匹配都相对于(但不包括)这个路径。

    这将允许我们通过将“src”值设置为“cwd”值来删除不需要的路径。这意味着生成的源文件路径在添加到 /js/min 文件夹时不会有不需要的前缀(“/js/src/”)。

    我们生成的 Grunt 文件如下所示:

    module.exports = function (grunt) {
    
        grunt.initConfig({
            uglify: {
                my_target: {
                    files: [{
                        expand: true,
                        cwd: 'js/src/',
                        src: '*.js',
                        dest: 'js/min/',
                        ext : '.min.js',
                    }]
                }
            }
        });
    
        grunt.loadNpmTasks('grunt-contrib-uglify');
    
        grunt.registerTask('default', ['uglify']);
    
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      相关资源
      最近更新 更多