1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var debug = require('debug')('clam:assetUrl');
|
9 | var config = require('./config.js');
|
10 | var path = require('path');
|
11 | var join = path.join;
|
12 | var _ = require('underscore');
|
13 |
|
14 | var scriptReg = /<script[^>]*? src\s{0,}=\s{0,}['"]\s{0,}(?!http(s)?:\/\/)(?!\/)([^"']*?)\s{0,}['"][^>]*?><\\?\/script>/g;
|
15 | var styleReg = /<link[^>]*? href\s{0,}=\s{0,}['"]\s{0,}(?!http(s)?:\/\/)(?!\/)([^"']*?)\s{0,}['"][^>]*?>/g;
|
16 | var absScript = /<script[^>]*? src\s{0,}=['"]\s{0,}(\/.+?)\s{0,}['"][^>]*?><\/script>/g;
|
17 | var absStyle = /<link[^>]*? href\s{0,}=\s{0,}['"]\s{0,}(\/.+?)\s{0,}['"][^>]*?>/g;
|
18 | var cdnPathReg = /http[s]?:\/\/[\w\.]+[\/.]*?/g;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function 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 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function toAbsolutePath(pageContent, urlDir) {
|
50 | var cdnPath = config.get('project').cdnPath;
|
51 |
|
52 |
|
53 | var localHosts = localhosts();
|
54 | debug('本地host列表[%s]', localHosts);
|
55 |
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | var port = config.get('project').port;
|
67 | var cdnPathWithPort = cdnPath;
|
68 | if (cdnPath.indexOf('http') === 0) {
|
69 |
|
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 |
|
85 |
|
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 |
|
140 | return pageContent;
|
141 | }
|
142 |
|
143 | exports.toAbsolutePath = toAbsolutePath;
|