Skip to content

Commit 199749f

Browse files
test: update
1 parent 1da1255 commit 199749f

File tree

4 files changed

+145
-119
lines changed

4 files changed

+145
-119
lines changed

src/hmr/hotModuleReplacement.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ function getCurrentScriptUrl(moduleId) {
100100
}
101101

102102
/**
103-
* @param {string} url url
104-
* @returns {boolean} true when is url can be requested, otherwise false
103+
* @param {string} url URL
104+
* @returns {boolean} true when URL can be request, otherwise false
105105
*/
106106
function isUrlRequest(url) {
107107
// An URL is not an request if
@@ -138,7 +138,8 @@ function updateCss(el, url) {
138138
return;
139139
}
140140

141-
if (!url || !url.includes(".css")) {
141+
// eslint-disable-next-line unicorn/prefer-includes
142+
if (!url || !(url.indexOf(".css") > -1)) {
142143
return;
143144
}
144145

@@ -191,7 +192,8 @@ function getReloadUrl(href, src) {
191192
*/
192193
// eslint-disable-next-line array-callback-return
193194
(url) => {
194-
if (href.includes(src)) {
195+
// eslint-disable-next-line unicorn/prefer-includes
196+
if (href.indexOf(src) > -1) {
195197
ret = url;
196198
}
197199
},

src/hmr/normalize-url.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ module.exports = function normalizeUrl(urlString) {
3131
return urlString;
3232
}
3333

34-
const protocol = urlString.includes("//")
35-
? `${urlString.split("//")[0]}//`
36-
: "";
34+
const protocol =
35+
// eslint-disable-next-line unicorn/prefer-includes
36+
urlString.indexOf("//") !== -1 ? `${urlString.split("//")[0]}//` : "";
3737
const components = urlString
3838
.replace(new RegExp(protocol, "i"), "")
3939
.split("/");

test/cases/hmr-locals/expected/main.js

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
\************************************************/
88
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
99

10-
/* eslint-env browser */
10+
/* global document */
1111
/*
1212
eslint-disable
1313
no-console,
1414
func-names
1515
*/
1616

17+
// eslint-disable-next-line jsdoc/no-restricted-syntax
1718
/** @typedef {any} TODO */
1819

1920
const normalizeUrl = __webpack_require__(/*! ./normalize-url */ "../../../src/hmr/normalize-url.js");
@@ -24,36 +25,40 @@ const noDocument = typeof document === "undefined";
2425

2526
const { forEach } = Array.prototype;
2627

28+
// eslint-disable-next-line jsdoc/no-restricted-syntax
2729
/**
28-
* @param {function} fn
29-
* @param {number} time
30-
* @returns {(function(): void)|*}
30+
* @param {Function} fn any function
31+
* @param {number} time time
32+
* @returns {() => void} wrapped function
3133
*/
3234
function debounce(fn, time) {
3335
let timeout = 0;
3436

3537
return function () {
36-
// @ts-ignore
38+
// @ts-expect-error
3739
const self = this;
3840
// eslint-disable-next-line prefer-rest-params
3941
const args = arguments;
40-
42+
// eslint-disable-next-line func-style
4143
const functionCall = function functionCall() {
4244
return fn.apply(self, args);
4345
};
4446

4547
clearTimeout(timeout);
4648

47-
// @ts-ignore
49+
// @ts-expect-error
4850
timeout = setTimeout(functionCall, time);
4951
};
5052
}
5153

54+
/**
55+
* @returns {void}
56+
*/
5257
function noop() {}
5358

5459
/**
55-
* @param {TODO} moduleId
56-
* @returns {TODO}
60+
* @param {string | number} moduleId a module id
61+
* @returns {TODO} current script url
5762
*/
5863
function getCurrentScriptUrl(moduleId) {
5964
let src = srcByModuleId[moduleId];
@@ -74,8 +79,8 @@ function getCurrentScriptUrl(moduleId) {
7479
}
7580

7681
/**
77-
* @param {string} fileMap
78-
* @returns {null | string[]}
82+
* @param {string} fileMap file map
83+
* @returns {null | string[]} normalized files
7984
*/
8085
return function (fileMap) {
8186
if (!src) {
@@ -97,15 +102,30 @@ function getCurrentScriptUrl(moduleId) {
97102
const reg = new RegExp(`${filename}\\.js$`, "g");
98103

99104
return normalizeUrl(
100-
src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`)
105+
src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`),
101106
);
102107
});
103108
};
104109
}
105110

106111
/**
107-
* @param {TODO} el
108-
* @param {string} [url]
112+
* @param {string} url URL
113+
* @returns {boolean} true when URL can be request, otherwise false
114+
*/
115+
function isUrlRequest(url) {
116+
// An URL is not an request if
117+
118+
// It is not http or https
119+
if (!/^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url)) {
120+
return false;
121+
}
122+
123+
return true;
124+
}
125+
126+
/**
127+
* @param {TODO} el html link element
128+
* @param {string=} url a URL
109129
*/
110130
function updateCss(el, url) {
111131
if (!url) {
@@ -127,11 +147,11 @@ function updateCss(el, url) {
127147
return;
128148
}
129149

150+
// eslint-disable-next-line unicorn/prefer-includes
130151
if (!url || !(url.indexOf(".css") > -1)) {
131152
return;
132153
}
133154

134-
// eslint-disable-next-line no-param-reassign
135155
el.visited = true;
136156

137157
const newEl = el.cloneNode();
@@ -166,34 +186,34 @@ function updateCss(el, url) {
166186
}
167187

168188
/**
169-
* @param {string} href
170-
* @param {TODO} src
171-
* @returns {TODO}
189+
* @param {string} href href
190+
* @param {TODO} src src
191+
* @returns {undefined | string} a reload url
172192
*/
173193
function getReloadUrl(href, src) {
174194
let ret;
175195

176-
// eslint-disable-next-line no-param-reassign
177196
href = normalizeUrl(href);
178197

179198
src.some(
180199
/**
181-
* @param {string} url
200+
* @param {string} url url
182201
*/
183202
// eslint-disable-next-line array-callback-return
184203
(url) => {
204+
// eslint-disable-next-line unicorn/prefer-includes
185205
if (href.indexOf(src) > -1) {
186206
ret = url;
187207
}
188-
}
208+
},
189209
);
190210

191211
return ret;
192212
}
193213

194214
/**
195-
* @param {string} [src]
196-
* @returns {boolean}
215+
* @param {string=} src source
216+
* @returns {boolean} true when loaded, otherwise false
197217
*/
198218
function reloadStyle(src) {
199219
if (!src) {
@@ -210,7 +230,7 @@ function reloadStyle(src) {
210230

211231
const url = getReloadUrl(el.href, src);
212232

213-
if (!isUrlRequest(url)) {
233+
if (url && !isUrlRequest(url)) {
214234
return;
215235
}
216236

@@ -228,6 +248,9 @@ function reloadStyle(src) {
228248
return loaded;
229249
}
230250

251+
/**
252+
* @returns {void}
253+
*/
231254
function reloadAll() {
232255
const elements = document.querySelectorAll("link");
233256

@@ -241,24 +264,9 @@ function reloadAll() {
241264
}
242265

243266
/**
244-
* @param {string} url
245-
* @returns {boolean}
246-
*/
247-
function isUrlRequest(url) {
248-
// An URL is not an request if
249-
250-
// It is not http or https
251-
if (!/^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url)) {
252-
return false;
253-
}
254-
255-
return true;
256-
}
257-
258-
/**
259-
* @param {TODO} moduleId
260-
* @param {TODO} options
261-
* @returns {TODO}
267+
* @param {number | string} moduleId a module id
268+
* @param {TODO} options options
269+
* @returns {TODO} wrapper function
262270
*/
263271
module.exports = function (moduleId, options) {
264272
if (noDocument) {
@@ -269,6 +277,9 @@ module.exports = function (moduleId, options) {
269277

270278
const getScriptSrc = getCurrentScriptUrl(moduleId);
271279

280+
/**
281+
* @returns {void}
282+
*/
272283
function update() {
273284
const src = getScriptSrc(options.filename);
274285
const reloaded = reloadStyle(src);
@@ -302,15 +313,13 @@ module.exports = function (moduleId, options) {
302313
\*****************************************/
303314
/***/ ((module) => {
304315

305-
/* eslint-disable */
306-
307316
/**
308-
* @param {string[]} pathComponents
309-
* @returns {string}
317+
* @param {string[]} pathComponents path components
318+
* @returns {string} normalized url
310319
*/
311-
function normalizeUrl(pathComponents) {
320+
function normalizeUrlInner(pathComponents) {
312321
return pathComponents
313-
.reduce(function (accumulator, item) {
322+
.reduce((accumulator, item) => {
314323
switch (item) {
315324
case "..":
316325
accumulator.pop();
@@ -327,24 +336,27 @@ function normalizeUrl(pathComponents) {
327336
}
328337

329338
/**
330-
* @param {string} urlString
331-
* @returns {string}
339+
* @param {string} urlString url string
340+
* @returns {string} normalized url string
332341
*/
333-
module.exports = function (urlString) {
342+
module.exports = function normalizeUrl(urlString) {
334343
urlString = urlString.trim();
335344

336345
if (/^data:/i.test(urlString)) {
337346
return urlString;
338347
}
339348

340-
var protocol =
341-
urlString.indexOf("//") !== -1 ? urlString.split("//")[0] + "//" : "";
342-
var components = urlString.replace(new RegExp(protocol, "i"), "").split("/");
343-
var host = components[0].toLowerCase().replace(/\.$/, "");
349+
const protocol =
350+
// eslint-disable-next-line unicorn/prefer-includes
351+
urlString.indexOf("//") !== -1 ? `${urlString.split("//")[0]}//` : "";
352+
const components = urlString
353+
.replace(new RegExp(protocol, "i"), "")
354+
.split("/");
355+
const host = components[0].toLowerCase().replace(/\.$/, "");
344356

345357
components[0] = "";
346358

347-
var path = normalizeUrl(components);
359+
const path = normalizeUrlInner(components);
348360

349361
return protocol + host + path;
350362
};

0 commit comments

Comments
 (0)