grunt-contrib-uglify参数和使用

来源:互联网 发布:linux基础入门教程 编辑:程序博客网 时间:2024/06/06 18:27

Grunt插件三grunt-contrib-uglify参数和使用

2014-8-26 作者:小V 浏览:7464

标签: javascript js构建工具grunt

Grunt插件grunt-contrib-uglify(js压缩)

参数类型默认值描述mangleBoolean{}打开或关闭使用默认选项重整。如果指定一个对象,它直接传递给ast.mangle_names()和ast.compute_char_frequency()(模仿命令行的行为)。compressBoolean{}打开或关闭使用默认选项源压缩。如果指定一个对象,它是通过作为选项UglifyJS.Compressor()。beautifyBooleanfalse打开的生成的源代码美化。对象将被合并,并通过了发往UglifyJS.OutputStream的选项()expressionBooleanfalse解析一个表达式,而不是一个程序(用于解析JSON)report'min', 'gzip''min'无论是报告只造成微小或报告微小和gzip的结果。这是为了看看到底有多好清理CSS是表演,但使用“gzip的”将任务需要5到10倍更长的时间来完成有用的。示例输出。sourceMapBooleanfalse如果为true,源映射文件将在同一目录下DEST文件生成。默认情况下,将具有相同的基本名称为DEST文件,但有.MAP扩展。sourceMapName字符串函数未定义要自定义的名称或生成的源地图的位置,通过一个字符串来表示,其中写的源地图。如果函数提供的丑化目的地作为参数传递和返回值将被用来作为文件名。sourceMapIn字符串函数未定义输入源的地图,从早期的汇编,如位置从CoffeeScript的。如果函数提供的丑化源作为参数传递和返回值将被用作sourceMap名。这仅是有道理的,当有一个源文件。sourceMapIncludeSourcesBooleanfalse如果您要包含源文件的源地图的sourcesContent性内容传递此标志。encloseObject未定义总结所有的代码在一个可配置的参数/参数列表中关闭。每个键 - 值对的封装对象实际上是一个参数,参数对。wrapString未定义总结所有的代码在一个封闭,一个简单的方法,以确保没有任何泄漏。对于需要以公众出口和全局变量是可用的变量。圈的值是全局变量的出口将作为。exportAllBooleanfalse当使用保鲜膜这会让所有的全局变量和函数可以通过出口变量。preserveCommentsBoolean
String
Functionundefined选项:'false' 'all' 'some'
'false'将去除所有评论 
'all'会保留那些没有被压扁或删除代码块中的所有意见 
'some'将保留所有以一个感叹号(!)意见或包括封闭的编译器指令式(@preserve@license@cc_on)
'function'函数指定自己的意见保鲜功能。您将通过当前节点和当前注释和预期返回true或falsebannerString空字符串这个字符串将被前置到缩小的输出。模板字符串(例如<%= config.value%>会自动扩充。footerString空字符串这个字符串将被追加到缩小的输出。模板字符串(例如<%= config.value%>会自动扩充。例子1:

grunt.initConfig({
  uglify: {
    my_target: {
      files: {
        'dest/output.min.js': ['src/input1.js', 'src/input2.js']
      }
    }
  }
});

没有识别码demo

grunt.initConfig({
  uglify: {
    options: {
      mangle: false
    },
    my_target: {
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});


保留标识符demo

grunt.initConfig({
  uglify: {
    options: {
      mangle: {
        except: ['jQuery', 'Backbone']
      }
    },
    my_target: {
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

来源地图demo

grunt.initConfig({
  uglify: {
    my_target: {
      options: {
        sourceMap: true,
        sourceMapName: 'path/to/sourcemap.map'
      },
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

高级源地图demo

grunt.initConfig({
  uglify: {
    my_target: {
      options: {
        sourceMap: true,
        sourceMapIncludeSources: true,
        sourceMapIn: 'example/coffeescript-sourcemap.js', // input sourcemap from a previous compilation
      },
      files: {
        'dest/output.min.js': ['src/input.js'],
      },
    },
  },
});


grunt.initConfig({
  uglify: {
    options: {
      compress: {
        drop_console: true
      }
    },
    my_target: {
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

美化demo

grunt.initConfig({
  uglify: {
    my_target: {
      options: {
        beautify: true
      },
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    },
    my_advanced_target: {
      options: {
        beautify: {
          width: 80,
          beautify: true
        }
      },
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),
  uglify: {
    options: {
      banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
        '<%= grunt.template.today("yyyy-mm-dd") %> */'
    },
    my_target: {
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

条件编译demo

grunt.initConfig({
  uglify: {
    options: {
      compress: {
        global_defs: {
          "DEBUG": false
        },
        dead_code: true
      }
    },
    my_target: {
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

动态编译一个文件夹中的所有文件demo

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