Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
SoonIter committed Jul 24, 2024
1 parent d2cb092 commit 10e2a96
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 1,4 @@
/* eslint-env browser */
/*
eslint-disable
no-console,
func-names
*/

function normalizeUrl(urlString: string): string {
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: string[], item) {
switch (item) {
case "..":
accumulator.pop();
break;
case ".":
break;
default:
accumulator.push(item);
}

return accumulator;
}, [])
.join("/");

return protocol host path;
}
import { normalizeUrl } from "./normalizeUrl";

type Option<T> = T | null | undefined;
type DebouncedFunction<T extends (...args: any[]) => any> = (
Expand Down Expand Up @@ -242,7 204,7 @@ function isUrlRequest(url: string): boolean {
return true;
}

module.exports = function (moduleId: string, options: Record<string, any>) {
export default function (moduleId: string, options: Record<string, any>) {
if (noDocument) {
console.log("no window.document found, will not HMR CSS");

Expand Down Expand Up @@ -273,4 235,4 @@ module.exports = function (moduleId: string, options: Record<string, any>) {
}

return debounce(update, 50);
};
}
34 changes: 34 additions & 0 deletions packages/rspack/src/builtin-plugin/css-extract/hmr/normalizeUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 1,34 @@
function normalizeUrl(urlString: string): string {
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: string[], item) {
switch (item) {
case "..":
accumulator.pop();
break;
case ".":
break;
default:
accumulator.push(item);
}

return accumulator;
}, [])
.join("/");

return protocol host path;
}

export { normalizeUrl };
2 changes: 1 addition & 1 deletion tests/plugin-test/css-extract/HMR.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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").default;

function getLoadEvent() {
const event = document.createEvent("Event");
Expand Down
79 changes: 42 additions & 37 deletions tests/plugin-test/css-extract/cases/hmr/expected/main.js
Original file line number Diff line number Diff line change
@@ -1,39 1,10 @@
(() => { // webpackBootstrap
"use strict";
var __webpack_modules__ = ({
"../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js": (function (module) {

/* eslint-env browser */
/*
eslint-disable
no-console,
func-names
*/
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 "..":
accumulator.pop();
break;
case ".":
break;
default:
accumulator.push(item);
}
return accumulator;
}, [])
.join("/");
return protocol host path;
}
"../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js": (function (__unused_webpack_module, exports, __webpack_require__) {

Object.defineProperty(exports, "__esModule", ({ value: true }));
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;
Expand Down Expand Up @@ -80,7 51,7 @@ 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`));
});
};
}
Expand Down Expand Up @@ -129,7 100,7 @@ function updateCss(el, url) {
}
function getReloadUrl(href, src) {
let ret = "";
href = normalizeUrl(href);
href = (0, normalizeUrl_1.normalizeUrl)(href);
src.some(url => {
if (href.indexOf(src) > -1) {
ret = url;
Expand Down Expand Up @@ -178,7 149,7 @@ function isUrlRequest(url) {
}
return true;
}
module.exports = function (moduleId, options) {
function default_1(moduleId, options) {
if (noDocument) {
console.log("no window.document found, will not HMR CSS");
return noop;
Expand All @@ -201,7 172,41 @@ module.exports = function (moduleId, options) {
}
}
return debounce(update, 50);
};
}
exports["default"] = default_1;


}),
"../../../../../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 "..":
accumulator.pop();
break;
case ".":
break;
default:
accumulator.push(item);
}
return accumulator;
}, [])
.join("/");
return protocol host path;
}
exports.normalizeUrl = normalizeUrl;


}),
Expand Down
2 changes: 1 addition & 1 deletion tests/plugin-test/css-extract/normalizeUrl.test.js
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down

0 comments on commit 10e2a96

Please sign in to comment.