Skip to content

Instantly share code, notes, and snippets.

@kiliman
Last active March 3, 2022 22:18
Show Gist options
  • Save kiliman/36ee3b503d66272b313a687371e7044c to your computer and use it in GitHub Desktop.
Save kiliman/36ee3b503d66272b313a687371e7044c to your computer and use it in GitHub Desktop.
Patch to enable breakpoint debugging in Remix route modules
diff --git a/node_modules/@remix-run/dev/compiler.js b/node_modules/@remix-run/dev/compiler.js
index 9bbf9b1..783ccad 100644
--- a/node_modules/@remix-run/dev/compiler.js
+++ b/node_modules/@remix-run/dev/compiler.js
@@ -371,7 +371,7 @@ async function createServerBuild(config, options, assetsManifestPromiseRef) {
bundle: true,
logLevel: "silent",
incremental: options.incremental,
- sourcemap: options.sourcemap ? "inline" : false,
+ sourcemap: options.sourcemap,
// The server build needs to know how to generate asset URLs for imports
// of CSS and other files.
assetNames: "_assets/[name]-[hash]",
@@ -399,9 +399,19 @@ async function writeServerBuildResult(config, outputFiles) {
await fse__namespace.ensureDir(path__namespace.dirname(config.serverBuildPath));
for (let file of outputFiles) {
- if (file.path === config.serverBuildPath) {
- await fse__namespace.writeFile(file.path, file.contents);
- break;
+ if (file.path.endsWith(".js")) {
+ // fix sourceMappingURL to be relative to current path instead of /build
+ const filename = file.path.substring(file.path.lastIndexOf("/") + 1);
+ const escapedFilename = filename.replace(/\./g, "\\.");
+ const pattern = `(//# sourceMappingURL=)(.*)${escapedFilename}`;
+ let contents = Buffer.from(file.contents).toString("utf-8");
+ contents = contents.replace(new RegExp(pattern), `$1${filename}`);
+ await fse__namespace.writeFile(file.path, contents);
+ } else if (file.path.endsWith(".map")) {
+ // remove route: prefix from source filenames so breakpoints work
+ let contents = Buffer.from(file.contents).toString("utf-8");
+ contents = contents.replace(/"route:/gm, '"');
+ await fse__namespace.writeFile(file.path, contents);
}
}
}

This patch fixes an issue with Remix sourcemaps that prevent setting breakpoints in route modules.

Add the patch-package package to your project.

Copy this @remix-run+dev+1.2.3.patch file (filename is important) to /patches folder.

Run npx patch-package to apply the patch to your project.

Next time you debug, you should be able to set a breakpoint directly in your loader.

There is a PR for this patch remix-run/remix#2065 (waiting for merge)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment