-
Notifications
You must be signed in to change notification settings - Fork 21
/
make_vue_script.php
94 lines (84 loc) · 2.31 KB
/
make_vue_script.php
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
<?php
/**
* User: loveyu
* Date: 2015/3/24
* Time: 23:32
*/
header("Content-Type: application/javascript; charset=utf-8");
$page = [];
define("VUE_PATH", __DIR__ . "/web_resource/vue/");
//加载工具库
foreach(glob(VUE_PATH . "script/utils/*.js") as $v){
$page[] = "utils/" . basename($v);
}
//加载根目录脚本
foreach(glob(VUE_PATH . "script/*.js") as $v){
$v = basename($v);
$page[] = $v;
}
//加载页面脚本
foreach(glob(VUE_PATH . "script/page/*.js") as $v){
$page[] = "page/" . basename($v);
}
$out_data = "";
//开始解析数据
foreach($page as $v){
if(!is_file(VUE_PATH . "script/{$v}")){
continue;
}
$content = file_get_contents(VUE_PATH . "script/{$v}");
$out_data .= parse($content) . "\n\n\n";
}
file_put_contents(VUE_PATH . "dist.js", $out_data);
//解析模板信息
function parse($content){
preg_match_all("/{[\\s]*__require:[\\s]*['|\"]([a-zA-Z0-9_-]+)\\/([a-zA-Z0-9_-]+)\\.html['|\"][\\s]*}/", $content, $matches, PREG_SET_ORDER);
foreach($matches as $v){
if(!isset($v[2])){
continue;
}
$content = str_replace($v[0], get_template($v[1] . "/" . $v[2]), $content);
}
return $content;
}
//读取模板信息
function get_template($path){
$object = ['template' => ''];
$path = VUE_PATH . "script/template/" . $path;
if(is_file($path . ".min.html")){
$content = file_get_contents($path . ".min.html");
$object['template'] = json_encode($content, JSON_UNESCAPED_UNICODE);
} elseif(is_file($path . ".html")){
$content = file_get_contents($path . ".html");
$object['template'] = json_encode(compress_html($content), JSON_UNESCAPED_UNICODE);
} else{
$object['template'] = json_encode("<h3 class='alert alert-danger'>模板未找到!!!</h3>", JSON_UNESCAPED_UNICODE);
}
if(is_file($path . ".js")){
$content = file_get_contents($path . ".js");
foreach([
'methods',
'paramAttributes',
'data',
'props',
'created',
'data'
] as $v){
$match = [];
if(preg_match("/_{$v}_[ ]*=[ ]*([\\s\\S]+?)[;]*\\/\\/_{$v}_/", $content, $match)){
if(isset($match[1])){
$object[$v] = $match[1];
}
}
}
}
$map = [];
foreach($object as $name => $v){
$map[] = $name . ":" . $v;
}
return "{" . implode(",", $map) . "}";
}
//压缩HTML文件
function compress_html($string){
return preg_replace("/[ ]{2,}/", " ", preg_replace("/[\\n\\r\\t]+/", " ", $string));
}