Skip to content

Commit

Permalink
Improve sourcemap loading (#5744)
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed May 2, 2024
1 parent 1dd9f7e commit c7068b6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
33 changes: 28 additions & 5 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9618,8 +9618,8 @@ export default{
mockUploadWorkerRequest({
expectedMainModule: "index.js",
expectedModules: {
"index.js.map": expect.stringContaining(
`"sources":["another.ts","index.ts"],"sourceRoot":""`
"index.js.map": expect.stringMatching(
/"sources":\["another.ts","index.ts"\],"sourceRoot":"".*"file":"index.js"/
),
},
});
Expand Down Expand Up @@ -9663,18 +9663,18 @@ export default{
"index.js.map",
JSON.stringify({
version: 3,
file: "index.js",
sources: ["index.ts"],
sourceRoot: "",
file: "index.js",
})
);

mockSubDomainRequest();
mockUploadWorkerRequest({
expectedMainModule: "index.js",
expectedModules: {
"index.js.map": expect.stringContaining(
`"sources":["index.ts"],"sourceRoot":""`
"index.js.map": expect.stringMatching(
/"sources":\["index.ts"\],"sourceRoot":"".*"file":"index.js"/
),
},
});
Expand Down Expand Up @@ -9716,6 +9716,29 @@ export default{

await runWrangler("deploy");
});
it("should correctly read sourcemaps with custom wrangler.toml location", async () => {
fs.mkdirSync("some/dir", { recursive: true });
writeWranglerToml(
{
main: "../../index.ts",
upload_source_maps: true,
},
"some/dir/wrangler.toml"
);
writeWorkerSource({ format: "ts" });

mockSubDomainRequest();
mockUploadWorkerRequest({
expectedMainModule: "index.js",
expectedModules: {
"index.js.map": expect.stringMatching(
/"sources":\[".*?another\.ts",".*?index\.ts"\],"sourceRoot":"".*"file":"index.js"/
),
},
});

await runWrangler("deploy -c some/dir/wrangler.toml");
});
});

describe("ai", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import TOML from "@iarna/toml";
import type { RawConfig } from "../../config";

/** Write a mock wrangler.toml file to disk. */
export default function writeWranglerToml(config: RawConfig = {}) {
export default function writeWranglerToml(
config: RawConfig = {},
path = "./wrangler.toml"
) {
fs.writeFileSync(
"./wrangler.toml",
path,
TOML.stringify({
compatibility_date: "2022-01-12",
name: "test-name",
Expand Down
16 changes: 3 additions & 13 deletions packages/wrangler/src/deployment-bundle/source-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function loadSourceMap(
return [];
}
const map = JSON.parse(
fs.readFileSync(sourceMapPath, "utf8")
fs.readFileSync(path.join(entryDirectory, sourceMapPath), "utf8")
) as RawSourceMap;
// Overwrite the file property of the source map to match the name of the
// main module in the multipart upload.
Expand All @@ -48,13 +48,11 @@ function loadSourceMap(
const sourceRootPath = path.dirname(
path.join(entryDirectory, sourceMapPath)
);
map.sourceRoot = stripPathPrefix(sourceRootPath, map.sourceRoot);
map.sourceRoot = path.relative(sourceRootPath, map.sourceRoot);
}
map.sources = map.sources.map((source) => {
const originalPath = path.join(path.dirname(filePath), source);
// Remove the temporary build directory prefix generated by Wrangler
// that appears in the source path.
return stripPathPrefix(entryDirectory, originalPath);
return path.relative(entryDirectory, originalPath);
});
return [
{
Expand Down Expand Up @@ -124,14 +122,6 @@ function cleanPathPrefix(filePath: string): string {
);
}

function stripPathPrefix(prefix: string, filePath: string): string {
// Don't assume that the path separator matches the current OS.
return stripPrefix(
prefix,
stripPrefix(prefix + "\\", stripPrefix(prefix + "/", filePath))
);
}

function stripPrefix(prefix: string, input: string): string {
let stripped = input;
while (stripped.startsWith(prefix)) {
Expand Down

0 comments on commit c7068b6

Please sign in to comment.