UNPKG

5.49 kBJavaScriptView Raw
1/**
2 * Created with JetBrains WebStorm.
3 * User: 陶清
4 * Date: 13-11-1
5 * Time: 下午4:05
6 * Asset Url处理模块
7 */
8var debug = require('debug')('clam:assetUrl');
9var config = require('./config.js');
10var path = require('path');
11var join = path.join;
12var _ = require('underscore');
13
14var scriptReg = /<script[^>]*? src\s{0,}=\s{0,}['"]\s{0,}(?!http(s)?:\/\/)(?!\/)([^"']*?)\s{0,}['"][^>]*?><\\?\/script>/g;
15var styleReg = /<link[^>]*? href\s{0,}=\s{0,}['"]\s{0,}(?!http(s)?:\/\/)(?!\/)([^"']*?)\s{0,}['"][^>]*?>/g;
16var absScript = /<script[^>]*? src\s{0,}=['"]\s{0,}(\/.+?)\s{0,}['"][^>]*?><\/script>/g;
17var absStyle = /<link[^>]*? href\s{0,}=\s{0,}['"]\s{0,}(\/.+?)\s{0,}['"][^>]*?>/g;
18var cdnPathReg = /http[s]?:\/\/[\w\.]+[\/.]*?/g;
19
20/**
21 * 解析在工程文件中配置的被解析到127.0.0.1的本地域名
22 * @returns {Array}
23 */
24function localhosts(){
25 var strHosts = config.get('project').hosts;
26
27 var lines = strHosts.split(/[\n\r]{1,2}/);
28 var hosts = [];
29 lines.forEach(function(str){
30 var units = str.split(/\s+/);
31 if(units[0] == '127.0.0.1'){
32 hosts = hosts.concat(units.slice(1,units.length));
33 }
34 });
35 hosts = _.union(hosts);
36 return hosts;
37}
38
39/**
40 * 处理页面内容中的资源路径
41 * 1. 本地相对路径转换为绝对路径
42 * 2. 本地绝对路径增加端口号
43 * 3. 以全cdnPath开头引入的格式资源,需要按照配置修改端口号,保证运行在非80端口上。
44 *
45 * @param pageContent 页面内容
46 * @param urlDir 页面/模块文件相对根路径的位置
47 * @return {String} 替换后的页面内容
48 */
49function toAbsolutePath(pageContent, urlDir) {
50 var cdnPath = config.get('project').cdnPath;
51
52 //处理模块中资源引用
53 var localHosts = localhosts();
54 debug('本地host列表[%s]', localHosts);
55
56 /*/抽取cdn域名
57 var cdnHost = cdnPath.match(cdnPathReg);
58 if (cdnHost) {
59 cdnHost = cdnHost[0];
60 }
61 else {
62 cdnHost = '';
63 }*/
64
65 //以http开头的就改变cndPath中的端口号
66 var port = config.get('project').port;
67 var cdnPathWithPort = cdnPath;
68 if (cdnPath.indexOf('http') === 0) {
69 //如果是预览服务器运行在80端口,就不更改
70 if (!port || port == 80) {
71 port = '';
72 }
73 else {
74 port = ':' + port;
75 }
76 var matchResult = cdnPath.match(/(http:\/\/[\w\.]+?)(\/.*)/);
77 if (matchResult) {
78 cdnPathWithPort = matchResult[1] + port + matchResult[2];
79 }
80 }
81
82 var resourceRootDir = '';
83 debug('资源跟路径%s', resourceRootDir);
84 //debug('处理前内容%s', pageContent);
85 //处理带域名的链接。1、增加端口号。2、增加时间戳路径
86 localHosts.forEach(function(localHost){
87 localHost = localHost.replace(/\./g, '\\.');
88 var scriptWithDomain = new RegExp('<script[^>]*? src=[\'"](?:http[s]?:\\/\\/)(' + localHost + ')(.*)[\'"][^>]*?>', 'g');
89 pageContent = pageContent.replace(scriptWithDomain, function ($1, $2) {
90 return $1.replace($2, $2 + port);
91 });
92 });
93
94 localHosts.forEach(function(localHost){
95 localHost = localHost.replace(/\./g, '\\.');
96 var styleWithDomain = new RegExp('<link[^>]*? href=[\'"](?:http[s]?:\\/\\/)(' + localHost + ')(.*)[\'"][^>]*?>', 'g');
97 pageContent = pageContent.replace(styleWithDomain, function ($1, $2) {
98 return $1.replace($2, $2 + port);
99 });
100 });
101 //替换资源相对路径应为带域名和端口号的绝对路径,根据当前页面所在相对路径转换
102 pageContent = pageContent.replace(scriptReg, function ($1, $2, $3) {
103 if ($3.match(/^\$\{/)) {
104 return $1;
105 }
106 else {
107 var replacedPath = join(resourceRootDir, urlDir, $3).replace(/\\/ig, '/');
108 if ($1.match(/clam-moveto/) && !$1.match(/clam-moveto\s{0,}=\s{0,}["']\s{0,}head\s{0,}["']/) && !$1.match(/clam-moveto\s{0,}=\s{0,}["']\s{0,}tail\s{0,}["']/)) {
109 replacedPath = "$CLAM_VER$/"+replacedPath;
110 }
111 return $1.replace($3, cdnPathWithPort + '/' + replacedPath).replace(/["'](\s{0,})([^"']*?)(\s{0,})["']/g, function($1,$2,$3) {
112 return '"'+$3+'"';
113 });
114 }
115 });
116 pageContent = pageContent.replace(styleReg, function ($1, $2, $3) {
117 if ($3.match(/^\$\{/)) {
118 return $1;
119 }
120 else {
121 var replacedPath = join(resourceRootDir, urlDir, $3).replace(/\\/ig, '/');
122 if ($1.match(/clam-moveto/) && !$1.match(/clam-moveto\s{0,}=\s{0,}["']\s{0,}head\s{0,}["']/) && !$1.match(/clam-moveto\s{0,}=\s{0,}["']\s{0,}tail\s{0,}["']/)) {
123 console.log(replacedPath)
124 replacedPath = "$CLAM_VER$/"+replacedPath;
125 }
126 return $1.replace($3, cdnPathWithPort + '/' + replacedPath).replace(/["'](\s{0,})([^"']*?)(\s{0,})["']/g, function($1,$2,$3) {
127 return '"'+$3+'"';
128 });
129 }
130 });
131
132 //处理绝对路径引用。直接增加域名和端口号
133 pageContent = pageContent.replace(absScript, function ($1, $2) {
134 return $1.replace($2, cdnPathWithPort + $2);
135 });
136 pageContent = pageContent.replace(absStyle, function ($1, $2) {
137 return $1.replace($2, cdnPathWithPort + $2);
138 });
139 //debug('处理后内容%s', pageContent);
140 return pageContent;
141}
142
143exports.toAbsolutePath = toAbsolutePath;