From c7068b6a73ae7cf8ed48e6003f9825855f259afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Somhairle=20MacLe=C3=B2id?= Date: Thu, 2 May 2024 18:55:39 +0100 Subject: [PATCH] Improve sourcemap loading (#5744) --- .../wrangler/src/__tests__/deploy.test.ts | 33 ++++++++++++++++--- .../__tests__/helpers/write-wrangler-toml.ts | 7 ++-- .../src/deployment-bundle/source-maps.ts | 16 ++------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/packages/wrangler/src/__tests__/deploy.test.ts b/packages/wrangler/src/__tests__/deploy.test.ts index f16304bb6a25..15fcbe63d52f 100644 --- a/packages/wrangler/src/__tests__/deploy.test.ts +++ b/packages/wrangler/src/__tests__/deploy.test.ts @@ -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"/ ), }, }); @@ -9663,9 +9663,9 @@ export default{ "index.js.map", JSON.stringify({ version: 3, - file: "index.js", sources: ["index.ts"], sourceRoot: "", + file: "index.js", }) ); @@ -9673,8 +9673,8 @@ export default{ 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"/ ), }, }); @@ -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", () => { diff --git a/packages/wrangler/src/__tests__/helpers/write-wrangler-toml.ts b/packages/wrangler/src/__tests__/helpers/write-wrangler-toml.ts index 54503b7e1e8c..d70adb3a02b6 100644 --- a/packages/wrangler/src/__tests__/helpers/write-wrangler-toml.ts +++ b/packages/wrangler/src/__tests__/helpers/write-wrangler-toml.ts @@ -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", diff --git a/packages/wrangler/src/deployment-bundle/source-maps.ts b/packages/wrangler/src/deployment-bundle/source-maps.ts index f4d7aba50f9d..f9771b176ea6 100644 --- a/packages/wrangler/src/deployment-bundle/source-maps.ts +++ b/packages/wrangler/src/deployment-bundle/source-maps.ts @@ -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. @@ -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 [ { @@ -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)) {