diff --git a/packages/rspack-test-tools/tests/hotCases/css/recovery-cacheable/__snapshots__/web/2.snap.txt b/packages/rspack-test-tools/tests/hotCases/css/recovery-cacheable/__snapshots__/web/2.snap.txt index 613647a028d..5e7c68c3aec 100644 --- a/packages/rspack-test-tools/tests/hotCases/css/recovery-cacheable/__snapshots__/web/2.snap.txt +++ b/packages/rspack-test-tools/tests/hotCases/css/recovery-cacheable/__snapshots__/web/2.snap.txt @@ -6,7 +6,7 @@ ## Asset Files - Bundle: bundle.js - Manifest: main.LAST_HASH.hot-update.json, size: 28 -- Update: main.LAST_HASH.hot-update.js, size: 830 +- Update: main.LAST_HASH.hot-update.js, size: 858 ## Manifest @@ -37,7 +37,7 @@ __webpack_require__.r(__webpack_exports__); // extracted by css-extract-rspack-plugin if(true) { - var cssReload = __webpack_require__(/*! ../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js */ "../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js")(module.id, {"locals":false}); + var cssReload = (__webpack_require__(/*! ../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js */ "../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js")/* .cssReload */.cssReload)(module.id, {"locals":false}); module.hot.dispose(cssReload); module.hot.accept(undefined, function(__WEBPACK_OUTDATED_DEPENDENCIES__) { (cssReload)(__WEBPACK_OUTDATED_DEPENDENCIES__); }.bind(this)); diff --git a/packages/rspack-test-tools/tests/hotCases/css/recovery/__snapshots__/web/2.snap.txt b/packages/rspack-test-tools/tests/hotCases/css/recovery/__snapshots__/web/2.snap.txt index 820fcfe82e1..419a7d6280c 100644 --- a/packages/rspack-test-tools/tests/hotCases/css/recovery/__snapshots__/web/2.snap.txt +++ b/packages/rspack-test-tools/tests/hotCases/css/recovery/__snapshots__/web/2.snap.txt @@ -6,7 +6,7 @@ ## Asset Files - Bundle: bundle.js - Manifest: main.LAST_HASH.hot-update.json, size: 28 -- Update: main.LAST_HASH.hot-update.js, size: 7865 +- Update: main.LAST_HASH.hot-update.js, size: 6945 ## Manifest @@ -24,7 +24,7 @@ #### Changed Modules - ../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js -- ../../../../../rspack/dist/builtin-plugin/css-extract/hmr/normalize-url.js +- ../../../../../rspack/dist/builtin-plugin/css-extract/hmr/normalizeUrl.js - ./index.css?8047 #### Changed Runtime Modules @@ -34,49 +34,33 @@ ```js "use strict"; self["webpackHotUpdate"]('main', { -"../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js": (function (module, __unused_webpack_exports, __webpack_require__) { +"../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js": (function (__unused_webpack_module, exports, __webpack_require__) { -/* eslint-env browser */ -/* - eslint-disable - no-console, - func-names -*/ -/** @typedef {any} TODO */ -const normalizeUrl = __webpack_require__(/*! ./normalize-url */ "../../../../../rspack/dist/builtin-plugin/css-extract/hmr/normalize-url.js"); +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.cssReload = void 0; +const normalizeUrl_1 = __webpack_require__(/*! ./normalizeUrl */ "../../../../../rspack/dist/builtin-plugin/css-extract/hmr/normalizeUrl.js"); const srcByModuleId = Object.create(null); const noDocument = typeof document === "undefined"; const { forEach } = Array.prototype; -/** - * @param {function} fn - * @param {number} time - * @returns {(function(): void)|*} - */ function debounce(fn, time) { let timeout = 0; return function () { // @ts-ignore const self = this; - // eslint-disable-next-line prefer-rest-params const args = arguments; const functionCall = function functionCall() { return fn.apply(self, args); }; clearTimeout(timeout); - // @ts-ignore timeout = setTimeout(functionCall, time); }; } function noop() { } -/** - * @param {TODO} moduleId - * @returns {TODO} - */ function getCurrentScriptUrl(moduleId) { let src = srcByModuleId[moduleId]; if (!src) { if (document.currentScript) { - ({ src } = /** @type {HTMLScriptElement} */ (document.currentScript)); + ({ src } = document.currentScript); } else { const scripts = document.getElementsByTagName("script"); @@ -87,10 +71,6 @@ function getCurrentScriptUrl(moduleId) { } srcByModuleId[moduleId] = src; } - /** - * @param {string} fileMap - * @returns {null | string[]} - */ return function (fileMap) { if (!src) { return null; @@ -105,23 +85,18 @@ function getCurrentScriptUrl(moduleId) { } return fileMap.split(",").map(mapRule => { const reg = new RegExp(`${filename}/.js$`, "g"); - return normalizeUrl(src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`)); + return (0, normalizeUrl_1.normalizeUrl)(src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`)); }); }; } -/** - * @param {TODO} el - * @param {string} [url] - */ function updateCss(el, url) { if (!url) { if (!el.href) { return; } - // eslint-disable-next-line url = el.href.split("?")[0]; } - if (!isUrlRequest(/** @type {string} */ (url))) { + if (!isUrlRequest(url)) { return; } if (el.isLoaded === false) { @@ -132,7 +107,6 @@ function updateCss(el, url) { if (!url || !(url.indexOf(".css") > -1)) { return; } - // eslint-disable-next-line no-param-reassign el.visited = true; const newEl = el.cloneNode(); newEl.isLoaded = false; @@ -141,48 +115,33 @@ function updateCss(el, url) { return; } newEl.isLoaded = true; - el.parentNode.removeChild(el); + el.parentNode?.removeChild(el); }); newEl.addEventListener("error", () => { if (newEl.isLoaded) { return; } newEl.isLoaded = true; - el.parentNode.removeChild(el); + el.parentNode?.removeChild(el); }); newEl.href = `${url}?${Date.now()}`; if (el.nextSibling) { - el.parentNode.insertBefore(newEl, el.nextSibling); + el.parentNode?.insertBefore(newEl, el.nextSibling); } else { - el.parentNode.appendChild(newEl); + el.parentNode?.appendChild(newEl); } } -/** - * @param {string} href - * @param {TODO} src - * @returns {TODO} - */ function getReloadUrl(href, src) { - let ret; - // eslint-disable-next-line no-param-reassign - href = normalizeUrl(href); - src.some( - /** - * @param {string} url - */ - // eslint-disable-next-line array-callback-return - url => { + let ret = ""; + href = (0, normalizeUrl_1.normalizeUrl)(href); + src.some(url => { if (href.indexOf(src) > -1) { ret = url; } }); return ret; } -/** - * @param {string} [src] - * @returns {boolean} - */ function reloadStyle(src) { if (!src) { return false; @@ -216,10 +175,6 @@ function reloadAll() { updateCss(el); }); } -/** - * @param {string} url - * @returns {boolean} - */ function isUrlRequest(url) { // An URL is not an request if // It is not http or https @@ -228,12 +183,7 @@ function isUrlRequest(url) { } return true; } -/** - * @param {TODO} moduleId - * @param {TODO} options - * @returns {TODO} - */ -module.exports = function (moduleId, options) { +function cssReload(moduleId, options) { if (noDocument) { console.log("no window.document found, will not HMR CSS"); return noop; @@ -248,7 +198,7 @@ module.exports = function (moduleId, options) { return; } if (reloaded) { - console.log("[HMR] css reload %s", src.join(" ")); + console.log("[HMR] css reload %s", src?.join(" ")); } else { console.log("[HMR] Reload all css"); @@ -256,19 +206,25 @@ module.exports = function (moduleId, options) { } } return debounce(update, 50); -}; +} +exports.cssReload = cssReload; }), -"../../../../../rspack/dist/builtin-plugin/css-extract/hmr/normalize-url.js": (function (module) { +"../../../../../rspack/dist/builtin-plugin/css-extract/hmr/normalizeUrl.js": (function (__unused_webpack_module, exports) { -/* eslint-disable */ -/** - * @param {string[]} pathComponents - * @returns {string} - */ -function normalizeUrl(pathComponents) { - return pathComponents +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.normalizeUrl = void 0; +function normalizeUrl(urlString) { + urlString = urlString.trim(); + if (/^data:/i.test(urlString)) { + return urlString; + } + var protocol = urlString.indexOf("//") !== -1 ? urlString.split("//")[0] + "//" : ""; + var components = urlString.replace(new RegExp(protocol, "i"), "").split("/"); + var host = components[0].toLowerCase().replace(//.$/, ""); + components[0] = ""; + var path = components .reduce(function (accumulator, item) { switch (item) { case "..": @@ -280,25 +236,11 @@ function normalizeUrl(pathComponents) { accumulator.push(item); } return accumulator; - }, /** @type {string[]} */ ([])) + }, []) .join("/"); -} -/** - * @param {string} urlString - * @returns {string} - */ -module.exports = function (urlString) { - urlString = urlString.trim(); - if (/^data:/i.test(urlString)) { - return urlString; - } - var protocol = urlString.indexOf("//") !== -1 ? urlString.split("//")[0] + "//" : ""; - var components = urlString.replace(new RegExp(protocol, "i"), "").split("/"); - var host = components[0].toLowerCase().replace(//.$/, ""); - components[0] = ""; - var path = normalizeUrl(components); return protocol + host + path; -}; +} +exports.normalizeUrl = normalizeUrl; }), @@ -307,7 +249,7 @@ __webpack_require__.r(__webpack_exports__); // extracted by css-extract-rspack-plugin if(true) { - var cssReload = __webpack_require__(/*! ../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js */ "../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js")(module.id, {"locals":false}); + var cssReload = (__webpack_require__(/*! ../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js */ "../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js")/* .cssReload */.cssReload)(module.id, {"locals":false}); module.hot.dispose(cssReload); module.hot.accept(undefined, function(__WEBPACK_OUTDATED_DEPENDENCIES__) { (cssReload)(__WEBPACK_OUTDATED_DEPENDENCIES__); }.bind(this)); diff --git a/packages/rspack/src/builtin-plugin/css-extract/hmr/hotModuleReplacement.js b/packages/rspack/src/builtin-plugin/css-extract/hmr/hotModuleReplacement.ts similarity index 63% rename from packages/rspack/src/builtin-plugin/css-extract/hmr/hotModuleReplacement.js rename to packages/rspack/src/builtin-plugin/css-extract/hmr/hotModuleReplacement.ts index 6fda8200b5c..0b4a48e1c18 100644 --- a/packages/rspack/src/builtin-plugin/css-extract/hmr/hotModuleReplacement.js +++ b/packages/rspack/src/builtin-plugin/css-extract/hmr/hotModuleReplacement.ts @@ -1,57 +1,45 @@ -/* eslint-env browser */ -/* - eslint-disable - no-console, - func-names -*/ +import { normalizeUrl } from "./normalizeUrl"; -/** @typedef {any} TODO */ +type Option = T | null | undefined; +type DebouncedFunction any> = ( + ...args: Parameters +) => void; -const normalizeUrl = require("./normalize-url"); - -const srcByModuleId = Object.create(null); +const srcByModuleId: Record = Object.create(null); const noDocument = typeof document === "undefined"; const { forEach } = Array.prototype; -/** - * @param {function} fn - * @param {number} time - * @returns {(function(): void)|*} - */ -function debounce(fn, time) { - let timeout = 0; +function debounce any>( + fn: T, + time: number +): DebouncedFunction { + let timeout: NodeJS.Timeout | number = 0; return function () { // @ts-ignore const self = this; - // eslint-disable-next-line prefer-rest-params const args = arguments; const functionCall = function functionCall() { - return fn.apply(self, args); + return fn.apply(self, args as unknown as Parameters); }; clearTimeout(timeout); - // @ts-ignore timeout = setTimeout(functionCall, time); }; } function noop() {} -/** - * @param {TODO} moduleId - * @returns {TODO} - */ -function getCurrentScriptUrl(moduleId) { +function getCurrentScriptUrl(moduleId: string) { let src = srcByModuleId[moduleId]; if (!src) { if (document.currentScript) { - ({ src } = /** @type {HTMLScriptElement} */ (document.currentScript)); + ({ src } = document.currentScript as HTMLScriptElement); } else { const scripts = document.getElementsByTagName("script"); const lastScriptTag = scripts[scripts.length - 1]; @@ -64,11 +52,7 @@ function getCurrentScriptUrl(moduleId) { srcByModuleId[moduleId] = src; } - /** - * @param {string} fileMap - * @returns {null | string[]} - */ - return function (fileMap) { + return function (fileMap: string): Option> { if (!src) { return null; } @@ -94,21 +78,16 @@ function getCurrentScriptUrl(moduleId) { }; } -/** - * @param {TODO} el - * @param {string} [url] - */ -function updateCss(el, url) { +function updateCss(el: HTMLLinkElement & Record, url?: string) { if (!url) { if (!el.href) { return; } - // eslint-disable-next-line url = el.href.split("?")[0]; } - if (!isUrlRequest(/** @type {string} */ (url))) { + if (!isUrlRequest(url)) { return; } @@ -122,10 +101,9 @@ function updateCss(el, url) { return; } - // eslint-disable-next-line no-param-reassign el.visited = true; - const newEl = el.cloneNode(); + const newEl = el.cloneNode() as Node & Record; newEl.isLoaded = false; @@ -135,7 +113,7 @@ function updateCss(el, url) { } newEl.isLoaded = true; - el.parentNode.removeChild(el); + el.parentNode?.removeChild(el); }); newEl.addEventListener("error", () => { @@ -144,49 +122,33 @@ function updateCss(el, url) { } newEl.isLoaded = true; - el.parentNode.removeChild(el); + el.parentNode?.removeChild(el); }); newEl.href = `${url}?${Date.now()}`; if (el.nextSibling) { - el.parentNode.insertBefore(newEl, el.nextSibling); + el.parentNode?.insertBefore(newEl, el.nextSibling); } else { - el.parentNode.appendChild(newEl); + el.parentNode?.appendChild(newEl); } } -/** - * @param {string} href - * @param {TODO} src - * @returns {TODO} - */ -function getReloadUrl(href, src) { - let ret; +function getReloadUrl(href: string, src: Array): string { + let ret = ""; - // eslint-disable-next-line no-param-reassign href = normalizeUrl(href); - src.some( - /** - * @param {string} url - */ - // eslint-disable-next-line array-callback-return - url => { - if (href.indexOf(src) > -1) { - ret = url; - } + src.some(url => { + if (href.indexOf(src as unknown as string) > -1) { + ret = url; } - ); + }); return ret; } -/** - * @param {string} [src] - * @returns {boolean} - */ -function reloadStyle(src) { +function reloadStyle(src: Option>): boolean { if (!src) { return false; } @@ -231,11 +193,7 @@ function reloadAll() { }); } -/** - * @param {string} url - * @returns {boolean} - */ -function isUrlRequest(url) { +function isUrlRequest(url: string): boolean { // An URL is not an request if // It is not http or https @@ -246,12 +204,7 @@ function isUrlRequest(url) { return true; } -/** - * @param {TODO} moduleId - * @param {TODO} options - * @returns {TODO} - */ -module.exports = function (moduleId, options) { +function cssReload(moduleId: string, options: Record) { if (noDocument) { console.log("no window.document found, will not HMR CSS"); @@ -273,7 +226,7 @@ module.exports = function (moduleId, options) { } if (reloaded) { - console.log("[HMR] css reload %s", src.join(" ")); + console.log("[HMR] css reload %s", src?.join(" ")); } else { console.log("[HMR] Reload all css"); @@ -282,4 +235,6 @@ module.exports = function (moduleId, options) { } return debounce(update, 50); -}; +} + +export { cssReload }; diff --git a/packages/rspack/src/builtin-plugin/css-extract/hmr/normalize-url.js b/packages/rspack/src/builtin-plugin/css-extract/hmr/normalizeUrl.ts similarity index 60% rename from packages/rspack/src/builtin-plugin/css-extract/hmr/normalize-url.js rename to packages/rspack/src/builtin-plugin/css-extract/hmr/normalizeUrl.ts index 04fb0969aec..3559ad42716 100644 --- a/packages/rspack/src/builtin-plugin/css-extract/hmr/normalize-url.js +++ b/packages/rspack/src/builtin-plugin/css-extract/hmr/normalizeUrl.ts @@ -1,32 +1,4 @@ -/* eslint-disable */ - -/** - * @param {string[]} pathComponents - * @returns {string} - */ -function normalizeUrl(pathComponents) { - return pathComponents - .reduce(function (accumulator, item) { - switch (item) { - case "..": - accumulator.pop(); - break; - case ".": - break; - default: - accumulator.push(item); - } - - return accumulator; - }, /** @type {string[]} */ ([])) - .join("/"); -} - -/** - * @param {string} urlString - * @returns {string} - */ -module.exports = function (urlString) { +function normalizeUrl(urlString: string): string { urlString = urlString.trim(); if (/^data:/i.test(urlString)) { @@ -40,7 +12,23 @@ module.exports = function (urlString) { components[0] = ""; - var path = normalizeUrl(components); + var path = components + .reduce(function (accumulator: string[], item) { + switch (item) { + case "..": + accumulator.pop(); + break; + case ".": + break; + default: + accumulator.push(item); + } + + return accumulator; + }, []) + .join("/"); return protocol + host + path; -}; +} + +export { normalizeUrl }; diff --git a/packages/rspack/src/builtin-plugin/css-extract/loader.ts b/packages/rspack/src/builtin-plugin/css-extract/loader.ts index b177cc2a447..5d03a6d8317 100644 --- a/packages/rspack/src/builtin-plugin/css-extract/loader.ts +++ b/packages/rspack/src/builtin-plugin/css-extract/loader.ts @@ -53,7 +53,7 @@ function hotLoader( var cssReload = require(${stringifyRequest( context.loaderContext, path.join(__dirname, "./hmr/hotModuleReplacement.js") - )})(module.id, ${JSON.stringify({ + )}).cssReload(module.id, ${JSON.stringify({ ...context.options, locals: !!context.locals })}); diff --git a/tests/plugin-test/css-extract/HMR.test.js b/tests/plugin-test/css-extract/HMR.test.js index 5c6bc9230d4..2daa3311d7b 100644 --- a/tests/plugin-test/css-extract/HMR.test.js +++ b/tests/plugin-test/css-extract/HMR.test.js @@ -4,7 +4,7 @@ /* eslint-env browser */ /* eslint-disable no-console */ -const hotModuleReplacement = require("../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement"); +const hotModuleReplacement = require("../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement").cssReload; function getLoadEvent() { const event = document.createEvent("Event"); diff --git a/tests/plugin-test/css-extract/cases/hmr/expected/main.js b/tests/plugin-test/css-extract/cases/hmr/expected/main.js index 67f979d0896..0749b8512d4 100644 --- a/tests/plugin-test/css-extract/cases/hmr/expected/main.js +++ b/tests/plugin-test/css-extract/cases/hmr/expected/main.js @@ -1,49 +1,33 @@ (() => { // webpackBootstrap "use strict"; var __webpack_modules__ = ({ -"../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js": (function (module, __unused_webpack_exports, __webpack_require__) { - -/* eslint-env browser */ -/* - eslint-disable - no-console, - func-names -*/ -/** @typedef {any} TODO */ -const normalizeUrl = __webpack_require__(/*! ./normalize-url */ "../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/normalize-url.js"); +"../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js": (function (__unused_webpack_module, exports, __webpack_require__) { + +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.cssReload = void 0; +const normalizeUrl_1 = __webpack_require__(/*! ./normalizeUrl */ "../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/normalizeUrl.js"); const srcByModuleId = Object.create(null); const noDocument = typeof document === "undefined"; const { forEach } = Array.prototype; -/** - * @param {function} fn - * @param {number} time - * @returns {(function(): void)|*} - */ function debounce(fn, time) { let timeout = 0; return function () { // @ts-ignore const self = this; - // eslint-disable-next-line prefer-rest-params const args = arguments; const functionCall = function functionCall() { return fn.apply(self, args); }; clearTimeout(timeout); - // @ts-ignore timeout = setTimeout(functionCall, time); }; } function noop() { } -/** - * @param {TODO} moduleId - * @returns {TODO} - */ function getCurrentScriptUrl(moduleId) { let src = srcByModuleId[moduleId]; if (!src) { if (document.currentScript) { - ({ src } = /** @type {HTMLScriptElement} */ (document.currentScript)); + ({ src } = document.currentScript); } else { const scripts = document.getElementsByTagName("script"); @@ -54,10 +38,6 @@ function getCurrentScriptUrl(moduleId) { } srcByModuleId[moduleId] = src; } - /** - * @param {string} fileMap - * @returns {null | string[]} - */ return function (fileMap) { if (!src) { return null; @@ -72,23 +52,18 @@ function getCurrentScriptUrl(moduleId) { } return fileMap.split(",").map(mapRule => { const reg = new RegExp(`${filename}\\.js$`, "g"); - return normalizeUrl(src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`)); + return (0, normalizeUrl_1.normalizeUrl)(src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`)); }); }; } -/** - * @param {TODO} el - * @param {string} [url] - */ function updateCss(el, url) { if (!url) { if (!el.href) { return; } - // eslint-disable-next-line url = el.href.split("?")[0]; } - if (!isUrlRequest(/** @type {string} */ (url))) { + if (!isUrlRequest(url)) { return; } if (el.isLoaded === false) { @@ -99,7 +74,6 @@ function updateCss(el, url) { if (!url || !(url.indexOf(".css") > -1)) { return; } - // eslint-disable-next-line no-param-reassign el.visited = true; const newEl = el.cloneNode(); newEl.isLoaded = false; @@ -108,48 +82,33 @@ function updateCss(el, url) { return; } newEl.isLoaded = true; - el.parentNode.removeChild(el); + el.parentNode?.removeChild(el); }); newEl.addEventListener("error", () => { if (newEl.isLoaded) { return; } newEl.isLoaded = true; - el.parentNode.removeChild(el); + el.parentNode?.removeChild(el); }); newEl.href = `${url}?${Date.now()}`; if (el.nextSibling) { - el.parentNode.insertBefore(newEl, el.nextSibling); + el.parentNode?.insertBefore(newEl, el.nextSibling); } else { - el.parentNode.appendChild(newEl); + el.parentNode?.appendChild(newEl); } } -/** - * @param {string} href - * @param {TODO} src - * @returns {TODO} - */ function getReloadUrl(href, src) { - let ret; - // eslint-disable-next-line no-param-reassign - href = normalizeUrl(href); - src.some( - /** - * @param {string} url - */ - // eslint-disable-next-line array-callback-return - url => { + let ret = ""; + href = (0, normalizeUrl_1.normalizeUrl)(href); + src.some(url => { if (href.indexOf(src) > -1) { ret = url; } }); return ret; } -/** - * @param {string} [src] - * @returns {boolean} - */ function reloadStyle(src) { if (!src) { return false; @@ -183,10 +142,6 @@ function reloadAll() { updateCss(el); }); } -/** - * @param {string} url - * @returns {boolean} - */ function isUrlRequest(url) { // An URL is not an request if // It is not http or https @@ -195,12 +150,7 @@ function isUrlRequest(url) { } return true; } -/** - * @param {TODO} moduleId - * @param {TODO} options - * @returns {TODO} - */ -module.exports = function (moduleId, options) { +function cssReload(moduleId, options) { if (noDocument) { console.log("no window.document found, will not HMR CSS"); return noop; @@ -215,7 +165,7 @@ module.exports = function (moduleId, options) { return; } if (reloaded) { - console.log("[HMR] css reload %s", src.join(" ")); + console.log("[HMR] css reload %s", src?.join(" ")); } else { console.log("[HMR] Reload all css"); @@ -223,19 +173,25 @@ module.exports = function (moduleId, options) { } } return debounce(update, 50); -}; +} +exports.cssReload = cssReload; }), -"../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/normalize-url.js": (function (module) { - -/* eslint-disable */ -/** - * @param {string[]} pathComponents - * @returns {string} - */ -function normalizeUrl(pathComponents) { - return pathComponents +"../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/normalizeUrl.js": (function (__unused_webpack_module, exports) { + +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.normalizeUrl = void 0; +function normalizeUrl(urlString) { + urlString = urlString.trim(); + if (/^data:/i.test(urlString)) { + return urlString; + } + var protocol = urlString.indexOf("//") !== -1 ? urlString.split("//")[0] + "//" : ""; + var components = urlString.replace(new RegExp(protocol, "i"), "").split("/"); + var host = components[0].toLowerCase().replace(/\.$/, ""); + components[0] = ""; + var path = components .reduce(function (accumulator, item) { switch (item) { case "..": @@ -247,25 +203,11 @@ function normalizeUrl(pathComponents) { accumulator.push(item); } return accumulator; - }, /** @type {string[]} */ ([])) + }, []) .join("/"); -} -/** - * @param {string} urlString - * @returns {string} - */ -module.exports = function (urlString) { - urlString = urlString.trim(); - if (/^data:/i.test(urlString)) { - return urlString; - } - var protocol = urlString.indexOf("//") !== -1 ? urlString.split("//")[0] + "//" : ""; - var components = urlString.replace(new RegExp(protocol, "i"), "").split("/"); - var host = components[0].toLowerCase().replace(/\.$/, ""); - components[0] = ""; - var path = normalizeUrl(components); return protocol + host + path; -}; +} +exports.normalizeUrl = normalizeUrl; }), @@ -275,7 +217,7 @@ __webpack_require__.r(__webpack_exports__); if(true) { // - var cssReload = __webpack_require__(/*! ../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js */ "../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js")(module.id, {"locals":false}); + var cssReload = (__webpack_require__(/*! ../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js */ "../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js")/* .cssReload */.cssReload)(module.id, {"locals":false}); module.hot.dispose(cssReload); module.hot.accept(undefined, function(__WEBPACK_OUTDATED_DEPENDENCIES__) { (cssReload)(__WEBPACK_OUTDATED_DEPENDENCIES__); }.bind(this)); diff --git a/tests/plugin-test/css-extract/normalizeUrl.test.js b/tests/plugin-test/css-extract/normalizeUrl.test.js index 8e0371bbd4f..28b95878603 100644 --- a/tests/plugin-test/css-extract/normalizeUrl.test.js +++ b/tests/plugin-test/css-extract/normalizeUrl.test.js @@ -1,4 +1,4 @@ -const normalizeUrl = require("../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/normalize-url"); +const { normalizeUrl } = require("../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/normalizeUrl"); const dataUrls = require("./fixtures/json/data-urls.json"); describe("normalize-url", () => {