forked from zhenn/zenbone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
112 lines (91 loc) · 3.34 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var _process = require('child_process');
var colors = require('colors');
var filetool = require('./base/filetool');
var path = require('path');
var fs = require('fs');
var package;
if (fs.existsSync(process.cwd() + '/package.json')) {
package = require(process.cwd() + '/package.json');
}
module.exports = {
main: function(opt) {
var self = this;
var env = self.env = opt.product ? 'product' : 'stage';
filetool.rmdirSync(process.cwd() + '/build');
self.createAssets(opt, function() {
self.buildHTML();
console.log('\n项目构建完成...'.green);
console.log((env + '环境哦 ' + env).red);
});
},
createAssets: function(opt, callback) {
var env = this.env;
console.log('正在构建...'.yellow);
var buildChild = _process.exec('NODE_ENV=' + env + ' webpack -p', function() {
callback();
});
buildChild.stdout.on('data', function(data) {
console.log(data);
});
},
// reutrn:
// [ '/Users/zhenn/ddd/index.html' ]
getHtmlFiles: function() {
var _path = process.cwd();
var contains = filetool.getContains(_path).list,
arr = [];
contains.forEach(function(item, i) {
if (item.type != 'file') {
return;
}
var file = _path + '/' + item.name;
if (path.extname(file) == '.html' || path.extname(file) == '.php') {
arr.push(file);
}
});
return arr;
},
compileHTML: function(content) {
var package = require(process.cwd() + '/package.json'),
name = package.name,
group = package.gitlabGroup,
version = package.version,
scriptReg = /<script +?src="(.+?).js">[\s\S]*?<\/script>/gi,
linkReg = /<script/i,
fileKey,
prehost = '/';
if (this.env == 'product') {
prehost = '//' + package.cdnDomain + '/';
}
content = content.replace(scriptReg, function($1, $2) {
if ($2.indexOf('/') >= 0) {
return $1;
}
fileKey = $2;
return $1.replace($2, prehost + group + '/' + name + '/' + version + '/assets/' + $2);
});
// content = content.replace(linkReg, );
content = content.replace(linkReg, function($1, $2) {
return '<link type="text/css" rel="stylesheet" href="' + prehost + group + '/' + name + '/' + version + '/assets/' + fileKey + '.css" />\n' + $1;
});
return content;
},
buildHTML: function() {
var self = this;
var htmlFiles = self.getHtmlFiles();
htmlFiles.forEach(function(item, i) {
self.buildSingleHTML(item);
});
},
// 输入文件路径, 创建编译后的html文件到build目录
buildSingleHTML: function(file) {
var self = this;
var cwd = process.cwd();
var targetPath = cwd + '/build' + file.replace(cwd, '');
var source = fs.readFileSync(file, 'utf-8');
console.log(('\n打包html:').green);
console.log(file.gray + ' =====> '.gray + targetPath.gray + ' 100%'.white);
var compiledContent = this.compileHTML(source);
filetool.writefile(targetPath, compiledContent);
}
};