Comparing version 0.1.0 to 0.1.1
#!/usr/bin/env node | ||
//var util = require("util"); | ||
var build = require('../main.js').build; | ||
@@ -10,27 +10,74 @@ var fs = require('fs'), path = require('path'); | ||
var config, | ||
cfgfile, | ||
log = '//请在要运行打包的目录下新建cp-config.js文件,并至少填入以下配置内容(更详细参数配置见readme文档):\n' + | ||
'exports.config = {\n' + | ||
' paths:{\n' + | ||
' src:[\n' + | ||
' "./test1.js-n", // 相对build配置文件的路径,-n参数,表示此文件不压缩\n' + | ||
' "./test.js"\n' + | ||
' ],\n' + | ||
' tar: "../tar.js" //输出的目标文件\n' + | ||
' }\n' + | ||
'}\n'; | ||
cfgfile, | ||
log = | ||
'//请在要运行打包的目录下新建cp-config.js文件并参考以下配置参数,命令行运行cutepack。:\n' + | ||
'//或者新建自定义命名的文件xxx,并参考以下配置参数,命令行运行cutepack xxx。:\n' + | ||
'exports.config = \n' + | ||
'[\n' + | ||
' {\n' + | ||
' paths:{\n' + | ||
' src:[\n' + | ||
' "./test1.js-n", // 相对build配置文件的路径,-n参数,表示此文件不压缩\n' + | ||
' "./test.js"\n' + | ||
' ],\n' + | ||
' tar: "../tar.js" //输出的目标文件\n' + | ||
' }\n' + | ||
' },\n' + | ||
' {\n' + | ||
' paths:{\n' + | ||
' src:[\n' + | ||
' "./test1.js-n",\n' + | ||
' "./test.js"\n' + | ||
' ],\n' + | ||
' tar: "../tar1.js"\n' + | ||
' },\n' + | ||
' options:{ //可选,一般不需要配置此参数\n' + | ||
' strict_semicolons: true, //设为true时,代码需要写严格的分号\n' + | ||
' mangle_options: {\n' + | ||
' except:["onename"] // 保留指定的变量名,函数名..不被修改\n' + | ||
' },\n' + | ||
' gen_options: {\n' + | ||
' ascii_only: false, // pass true if you want to encode non-ASCII characters as unicode characters.\n' + | ||
' inline_script: false // – pass true to escape occurrences of </script in strings\n' + | ||
' }\n' + | ||
' }\n' + | ||
' }\n' + | ||
']'; | ||
try{ | ||
cfgfile = require(filepath) | ||
config = cfgfile.config; | ||
cfgfile = require(filepath) | ||
config = cfgfile.config; | ||
}catch (e){ | ||
console.log(log); | ||
return; | ||
console.log(log); | ||
return; | ||
} | ||
if(typeof config == 'undefined' || !config.paths) { | ||
var options, _toString = Array.prototype.toString; | ||
/** | ||
console.log(typeof ('aaa')) | ||
console.log(typeof (function(){})) | ||
console.log(typeof (new function(){})) | ||
console.log(typeof ({})) | ||
console.log(typeof ([])) | ||
console.log(typeof (/^\s+$/)) | ||
console.log(util.isRegExp(/^\s+$/)) | ||
console.log(Array.prototype.toString.call([{},{},'aaa'])) //注意此结果 | ||
console.log(util.isArray([{},{},'aaa'])) | ||
console.log(Array.isArray([{},{},'aaa'])) | ||
*/ | ||
if(typeof config == 'undefined') { | ||
console.log(log); | ||
return; | ||
}else if(_toString.call(config) === '[object Object]' && config.paths){ //兼容原来配置方式 | ||
options = config.options; | ||
build(config.paths, options); | ||
}else if(Array.isArray(config)){ | ||
config.forEach(function(item){ | ||
if(_toString.call(item) === '[object Object]' && item.paths){ | ||
options = item.options; | ||
build(item.paths, options); | ||
} | ||
}) | ||
} | ||
var options = config.options || 'undefined'; | ||
build(config.paths, options); // 没特殊需求,不需要传递options参数 |
45
main.js
@@ -7,3 +7,3 @@ | ||
var _util = {}, compressCode = ''; | ||
var _util = {'compressCode':''}; | ||
_util.uglify = function (orig_code, options) { | ||
@@ -14,2 +14,3 @@ options || (options = {}); | ||
//console.log(orig_code,options) | ||
var ast = jsp.parse(orig_code, options.strict_semicolons); // parse code and get the initial AST | ||
@@ -26,3 +27,3 @@ ast = pro.ast_mangle(ast, options.mangle_options); // get a new AST with mangled names(去掉,则不修改变量名) | ||
_util.checkPath = function (path) { | ||
var arr = path.split(/\.js/),arrlen=arr.length, flag = ''; | ||
var arr = path.split(/\.js/),arrlen = arr.length, flag = ''; | ||
//console.log(arr.length); | ||
@@ -40,16 +41,10 @@ if (arr[0] === '') return; | ||
} | ||
} | ||
_util.pathParse = function (paths, options) { | ||
var root = _util.getRoot(), pathobj = '', pat = '', flag = '', srcs = paths.src; | ||
var root = _util.getRoot(), pathobj = '', pat = '', srcs = paths.src; | ||
for (var i = 0; i < srcs.length; i++) { | ||
pathobj = _util.checkPath(path.join(root, path.normalize(srcs[i]))); | ||
pat = pathobj.path; | ||
flag = pathobj.flag; | ||
if (pat && fs.existsSync(pat)) { | ||
if (flag && flag === '-n') { | ||
compressCode += fs.readFileSync(pat, 'utf8') + '\n'; | ||
}else { | ||
compressCode += _util.compress(pat, options) + ';\n'; | ||
} | ||
_util.compressCode += _util.compress(pat, pathobj.flag, options) | ||
} else { | ||
@@ -61,8 +56,11 @@ continue; | ||
} | ||
_util.compress = function (file, options) { | ||
var data = fs.readFileSync(file, 'utf8'), code = ''; | ||
//console.log(data); | ||
_util.compress = function (pat, flag, options) { | ||
var data = fs.readFileSync(pat, 'utf8'), code = ''; | ||
if (data) { | ||
code = _util.uglify(data, options); | ||
//console.log(code); | ||
if (flag && flag === '-n') { | ||
code = data + '\n'; | ||
}else { | ||
//console.log(_util.compressCode) | ||
code = _util.uglify(data, options) + ';\n'; | ||
} | ||
return code; | ||
@@ -72,6 +70,5 @@ } | ||
_util.createTarget = function (tar) { | ||
fs.writeFile('./' + tar, compressCode, function (err) { | ||
if (err) throw err; | ||
console.log('build success'); | ||
}); | ||
var tar = './' + tar | ||
fs.writeFileSync(tar, _util.compressCode); | ||
console.log('[build success] target file in "' + tar + '"'); | ||
} | ||
@@ -81,11 +78,11 @@ | ||
if (typeof paths.src == 'undefined' || !util.isArray(paths.src)) { | ||
consoel.log('请确认设置了src路径,并且为数组类型'); | ||
console.log('请确认设置了src路径,并且为数组类型'); | ||
return; | ||
} | ||
if(typeof paths.tar == 'undefined' || Array.prototype.toString.call(paths.tar) != '[object String]') { | ||
paths.tar = '__build_' + Date.now() + '.js'; | ||
} | ||
// | ||
if(typeof paths.tar == 'undefined' || Array.prototype.toString.call(paths.tar) != '[object String]') { | ||
paths.tar = '__build_' + Date.now() + '.js'; | ||
} | ||
_util.compressCode = ''; | ||
_util.pathParse(paths, options); | ||
} | ||
exports.build = _build; |
{ | ||
"name": "cutepack", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "a simple compression and package module", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
@@ -15,34 +15,45 @@ cutepack | ||
- 使用 | ||
> 配置好cp-config.js文件,命令行运行 cutepack | ||
> 方法一:配置好默认命名的cp-config.js文件,命令行运行 cutepack | ||
> 方法二:新建自定义命名的文件xxx,命令行运行 cutepack xxx | ||
<pre> | ||
cp-config.js示例: | ||
exports.config = { | ||
paths:{ | ||
src:[ | ||
'./test1.js-n', // -n参数,表示 此文件不压缩 | ||
'./test.js' | ||
], | ||
tar: '../tar.js' //输出的目标文件 | ||
}, | ||
options:{ | ||
strict_semicolons: false, //设为true时,代码需要写严格的分号 | ||
mangle_options: { | ||
//defines: { DEBUG: ['onename', 'false'], VERSION: ['string', '1.0'] }, | ||
except:['onename'] // 保留指定的变量名,函数名..不被修改 | ||
}, | ||
squeeze_options:{}, | ||
gen_options: { | ||
ascii_only: false, // pass true if you want to encode non-ASCII characters as \uXXXX. | ||
inline_script: false // – pass true to escape occurrences of </script> in strings | ||
} | ||
} | ||
} | ||
exports.config = [ | ||
{ | ||
paths:{ | ||
src:[ | ||
'./test1.js-n', // -n参数,表示 此文件不压缩 | ||
'./test.js' | ||
], | ||
tar: '../tar.js' //输出的目标文件 | ||
} | ||
}, | ||
{ | ||
paths:{ | ||
src:[ | ||
'./test1.js-n', | ||
'./test.js' | ||
], | ||
tar: '../tar.js' | ||
}, | ||
options:{ | ||
strict_semicolons: false, //设为true时,代码需要写严格的分号 | ||
mangle_options: { | ||
//defines: { DEBUG: ['onename', 'false'], VERSION: ['string', '1.0'] }, | ||
except:['onename'] // 保留指定的变量名,函数名..不被修改 | ||
}, | ||
squeeze_options:{}, | ||
gen_options: { | ||
ascii_only: false, // pass true if you want to encode non-ASCII characters as \uXXXX. | ||
inline_script: false // – pass true to escape occurrences of </script> in strings | ||
} | ||
} | ||
} | ||
] | ||
说明: | ||
paths数组 -- 为需要压缩打包的文件路径列表和目标文件路径。路径相对于cp-config.js所在的目录 | ||
options -- 为压缩参数配置,例如保留关键字,转换中文等。没特殊要求,options可以不写 | ||
</pre> | ||
paths数组 -- 为需要压缩打包的文件路径列表和目标文件路径。路径相对于cp-config.js所在的目录 | ||
options -- 为压缩参数配置,例如保留关键字,转换中文等。没特殊要求,options可以不写 | ||
- 局限性 | ||
@@ -61,2 +72,5 @@ | ||
## change log ## | ||
- v0.1.1 | ||
> 支持多文件打包压缩到不同的目标文件,并兼容前一版本配置参数。2013/6/25 15:34:38 | ||
- v0.1.0 | ||
@@ -66,4 +80,6 @@ | ||
> 修改配置文件中paths字段设置。*2013/6/6 13:43:06* | ||
> 修改配置文件中paths字段设置。*2013/6/6 13:43:06* | ||
> 修复压缩引擎默认去掉文件前后分号,可能引起的“undefined is not a function”错误。*2013/6/14 17:57:48* | ||
- v0.0.1 | ||
@@ -70,0 +86,0 @@ |
exports.config = { | ||
paths:{ | ||
src:[ | ||
'./test1.js-n', // -n参数,表示 此文件不压缩 | ||
'./test.js' | ||
], | ||
tar: '../tar.js' //输出的目标文件 | ||
exports.config = [ | ||
{ | ||
paths:{ | ||
src:[ | ||
'./test1.js-n', // -n参数,表示 此文件不压缩 | ||
'./test.js' | ||
], | ||
tar: 'in/out2in.js' //输出的目标文件 | ||
}, | ||
options:{ | ||
strict_semicolons: false, //设为true时,代码需要写严格的分号 | ||
mangle_options: { | ||
//defines: { DEBUG: ['onename', 'false'], VERSION: ['string', '1.0'] }, | ||
except:['onename'] // 保留指定的变量名,函数名..不被修改 | ||
}, | ||
squeeze_options:{ | ||
}, | ||
gen_options: { | ||
ascii_only: false, // pass true if you want to encode non-ASCII characters as \uXXXX. | ||
inline_script: false // – pass true to escape occurrences of </script in strings | ||
} | ||
} | ||
}, | ||
options:{ | ||
strict_semicolons: false, //设为true时,代码需要写严格的分号 | ||
mangle_options: { | ||
//defines: { DEBUG: ['onename', 'false'], VERSION: ['string', '1.0'] }, | ||
except:['onename'] // 保留指定的变量名,函数名..不被修改 | ||
}, | ||
squeeze_options:{ | ||
}, | ||
gen_options: { | ||
ascii_only: false, // pass true if you want to encode non-ASCII characters as \uXXXX. | ||
inline_script: false // – pass true to escape occurrences of </script in strings | ||
} | ||
} | ||
} | ||
{ | ||
paths:{ | ||
src:[ | ||
'./in/tin1.js-n', // -n参数,表示 此文件不压缩 | ||
'./in/tin.js' | ||
], | ||
tar: './in2out.js' //输出的目标文件 | ||
} | ||
} | ||
] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
195940
15
4707
89