2.9.1 • Published 8 years ago

tpack-uglify-js v2.9.1

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

tpack-uglify-js

TPack 插件:使用 UglifyJS 压缩或格式化 JS。

安装

$ npm install tpack-uglify-js -g

使用

压缩 JS

tpack.src("*.js").ignore("*.min.*").pipe(tpack.plugin("tpack-uglify-js"));

压缩 JS 并重命名

tpack.src("*.js").ignore("*.min.*").pipe(tpack.plugin("tpack-uglify-js")).dest("$1.min.js");

格式化 JS

tpack.src("*.js").pipe(tpack.plugin("tpack-uglify-js"), {
	compress: false,
	output: {
		beautify: true
	}
});

JS 语法检查

tpack.src("*.js").pipe(tpack.plugin("tpack-uglify-js"), {
	compress: false
});

源映射(Source Map)

本插件可生成源映射,具体用法见 源映射

配置

tpack.src("*.js").pipe(tpack.plugin("tpack-uglify-js"), {
	warnings: false,            // 是否显示警告。
    inSourceMap: null,          // 输入的源映射。[1]
    outSourceMap: false,        // 输出源映射。[1]
	mangle: {},					// 不重命名的变量列表。如 {'except': ['$']	}。
	mangleProperties: false,    // 不重命名属性。
	parse: {
		strict: false,          // 解析时默认启用严格模式。
		filename: null,         // 设置解析的文档名,主要用于调试。[1]
		toplevel: null,         // 解析所属的顶层语法树节点。
	},
	compress: {
		sequences: true,        // 将连续语句转为逗号表达式。如 a = 1; return a; → return a=1, a; 。
		properties: true,       // 将常量属性名转为点表达式。如 a["foo"] → a.foo。
		dead_code: true,        // 删除永远无法执行的代码。如 if(false) {...} 中的代码。[1]
		drop_debugger: true,    // 删除 “debugger” 语句。[1]
		drop_console: true,     // 删除 console.xx 语句。[1]
		unsafe: false,          // 允许不安全的优化(建议关闭)。如 new Object() → {}。具体见 [不安全代码](https://github.com/mishoo/UglifyJS2#the-unsafe-option)
		conditionals: true,     // 优化常量条件表达式。
		comparisons: true,      // 优化比较运算。如 !(a <= b) → a > b (仅当 unsafe 为 true 时优化)和 a = !b && !c && !d && !e → a=!(b||c||d||e)。
		evaluate: true,         // 尝试执行常量表达式。具体见 [条件编译](https://github.com/mishoo/UglifyJS2#conditional-compilation)
		booleans: true,         // 优化布尔运算。如 !!a ? b : c → a ? b : c。
		loops: true,            // 优化常量循环。如 while(true) → while(1)
		unused: true,           // 删除未引用的局部变量和函数。
		hoist_funs: true,       // 提升函数定义到函数顶部。
		hoist_vars: false,      // 提升变量声明到函数顶部。(由于提升会可能导致代码量增加,因此默认为 false。)
		if_return: true,        // 优化 return/continue 语句后的 if 语句。 
		join_vars: true,        // 合并多个变量声明为同一个 var 语句。
		cascade: true,          // 尝试简化逗号表达式。如 x, x → x 和 x = something(), x → x = something()。
		side_effects: true,     // 删除无外部影响的函数调用。如 console.log() 删除后不会影响其它逻辑。
        pure_getters: false,    // 将所有属性和字段都作为无影响的函数处理。
		warnings: false,        // 压缩删除代码时是否显示警告。
        pure_funcs: null,       // 指定无影响的函数列表。如 ["Math.floor"],
        keep_fargs: true,       // 保留未使用的函数参数。如果设为 false,会导致依赖 Function.prototype.length 的代码出错。
        keep_fnames: false,     // 保留函数名。如果设为 false,会导致依赖 Function.prototype.name 的代码出错。
        passes: 1,              // 压缩的次数。
		global_defs: {          // 预设全局常量。[1]
			DEBUG: false,
			RELEASE: true
		},
	},
	output: {
		beautify: false,        // 是否格式化代码。
		indent_level: 4,        // 缩进字符数。(仅当格式化代码时有效)
		indent_start: 0,        // 每行缩进数。(仅当格式化代码时有效)
		quote_keys: false,      // 使用引号定义 JSON 对象的键。
        quote_style: 0,         // 引号风格。0:优先使用双引号。1:全部使用单引号。2:全部使用双引号。3:保留原引号。
		space_colon: true,      // 在冒号后天添加一个空格。
		ascii_only: false,      // 编码特殊 Unicode 字符。
		inline_script: false,   // 编码 "</script"。
		width: 80,              // 允许最大列数。(仅当格式化代码时有效)
		max_line_len: 32000,    // 允许最大行号。(仅当压缩代码时有效)
		ie_proof: true,         // 输出 IE 安全代码。
		source_map: null,       // 是否输出源映射。
		bracketize: false,      // 为单行语句追加使用花括号。
        preamble: null,         // 在最终源码前追加的文本(一般是一段注释)。
		comments: /^!|@preserve|@license|@cc_on/,   // 输出注释。[1]
		semicolons: true,       // 使用分号分割语句(否则使用换行)。
	}
});

1: 插件内部已重设了此配置的默认值。

另参考 https://github.com/mishoo/UglifyJS

2.9.1

8 years ago

2.9.0

8 years ago

1.1.2

8 years ago

1.1.0

8 years ago

1.0.1

9 years ago

1.0.0

9 years ago