Skip to content

Commit d5e5802

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

File tree

4 files changed

+125
-71
lines changed

4 files changed

+125
-71
lines changed

src/hmr/normalize-url.js

+39-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
/* eslint-disable */
22

3-
module.exports = function (urlString) {
4-
var defaultProtocol = 'http:';
3+
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('/');
19+
}
520

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+
36+
module.exports = function (urlString) {
637
urlString = urlString.trim();
738

839
if (/^data:/i.test(urlString)) {
@@ -11,32 +42,20 @@ module.exports = function (urlString) {
1142

1243
var hasRelativeProtocol =
1344
urlString.length > 2 && urlString[0] === '/' && urlString[1] === '/';
14-
var isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
1545

16-
if (!isRelativeUrl) {
17-
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, defaultProtocol);
18-
}
19-
20-
var urlObj = new URL(urlString);
46+
var urlObj = parseUrl(urlString);
2147

2248
var keepTrailingSlash = false;
2349

24-
// Remove duplicate slashes if not preceded by a protocol
25-
if (urlObj.pathname) {
26-
urlObj.pathname = urlObj.pathname.replace(
27-
/(?<!\b(?:[a-z][a-z\d+\-.]{1,50}:))\/{2,}/g,
28-
'/'
29-
);
30-
31-
keepTrailingSlash = /\/$/i.test(urlString) && /\/$/i.test(urlObj.pathname);
50+
if (urlObj.path) {
51+
keepTrailingSlash = /\/$/i.test(urlString) && /\/$/i.test(urlObj.path);
3252
}
3353

34-
if (urlObj.hostname) {
35-
urlObj.hostname = urlObj.hostname.replace(/\.$/, '');
54+
if (urlObj.host) {
55+
urlObj.host = urlObj.host.replace(/\.$/, '');
3656
}
3757

38-
// Take advantage of many of the Node `url` normalizations
39-
urlString = urlObj.toString();
58+
urlString = urlObj.protocol + urlObj.host + urlObj.path;
4059

4160
if (!keepTrailingSlash && urlObj.hash === '') {
4261
urlString = urlString.replace(/\/$/, '');

test/cases/hmr/expected/webpack-4/main.js

+39-20
Original file line numberDiff line numberDiff line change
@@ -1113,9 +1113,40 @@ module.exports = function (moduleId, options) {
11131113

11141114
/* eslint-disable */
11151115

1116-
module.exports = function (urlString) {
1117-
var defaultProtocol = 'http:';
1116+
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+
});
1130+
1131+
return result.join('/');
1132+
}
1133+
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] = '';
11181141

1142+
return {
1143+
protocol: protocol,
1144+
host: host.toLowerCase(),
1145+
path: normalizeUrl(components),
1146+
};
1147+
};
1148+
1149+
module.exports = function (urlString) {
11191150
urlString = urlString.trim();
11201151

11211152
if (/^data:/i.test(urlString)) {
@@ -1124,32 +1155,20 @@ module.exports = function (urlString) {
11241155

11251156
var hasRelativeProtocol =
11261157
urlString.length > 2 && urlString[0] === '/' && urlString[1] === '/';
1127-
var isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
11281158

1129-
if (!isRelativeUrl) {
1130-
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, defaultProtocol);
1131-
}
1132-
1133-
var urlObj = new URL(urlString);
1159+
var urlObj = parseUrl(urlString);
11341160

11351161
var keepTrailingSlash = false;
11361162

1137-
// Remove duplicate slashes if not preceded by a protocol
1138-
if (urlObj.pathname) {
1139-
urlObj.pathname = urlObj.pathname.replace(
1140-
/(?<!\b(?:[a-z][a-z\d+\-.]{1,50}:))\/{2,}/g,
1141-
'/'
1142-
);
1143-
1144-
keepTrailingSlash = /\/$/i.test(urlString) && /\/$/i.test(urlObj.pathname);
1163+
if (urlObj.path) {
1164+
keepTrailingSlash = /\/$/i.test(urlString) && /\/$/i.test(urlObj.path);
11451165
}
11461166

1147-
if (urlObj.hostname) {
1148-
urlObj.hostname = urlObj.hostname.replace(/\.$/, '');
1167+
if (urlObj.host) {
1168+
urlObj.host = urlObj.host.replace(/\.$/, '');
11491169
}
11501170

1151-
// Take advantage of many of the Node `url` normalizations
1152-
urlString = urlObj.toString();
1171+
urlString = urlObj.protocol + urlObj.host + urlObj.path;
11531172

11541173
if (!keepTrailingSlash && urlObj.hash === '') {
11551174
urlString = urlString.replace(/\/$/, '');

test/cases/hmr/expected/webpack-5/main.js

+39-20
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,40 @@ module.exports = function (moduleId, options) {
255255

256256
/* eslint-disable */
257257

258-
module.exports = function (urlString) {
259-
var defaultProtocol = 'http:';
258+
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+
});
272+
273+
return result.join('/');
274+
}
275+
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] = '';
260283

284+
return {
285+
protocol: protocol,
286+
host: host.toLowerCase(),
287+
path: normalizeUrl(components),
288+
};
289+
};
290+
291+
module.exports = function (urlString) {
261292
urlString = urlString.trim();
262293

263294
if (/^data:/i.test(urlString)) {
@@ -266,32 +297,20 @@ module.exports = function (urlString) {
266297

267298
var hasRelativeProtocol =
268299
urlString.length > 2 && urlString[0] === '/' && urlString[1] === '/';
269-
var isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
270300

271-
if (!isRelativeUrl) {
272-
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, defaultProtocol);
273-
}
274-
275-
var urlObj = new URL(urlString);
301+
var urlObj = parseUrl(urlString);
276302

277303
var keepTrailingSlash = false;
278304

279-
// Remove duplicate slashes if not preceded by a protocol
280-
if (urlObj.pathname) {
281-
urlObj.pathname = urlObj.pathname.replace(
282-
/(?<!\b(?:[a-z][a-z\d+\-.]{1,50}:))\/{2,}/g,
283-
'/'
284-
);
285-
286-
keepTrailingSlash = /\/$/i.test(urlString) && /\/$/i.test(urlObj.pathname);
305+
if (urlObj.path) {
306+
keepTrailingSlash = /\/$/i.test(urlString) && /\/$/i.test(urlObj.path);
287307
}
288308

289-
if (urlObj.hostname) {
290-
urlObj.hostname = urlObj.hostname.replace(/\.$/, '');
309+
if (urlObj.host) {
310+
urlObj.host = urlObj.host.replace(/\.$/, '');
291311
}
292312

293-
// Take advantage of many of the Node `url` normalizations
294-
urlString = urlObj.toString();
313+
urlString = urlObj.protocol + urlObj.host + urlObj.path;
295314

296315
if (!keepTrailingSlash && urlObj.hash === '') {
297316
urlString = urlString.replace(/\/$/, '');

test/fixtures/json/data-urls.json

+8-11
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@
55
["example.com.", "http://example.com"],
66
["example.com", "http://example.com"],
77
["http://example.com", "http://example.com"],
8-
["http://example.com:80", "http://example.com"],
9-
["https://example.com:443", "https://example.com"],
8+
["http://example.com:80", "http://example.com:80"],
9+
["https://example.com:443", "https://example.com:443"],
1010
["//example.com", "//example.com"],
11-
["//example.com:80/", "//example.com/"],
12-
["ftp://example.com:21", "ftp://example.com"],
11+
["//example.com:80/", "//example.com:80/"],
12+
["ftp://example.com:21", "ftp://example.com:21"],
1313
["http://example.com:5000", "http://example.com:5000"],
1414
["http://example.com/foo/", "http://example.com/foo/"],
1515
["https://foo.com/https://bar.com", "https://foo.com/https://bar.com"],
16-
["https://foo.com/https://bar.com/foo//bar", "https://foo.com/https://bar.com/foo/bar"],
16+
["https://foo.com/https://bar.com/foo//bar", "https://foo.com/https://bar.com/foo//bar"],
1717
["https://foo.com/http://bar.com", "https://foo.com/http://bar.com"],
18-
["https://foo.com/http://bar.com/foo//bar", "https://foo.com/http://bar.com/foo/bar"],
18+
["https://foo.com/http://bar.com/foo//bar", "https://foo.com/http://bar.com/foo//bar"],
1919
["https://foo.com/%FAIL%/07/94/ca/55.jpg", "https://foo.com/%FAIL%/07/94/ca/55.jpg"],
2020
["http://example.com/?", "http://example.com/?"],
2121
["http://example.com/?b=bar&a=foo", "http://example.com/?b=bar&a=foo"],
2222
["http://example.com/?b=bar//&a=foo", "http://example.com/?b=bar//&a=foo"],
23-
["êxample.com", "http://xn--xample-hva.com"],
2423
["//example.com/", "//example.com/"],
2524
["http://example.com/foo#bar", "http://example.com/foo#bar"],
2625
["http://example.com/foo/#bar", "http://example.com/foo/#bar"],
@@ -29,16 +28,14 @@
2928
["http://example.com/foo/../bar/./baz", "http://example.com/bar/baz"],
3029
["http://example.com/foo/../bar/../baz", "http://example.com/baz"],
3130
["http://example.com/foo/../bar/../baz/", "http://example.com/baz/"],
32-
["http://example.com/foo///bar//baz/", "http://example.com/foo/bar/baz/"],
31+
["http://example.com/foo///bar//baz/", "http://example.com/foo///bar//baz/"],
3332
["sindre://www.sorhus.com/", "sindre://www.sorhus.com/"],
3433
["sindre://www.sorhus.com/foo/bar", "sindre://www.sorhus.com/foo/bar"],
3534
["www.example.com", "http://www.example.com"],
36-
["http://www.êxample.com", "http://www.xn--xample-hva.com"],
3735
["sindre://www.sorhus.com", "sindre://www.sorhus.com"],
3836
["http://www.sorhus.xx--bck1b9a5dre4c", "http://www.sorhus.xx--bck1b9a5dre4c"],
3937
["http://www.www.example.com", "http://www.www.example.com"],
40-
["example.com/?foo=bar baz", "http://example.com/?foo=bar%20baz"],
4138
["https://user:[email protected]/", "https://user:[email protected]/"],
42-
["https://EXAMPLE.com/../x", "https://example.com/x"]
39+
["https://EXAMPLE.com/./x", "https://example.com/x"]
4340
]
4441
}

0 commit comments

Comments
 (0)