Skip to content

Commit acd5100

Browse files
fix: speed up initial client bundling
1 parent 1694b52 commit acd5100

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

client-src/utils/getCurrentScriptSource.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

client-src/utils/parseURL.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
1-
import getCurrentScriptSource from "./getCurrentScriptSource.js";
1+
/**
2+
* @returns {string}
3+
*/
4+
function getCurrentScriptSource() {
5+
// `document.currentScript` is the most accurate way to find the current script,
6+
// but is not supported in all browsers.
7+
if (document.currentScript) {
8+
return document.currentScript.getAttribute("src");
9+
}
10+
11+
// Fallback to getting all scripts running in the document.
12+
const scriptElements = document.scripts || [];
13+
const scriptElementsWithSrc = Array.prototype.filter.call(
14+
scriptElements,
15+
(element) => element.getAttribute("src"),
16+
);
17+
18+
if (scriptElementsWithSrc.length > 0) {
19+
const currentScript =
20+
scriptElementsWithSrc[scriptElementsWithSrc.length - 1];
21+
22+
return currentScript.getAttribute("src");
23+
}
24+
25+
// Fail as there was no script to use.
26+
throw new Error("[webpack-dev-server] Failed to get current script source.");
27+
}
228

329
/**
430
* @param {string} resourceQuery

0 commit comments

Comments
 (0)