Skip to content

Commit ac1fa6a

Browse files
committed
fix(runtime): createRequire() ignores a URL object's fragment
A URL object's href is serialized, so a '#' in it always starts the fragment. Cut there before reading the path; a URL string still keeps a '#' as part of the path.
1 parent 49456d7 commit ac1fa6a

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎test-app/app/src/main/assets/app/tests/testCreateRequire.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ describe("createRequire", function () {
8989
expect(req("./target.js").tag).toBe("createrequire-target");
9090
});
9191

92+
it("ignores a URL object's fragment", function () {
93+
var req = nsModule.createRequire(
94+
new URL("file://" + fixtureDir + "/anything.js#fragment/child"));
95+
expect(req("./target.js").tag).toBe("createrequire-target");
96+
});
97+
9298
it("still resolves ~ specifiers against the app root", function () {
9399
var req = nsModule.createRequire(fixtureDir + "/anything.js");
94100
expect(req("~/esm/createrequire/target.js").tag).toBe("createrequire-target");

‎test-app/runtime/src/main/cpp/js/ns-module.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ function requireBaseDir(filenameOrURL) {
7575
if (typeof href !== "string") {
7676
throw new TypeError(CREATE_REQUIRE_ERROR);
7777
}
78-
filepath = urlStringToPath(href);
78+
// A URL object's href is serialized, so its first '#' starts the
79+
// fragment; a '#' in the path itself is spelled %23.
80+
const hashAt = StringPrototypeIndexOf(href, "#");
81+
filepath = urlStringToPath(hashAt >= 0 ? StringPrototypeSlice(href, 0, hashAt) : href);
7982
} else if (typeof filenameOrURL !== "string") {
8083
throw new TypeError(CREATE_REQUIRE_ERROR);
8184
} else if (StringPrototypeStartsWith(filenameOrURL, "/")) {

0 commit comments

Comments
 (0)