Skip to content

Commit

Permalink
refactor: move normalize-url fn
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoyun committed Jul 22, 2024
1 parent 39fea2e commit fab4da0
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: 6774
- Update: main.LAST_HASH.hot-update.js, size: 6383

## Manifest

Expand All @@ -24,7 24,6 @@

#### Changed Modules
- ../../../../../rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js
- ../../../../../rspack/dist/builtin-plugin/css-extract/hmr/normalize-url.js
- ./index.css?8047

#### Changed Runtime Modules
Expand All @@ -34,15 33,39 @@
```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 (module) {

/* eslint-env browser */
/*
eslint-disable
no-console,
func-names
*/
const normalizeUrlFn = __webpack_require__(/*! ./normalize-url */ "../../../../../rspack/dist/builtin-plugin/css-extract/hmr/normalize-url.js");
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;
}
const srcByModuleId = Object.create(null);
const noDocument = typeof document === "undefined";
const { forEach } = Array.prototype;
Expand Down Expand Up @@ -89,7 112,7 @@ function getCurrentScriptUrl(moduleId) {
}
return fileMap.split(",").map(mapRule => {
const reg = new RegExp(`${filename}/.js$`, "g");
return normalizeUrlFn(src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`));
return normalizeUrl(src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`));
});
};
}
Expand Down Expand Up @@ -138,7 161,7 @@ function updateCss(el, url) {
}
function getReloadUrl(href, src) {
let ret = "";
href = normalizeUrlFn(href);
href = normalizeUrl(href);
src.some(url => {
if (href.indexOf(src) > -1) {
ret = url;
Expand Down Expand Up @@ -213,39 236,6 @@ module.exports = function (moduleId, options) {
};


}),
"../../../../../rspack/dist/builtin-plugin/css-extract/hmr/normalize-url.js": (function (module) {

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


}),
"./index.css?8047": (function (module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 5,38 @@
func-names
*/

const normalizeUrlFn = require("./normalize-url");
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;
}

type Option<T> = T | null | undefined;
type DebouncedFunction<T extends (...args: any[]) => any> = (
Expand Down Expand Up @@ -78,7 109,7 @@ function getCurrentScriptUrl(moduleId: string) {
return fileMap.split(",").map(mapRule => {
const reg = new RegExp(`${filename}\\.js$`, "g");

return normalizeUrlFn(
return normalizeUrl(
src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`)
);
});
Expand Down Expand Up @@ -144,7 175,7 @@ function updateCss(el: HTMLLinkElement & Record<string, any>, url?: string) {
function getReloadUrl(href: string, src: Array<string>): string {
let ret = "";

href = normalizeUrlFn(href);
href = normalizeUrl(href);

src.some(url => {
if (href.indexOf(src as unknown as string) > -1) {
Expand Down
34 changes: 15 additions & 19 deletions packages/rspack/src/builtin-plugin/css-extract/hmr/normalize-url.ts
Original file line number Diff line number Diff line change
@@ -1,21 1,3 @@
function normalizeUrl(pathComponents: string[]): string {
return pathComponents
.reduce(function (accumulator: string[], item) {
switch (item) {
case "..":
accumulator.pop();
break;
case ".":
break;
default:
accumulator.push(item);
}

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

module.exports = function (urlString: string): string {
urlString = urlString.trim();

Expand All @@ -30,7 12,21 @@ module.exports = function (urlString: string): string {

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;
};
65 changes: 28 additions & 37 deletions tests/plugin-test/css-extract/cases/hmr/expected/main.js
Original file line number Diff line number Diff line change
@@ -1,15 1,39 @@
(() => { // webpackBootstrap
"use strict";
var __webpack_modules__ = ({
"../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js": (function (module, __unused_webpack_exports, __webpack_require__) {
"../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/hotModuleReplacement.js": (function (module) {

/* eslint-env browser */
/*
eslint-disable
no-console,
func-names
*/
const normalizeUrlFn = __webpack_require__(/*! ./normalize-url */ "../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/normalize-url.js");
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;
}
const srcByModuleId = Object.create(null);
const noDocument = typeof document === "undefined";
const { forEach } = Array.prototype;
Expand Down Expand Up @@ -56,7 80,7 @@ function getCurrentScriptUrl(moduleId) {
}
return fileMap.split(",").map(mapRule => {
const reg = new RegExp(`${filename}\\.js$`, "g");
return normalizeUrlFn(src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`));
return normalizeUrl(src.replace(reg, `${mapRule.replace(/{fileName}/g, filename)}.css`));
});
};
}
Expand Down Expand Up @@ -105,7 129,7 @@ function updateCss(el, url) {
}
function getReloadUrl(href, src) {
let ret = "";
href = normalizeUrlFn(href);
href = normalizeUrl(href);
src.some(url => {
if (href.indexOf(src) > -1) {
ret = url;
Expand Down Expand Up @@ -180,39 204,6 @@ module.exports = function (moduleId, options) {
};


}),
"../../../../../packages/rspack/dist/builtin-plugin/css-extract/hmr/normalize-url.js": (function (module) {

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


}),
"./index.css?f410": (function (module, __webpack_exports__, __webpack_require__) {
__webpack_require__.r(__webpack_exports__);
Expand Down

0 comments on commit fab4da0

Please sign in to comment.