Skip to content

Commit a831b11

Browse files
authored
test: Correct source map resolution (#500)
1 parent 8d41c49 commit a831b11

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

test-e2e/fixtures/load-preact-version.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ export function loadPreactVersion(): Plugin {
4343
const entry = pkg.exports[modName].import;
4444

4545
const code = fs.readFileSync(path.join(versionDir, entry), "utf-8");
46-
const map = fs.readFileSync(
47-
path.join(versionDir, entry + ".map"),
48-
"utf-8",
49-
);
46+
const map = findMap(path.join(versionDir, entry + ".map"));
5047

5148
return {
5249
code: code.replace(
@@ -106,10 +103,7 @@ export function loadPreactVersion(): Plugin {
106103
path.join(versionDir, importee),
107104
"utf-8",
108105
);
109-
const map = fs.readFileSync(
110-
path.join(versionDir, importee + ".map"),
111-
"utf-8",
112-
);
106+
const map = findMap(path.join(versionDir, importee + ".map"));
113107
const out = {
114108
code: code.replace(
115109
/["']preact/g,
@@ -138,10 +132,7 @@ export function loadPreactVersion(): Plugin {
138132
}
139133

140134
const code = fs.readFileSync(path.join(folder, importee), "utf-8");
141-
const map = fs.readFileSync(
142-
path.join(folder, importee + ".map"),
143-
"utf-8",
144-
);
135+
const map = findMap(path.join(folder, importee + ".map"));
145136
const out = {
146137
code: code.replace(
147138
/(["'])preact/g,
@@ -162,3 +153,18 @@ export function loadPreactVersion(): Plugin {
162153
},
163154
};
164155
}
156+
157+
// Preact 10's `.mjs` files are renamed copies of the `.module.js` output.
158+
// We don't attempt to rewrite the sourcemap comment paths within, nor do we
159+
// offer `.mjs.map` files, so we need to fallback to `.module.js.map` for X.
160+
function findMap(path: string) {
161+
let map = "";
162+
try {
163+
map = fs.readFileSync(path, "utf-8");
164+
} catch (e) {
165+
if (e.code !== "ENOENT") throw e;
166+
map = fs.readFileSync(path.replace(".mjs", ".module.js"), "utf-8");
167+
}
168+
169+
return map;
170+
}

0 commit comments

Comments
 (0)