Skip to content

Commit 6d58a5d

Browse files
refactor: drop normalize-url package
1 parent d5e5802 commit 6d58a5d

File tree

4 files changed

+68
-156
lines changed

4 files changed

+68
-156
lines changed

Diff for: src/hmr/normalize-url.js

+22-51
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,40 @@
11
/* eslint-disable */
22

33
function normalizeUrl(pathComponents) {
4-
var result = [];
5-
6-
pathComponents.forEach((item) => {
7-
switch (item) {
8-
case '..':
9-
result.pop();
10-
break;
11-
case '.':
12-
break;
13-
default:
14-
result.push(item);
15-
}
16-
});
17-
18-
return result.join('/');
4+
return pathComponents
5+
.reduce((accumulator, item) => {
6+
switch (item) {
7+
case '..':
8+
accumulator.pop();
9+
break;
10+
case '.':
11+
break;
12+
default:
13+
accumulator.push(item);
14+
}
15+
16+
return accumulator;
17+
}, [])
18+
.join('/');
1919
}
2020

21-
var parseUrl = function (url) {
22-
var protocol =
23-
url.indexOf('//') !== -1 ? url.split('//')[0] + '//' : 'http://';
24-
var components = url.replace(new RegExp(protocol, 'i'), '').split('/');
25-
var host = components[0];
26-
27-
components[0] = '';
28-
29-
return {
30-
protocol: protocol,
31-
host: host.toLowerCase(),
32-
path: normalizeUrl(components),
33-
};
34-
};
35-
3621
module.exports = function (urlString) {
3722
urlString = urlString.trim();
3823

3924
if (/^data:/i.test(urlString)) {
4025
return urlString;
4126
}
4227

43-
var hasRelativeProtocol =
44-
urlString.length > 2 && urlString[0] === '/' && urlString[1] === '/';
45-
46-
var urlObj = parseUrl(urlString);
47-
48-
var keepTrailingSlash = false;
49-
50-
if (urlObj.path) {
51-
keepTrailingSlash = /\/$/i.test(urlString) && /\/$/i.test(urlObj.path);
52-
}
53-
54-
if (urlObj.host) {
55-
urlObj.host = urlObj.host.replace(/\.$/, '');
56-
}
28+
var protocol =
29+
urlString.indexOf('//') !== -1 ? urlString.split('//')[0] + '//' : '';
30+
var components = urlString.replace(new RegExp(protocol, 'i'), '').split('/');
31+
var host = components[0].toLowerCase().replace(/\.$/, '');
5732

58-
urlString = urlObj.protocol + urlObj.host + urlObj.path;
33+
components[0] = '';
5934

60-
if (!keepTrailingSlash && urlObj.hash === '') {
61-
urlString = urlString.replace(/\/$/, '');
62-
}
35+
var path = normalizeUrl(components);
6336

64-
if (hasRelativeProtocol) {
65-
urlString = urlString.replace(/^http:\/\//, '//');
66-
}
37+
urlString = protocol + host + path;
6738

6839
return urlString;
6940
};

Diff for: test/cases/hmr/expected/webpack-4/main.js

+21-50
Original file line numberDiff line numberDiff line change
@@ -1114,69 +1114,40 @@ module.exports = function (moduleId, options) {
11141114
/* eslint-disable */
11151115

11161116
function normalizeUrl(pathComponents) {
1117-
var result = [];
1118-
1119-
pathComponents.forEach((item) => {
1120-
switch (item) {
1121-
case '..':
1122-
result.pop();
1123-
break;
1124-
case '.':
1125-
break;
1126-
default:
1127-
result.push(item);
1128-
}
1129-
});
1117+
return pathComponents
1118+
.reduce((accumulator, item) => {
1119+
switch (item) {
1120+
case '..':
1121+
accumulator.pop();
1122+
break;
1123+
case '.':
1124+
break;
1125+
default:
1126+
accumulator.push(item);
1127+
}
11301128

1131-
return result.join('/');
1129+
return accumulator;
1130+
}, [])
1131+
.join('/');
11321132
}
11331133

1134-
var parseUrl = function (url) {
1135-
var protocol =
1136-
url.indexOf('//') !== -1 ? url.split('//')[0] + '//' : 'http://';
1137-
var components = url.replace(new RegExp(protocol, 'i'), '').split('/');
1138-
var host = components[0];
1139-
1140-
components[0] = '';
1141-
1142-
return {
1143-
protocol: protocol,
1144-
host: host.toLowerCase(),
1145-
path: normalizeUrl(components),
1146-
};
1147-
};
1148-
11491134
module.exports = function (urlString) {
11501135
urlString = urlString.trim();
11511136

11521137
if (/^data:/i.test(urlString)) {
11531138
return urlString;
11541139
}
11551140

1156-
var hasRelativeProtocol =
1157-
urlString.length > 2 && urlString[0] === '/' && urlString[1] === '/';
1158-
1159-
var urlObj = parseUrl(urlString);
1160-
1161-
var keepTrailingSlash = false;
1162-
1163-
if (urlObj.path) {
1164-
keepTrailingSlash = /\/$/i.test(urlString) && /\/$/i.test(urlObj.path);
1165-
}
1166-
1167-
if (urlObj.host) {
1168-
urlObj.host = urlObj.host.replace(/\.$/, '');
1169-
}
1141+
var protocol =
1142+
urlString.indexOf('//') !== -1 ? urlString.split('//')[0] + '//' : '';
1143+
var components = urlString.replace(new RegExp(protocol, 'i'), '').split('/');
1144+
var host = components[0].toLowerCase().replace(/\.$/, '');
11701145

1171-
urlString = urlObj.protocol + urlObj.host + urlObj.path;
1146+
components[0] = '';
11721147

1173-
if (!keepTrailingSlash && urlObj.hash === '') {
1174-
urlString = urlString.replace(/\/$/, '');
1175-
}
1148+
var path = normalizeUrl(components);
11761149

1177-
if (hasRelativeProtocol) {
1178-
urlString = urlString.replace(/^http:\/\//, '//');
1179-
}
1150+
urlString = protocol + host + path;
11801151

11811152
return urlString;
11821153
};

Diff for: test/cases/hmr/expected/webpack-5/main.js

+21-50
Original file line numberDiff line numberDiff line change
@@ -256,69 +256,40 @@ module.exports = function (moduleId, options) {
256256
/* eslint-disable */
257257

258258
function normalizeUrl(pathComponents) {
259-
var result = [];
260-
261-
pathComponents.forEach((item) => {
262-
switch (item) {
263-
case '..':
264-
result.pop();
265-
break;
266-
case '.':
267-
break;
268-
default:
269-
result.push(item);
270-
}
271-
});
259+
return pathComponents
260+
.reduce((accumulator, item) => {
261+
switch (item) {
262+
case '..':
263+
accumulator.pop();
264+
break;
265+
case '.':
266+
break;
267+
default:
268+
accumulator.push(item);
269+
}
272270

273-
return result.join('/');
271+
return accumulator;
272+
}, [])
273+
.join('/');
274274
}
275275

276-
var parseUrl = function (url) {
277-
var protocol =
278-
url.indexOf('//') !== -1 ? url.split('//')[0] + '//' : 'http://';
279-
var components = url.replace(new RegExp(protocol, 'i'), '').split('/');
280-
var host = components[0];
281-
282-
components[0] = '';
283-
284-
return {
285-
protocol: protocol,
286-
host: host.toLowerCase(),
287-
path: normalizeUrl(components),
288-
};
289-
};
290-
291276
module.exports = function (urlString) {
292277
urlString = urlString.trim();
293278

294279
if (/^data:/i.test(urlString)) {
295280
return urlString;
296281
}
297282

298-
var hasRelativeProtocol =
299-
urlString.length > 2 && urlString[0] === '/' && urlString[1] === '/';
300-
301-
var urlObj = parseUrl(urlString);
302-
303-
var keepTrailingSlash = false;
304-
305-
if (urlObj.path) {
306-
keepTrailingSlash = /\/$/i.test(urlString) && /\/$/i.test(urlObj.path);
307-
}
308-
309-
if (urlObj.host) {
310-
urlObj.host = urlObj.host.replace(/\.$/, '');
311-
}
283+
var protocol =
284+
urlString.indexOf('//') !== -1 ? urlString.split('//')[0] + '//' : '';
285+
var components = urlString.replace(new RegExp(protocol, 'i'), '').split('/');
286+
var host = components[0].toLowerCase().replace(/\.$/, '');
312287

313-
urlString = urlObj.protocol + urlObj.host + urlObj.path;
288+
components[0] = '';
314289

315-
if (!keepTrailingSlash && urlObj.hash === '') {
316-
urlString = urlString.replace(/\/$/, '');
317-
}
290+
var path = normalizeUrl(components);
318291

319-
if (hasRelativeProtocol) {
320-
urlString = urlString.replace(/^http:\/\//, '//');
321-
}
292+
urlString = protocol + host + path;
322293

323294
return urlString;
324295
};

Diff for: test/fixtures/json/data-urls.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
22
"main": [
3-
["example.com", "http://example.com"],
4-
["example.com ", "http://example.com"],
5-
["example.com.", "http://example.com"],
6-
["example.com", "http://example.com"],
3+
["example.com", "example.com"],
4+
["example.com ", "example.com"],
5+
["example.com.", "example.com"],
76
["http://example.com", "http://example.com"],
87
["http://example.com:80", "http://example.com:80"],
98
["https://example.com:443", "https://example.com:443"],
@@ -31,7 +30,7 @@
3130
["http://example.com/foo///bar//baz/", "http://example.com/foo///bar//baz/"],
3231
["sindre://www.sorhus.com/", "sindre://www.sorhus.com/"],
3332
["sindre://www.sorhus.com/foo/bar", "sindre://www.sorhus.com/foo/bar"],
34-
["www.example.com", "http://www.example.com"],
33+
["www.example.com", "www.example.com"],
3534
["sindre://www.sorhus.com", "sindre://www.sorhus.com"],
3635
["http://www.sorhus.xx--bck1b9a5dre4c", "http://www.sorhus.xx--bck1b9a5dre4c"],
3736
["http://www.www.example.com", "http://www.www.example.com"],

0 commit comments

Comments
 (0)