Skip to content

Commit d7bc895

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

File tree

6 files changed

+61
-366
lines changed

6 files changed

+61
-366
lines changed

src/hmr/hotModuleReplacement.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ function getCurrentScriptUrl(moduleId) {
7070
const reg = new RegExp(`${filename}\\.js$`, 'g');
7171

7272
return normalizeUrl(
73-
src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`),
74-
{ stripWWW: false }
73+
src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`)
7574
);
7675
});
7776
};

src/hmr/normalize-url.js

+7-98
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,10 @@
11
/* eslint-disable */
22

3-
function testParameter(name, filters) {
4-
return filters.some(function (filter) {
5-
return filter instanceof RegExp ? filter.test(name) : filter === name;
6-
});
7-
}
8-
9-
module.exports = function (urlString, options) {
10-
var normalizeOptions = Object.assign(
11-
{},
12-
{
13-
defaultProtocol: 'http:',
14-
normalizeProtocol: true,
15-
forceHttp: false,
16-
forceHttps: false,
17-
stripAuthentication: true,
18-
stripHash: false,
19-
stripWWW: true,
20-
removeQueryParameters: [/^utm_\w+/i],
21-
removeTrailingSlash: true,
22-
removeSingleSlash: true,
23-
removeDirectoryIndex: false,
24-
sortQueryParameters: true,
25-
},
26-
options
27-
);
3+
module.exports = function (urlString) {
4+
var defaultProtocol = 'http:';
285

296
urlString = urlString.trim();
307

31-
// Data URL
328
if (/^data:/i.test(urlString)) {
339
return urlString;
3410
}
@@ -37,103 +13,36 @@ module.exports = function (urlString, options) {
3713
urlString.length > 2 && urlString[0] === '/' && urlString[1] === '/';
3814
var isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
3915

40-
// Prepend protocol
4116
if (!isRelativeUrl) {
42-
// eslint-disable-next-line no-param-reassign
43-
urlString = urlString.replace(
44-
/^(?!(?:\w+:)?\/\/)|^\/\//,
45-
normalizeOptions.defaultProtocol
46-
);
17+
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, defaultProtocol);
4718
}
4819

4920
var urlObj = new URL(urlString);
5021

51-
// Remove auth
52-
if (normalizeOptions.stripAuthentication) {
53-
urlObj.username = '';
54-
urlObj.password = '';
55-
}
56-
57-
// Remove hash
58-
if (normalizeOptions.stripHash) {
59-
urlObj.hash = '';
60-
}
22+
var keepTrailingSlash = false;
6123

6224
// Remove duplicate slashes if not preceded by a protocol
6325
if (urlObj.pathname) {
6426
urlObj.pathname = urlObj.pathname.replace(
6527
/(?<!\b(?:[a-z][a-z\d+\-.]{1,50}:))\/{2,}/g,
6628
'/'
6729
);
68-
}
6930

70-
// Decode URI octets
71-
if (urlObj.pathname) {
72-
try {
73-
urlObj.pathname = decodeURI(urlObj.pathname);
74-
// eslint-disable-next-line no-empty
75-
} catch (_) {}
31+
keepTrailingSlash = /\/$/i.test(urlString) && /\/$/i.test(urlObj.pathname);
7632
}
7733

7834
if (urlObj.hostname) {
79-
// Remove trailing dot
8035
urlObj.hostname = urlObj.hostname.replace(/\.$/, '');
81-
82-
// Remove `www.`
83-
if (
84-
normalizeOptions.stripWWW &&
85-
/^www\.(?!www\.)(?:[a-z\-\d]{1,63})\.(?:[a-z.\-\d]{2,63})$/.test(
86-
urlObj.hostname
87-
)
88-
) {
89-
urlObj.hostname = urlObj.hostname.replace(/^www\./, '');
90-
}
91-
}
92-
93-
// Remove query unwanted parameters
94-
if (Array.isArray(normalizeOptions.removeQueryParameters)) {
95-
for (var key in Array.from(urlObj.searchParams.keys())) {
96-
if (testParameter(key, normalizeOptions.removeQueryParameters)) {
97-
urlObj.searchParams.delete(key);
98-
}
99-
}
100-
}
101-
102-
// Sort query parameters
103-
if (normalizeOptions.sortQueryParameters) {
104-
urlObj.searchParams.sort();
105-
}
106-
107-
if (normalizeOptions.removeTrailingSlash) {
108-
urlObj.pathname = urlObj.pathname.replace(/\/$/, '');
10936
}
11037

111-
var oldUrlString = urlString;
112-
11338
// Take advantage of many of the Node `url` normalizations
11439
urlString = urlObj.toString();
11540

116-
if (
117-
!normalizeOptions.removeSingleSlash &&
118-
urlObj.pathname === '/' &&
119-
oldUrlString.indexOf('/') !== oldUrlString.length - 1 &&
120-
urlObj.hash === ''
121-
) {
122-
urlString = urlString.replace(/\/$/, '');
123-
}
124-
125-
// Remove ending `/` unless removeSingleSlash is false
126-
if (
127-
(normalizeOptions.removeTrailingSlash || urlObj.pathname === '/') &&
128-
urlObj.hash === '' &&
129-
normalizeOptions.removeSingleSlash
130-
) {
41+
if (!keepTrailingSlash && urlObj.hash === '') {
13142
urlString = urlString.replace(/\/$/, '');
13243
}
13344

134-
// Restore relative protocol, if applicable
135-
if (hasRelativeProtocol && !normalizeOptions.normalizeProtocol) {
136-
// eslint-disable-next-line no-param-reassign
45+
if (hasRelativeProtocol) {
13746
urlString = urlString.replace(/^http:\/\//, '//');
13847
}
13948

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

+8-100
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,7 @@ function getCurrentScriptUrl(moduleId) {
949949
const reg = new RegExp(`${filename}\\.js$`, 'g');
950950

951951
return normalizeUrl(
952-
src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`),
953-
{ stripWWW: false }
952+
src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`)
954953
);
955954
});
956955
};
@@ -1114,35 +1113,11 @@ module.exports = function (moduleId, options) {
11141113

11151114
/* eslint-disable */
11161115

1117-
function testParameter(name, filters) {
1118-
return filters.some(function (filter) {
1119-
return filter instanceof RegExp ? filter.test(name) : filter === name;
1120-
});
1121-
}
1122-
1123-
module.exports = function (urlString, options) {
1124-
var normalizeOptions = Object.assign(
1125-
{},
1126-
{
1127-
defaultProtocol: 'http:',
1128-
normalizeProtocol: true,
1129-
forceHttp: false,
1130-
forceHttps: false,
1131-
stripAuthentication: true,
1132-
stripHash: false,
1133-
stripWWW: true,
1134-
removeQueryParameters: [/^utm_\w+/i],
1135-
removeTrailingSlash: true,
1136-
removeSingleSlash: true,
1137-
removeDirectoryIndex: false,
1138-
sortQueryParameters: true,
1139-
},
1140-
options
1141-
);
1116+
module.exports = function (urlString) {
1117+
var defaultProtocol = 'http:';
11421118

11431119
urlString = urlString.trim();
11441120

1145-
// Data URL
11461121
if (/^data:/i.test(urlString)) {
11471122
return urlString;
11481123
}
@@ -1151,103 +1126,36 @@ module.exports = function (urlString, options) {
11511126
urlString.length > 2 && urlString[0] === '/' && urlString[1] === '/';
11521127
var isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
11531128

1154-
// Prepend protocol
11551129
if (!isRelativeUrl) {
1156-
// eslint-disable-next-line no-param-reassign
1157-
urlString = urlString.replace(
1158-
/^(?!(?:\w+:)?\/\/)|^\/\//,
1159-
normalizeOptions.defaultProtocol
1160-
);
1130+
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, defaultProtocol);
11611131
}
11621132

11631133
var urlObj = new URL(urlString);
11641134

1165-
// Remove auth
1166-
if (normalizeOptions.stripAuthentication) {
1167-
urlObj.username = '';
1168-
urlObj.password = '';
1169-
}
1170-
1171-
// Remove hash
1172-
if (normalizeOptions.stripHash) {
1173-
urlObj.hash = '';
1174-
}
1135+
var keepTrailingSlash = false;
11751136

11761137
// Remove duplicate slashes if not preceded by a protocol
11771138
if (urlObj.pathname) {
11781139
urlObj.pathname = urlObj.pathname.replace(
11791140
/(?<!\b(?:[a-z][a-z\d+\-.]{1,50}:))\/{2,}/g,
11801141
'/'
11811142
);
1182-
}
11831143

1184-
// Decode URI octets
1185-
if (urlObj.pathname) {
1186-
try {
1187-
urlObj.pathname = decodeURI(urlObj.pathname);
1188-
// eslint-disable-next-line no-empty
1189-
} catch (_) {}
1144+
keepTrailingSlash = /\/$/i.test(urlString) && /\/$/i.test(urlObj.pathname);
11901145
}
11911146

11921147
if (urlObj.hostname) {
1193-
// Remove trailing dot
11941148
urlObj.hostname = urlObj.hostname.replace(/\.$/, '');
1195-
1196-
// Remove `www.`
1197-
if (
1198-
normalizeOptions.stripWWW &&
1199-
/^www\.(?!www\.)(?:[a-z\-\d]{1,63})\.(?:[a-z.\-\d]{2,63})$/.test(
1200-
urlObj.hostname
1201-
)
1202-
) {
1203-
urlObj.hostname = urlObj.hostname.replace(/^www\./, '');
1204-
}
12051149
}
12061150

1207-
// Remove query unwanted parameters
1208-
if (Array.isArray(normalizeOptions.removeQueryParameters)) {
1209-
for (var key in Array.from(urlObj.searchParams.keys())) {
1210-
if (testParameter(key, normalizeOptions.removeQueryParameters)) {
1211-
urlObj.searchParams.delete(key);
1212-
}
1213-
}
1214-
}
1215-
1216-
// Sort query parameters
1217-
if (normalizeOptions.sortQueryParameters) {
1218-
urlObj.searchParams.sort();
1219-
}
1220-
1221-
if (normalizeOptions.removeTrailingSlash) {
1222-
urlObj.pathname = urlObj.pathname.replace(/\/$/, '');
1223-
}
1224-
1225-
var oldUrlString = urlString;
1226-
12271151
// Take advantage of many of the Node `url` normalizations
12281152
urlString = urlObj.toString();
12291153

1230-
if (
1231-
!normalizeOptions.removeSingleSlash &&
1232-
urlObj.pathname === '/' &&
1233-
oldUrlString.indexOf('/') !== oldUrlString.length - 1 &&
1234-
urlObj.hash === ''
1235-
) {
1236-
urlString = urlString.replace(/\/$/, '');
1237-
}
1238-
1239-
// Remove ending `/` unless removeSingleSlash is false
1240-
if (
1241-
(normalizeOptions.removeTrailingSlash || urlObj.pathname === '/') &&
1242-
urlObj.hash === '' &&
1243-
normalizeOptions.removeSingleSlash
1244-
) {
1154+
if (!keepTrailingSlash && urlObj.hash === '') {
12451155
urlString = urlString.replace(/\/$/, '');
12461156
}
12471157

1248-
// Restore relative protocol, if applicable
1249-
if (hasRelativeProtocol && !normalizeOptions.normalizeProtocol) {
1250-
// eslint-disable-next-line no-param-reassign
1158+
if (hasRelativeProtocol) {
12511159
urlString = urlString.replace(/^http:\/\//, '//');
12521160
}
12531161

0 commit comments

Comments
 (0)