Skip to content

Commit c5b8778

Browse files
committed
使用压缩工具UglifyJS
1 parent c4376f8 commit c5b8778

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

build/compile.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var marked = require('marked');
66
var watch = require('watch');
77
var stylus = require('stylus')
88
var highlight = require('highlight.js')
9+
var UglifyJS = require("uglify-js");
910
var renderer = new marked.Renderer();
1011
var color = require('colors-cli/safe');
1112
var error = color.red.bold;
@@ -58,13 +59,10 @@ exec('rm -rf .deploy');
5859
// 生成 项目所需的文件
5960
CreateDatajs('./.deploy/js/dt.js',function(dt_path,arr){
6061

61-
cp(path.join(path_root,'/template/js/index.js'),
62-
path.join(path_root,'/.deploy/js/index.js'),function(err,r){
63-
if(err) return console.log(error(" → error:"),err.errno);
64-
console.log(success(" → ") + '/template/js/index.js');
65-
})
62+
CreateJS('/template/js/index.js','/.deploy/js/index.js')
6663

6764
CreateStyl('/template/styl/index.styl','/.deploy/css/index.css')
65+
6866
// 首页生成
6967
ReadTmpToHTML('/template/index.ejs','/.deploy/index.html',null,{
7068
'p':'/index.html',
@@ -102,11 +100,7 @@ watch.watchTree(path.join(path.dirname(__dirname),'/'), function (f, curr, prev)
102100
CreateStyl('/template/styl/index.styl','/.deploy/css/index.css')
103101
}else if(/\.js$/.test(f)){
104102

105-
cp(path.join(path_root,'/template/js/index.js'),
106-
path.join(path_root,'/.deploy/js/index.js'),function(err,r){
107-
if(err) return console.log(error(" → error:"),err.errno);
108-
console.log(success(" → ") + '/template/js/index.js');
109-
})
103+
CreateJS('/template/js/index.js','/.deploy/js/index.js')
110104

111105
}else if(/\.ejs$/.test(f)){
112106
// 首页生成
@@ -122,6 +116,18 @@ watch.watchTree(path.join(path.dirname(__dirname),'/'), function (f, curr, prev)
122116
})
123117

124118

119+
function CreateJS(from_path,to_path){
120+
121+
// 生成到指定目录
122+
var new_to_path = path.join(path.dirname(__dirname),to_path);
123+
// 循环创建目录
124+
mkdirsSync(path.dirname(new_to_path));
125+
var js_code = UglifyJS.minify(path.join(path_root,from_path), { mangle: { toplevel: true } });
126+
fs.writeFileSync(new_to_path, js_code.code);
127+
console.log(success(" → ")+to_path + '');
128+
129+
}
130+
125131
/**
126132
* [ReadTmpToHTML ejs 模板转换成HTML]
127133
* @param {[type]} from_path [模版来源地址]

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
},
3434
"dependencies": {
3535
"gh-pages": "^0.12.0",
36-
"loading-cli": "^1.0.2"
36+
"loading-cli": "^1.0.2",
37+
"uglify-js": "^2.7.4"
3738
}
3839
}

0 commit comments

Comments
 (0)