next.js: Next + SWC appear to generate incorrect sourcemaps

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 16.14.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 12.2.6-canary.2
      eslint-config-next: N/A
      react: 0.0.0-experimental-e7d0053e6-20220325
      react-dom: 0.0.0-experimental-e7d0053e6-20220325

What browser are you using? (if relevant)

N/A

How are you deploying your application? (if relevant)

Vercel, but N/A

Describe the Bug

Next generates different sourcemaps when we switch between using Babel to transpile our code, and SWC. The SWC sourcemaps are incorrect - the mapped output differs from the original source code by a few lines, causing incorrect debugging behavior.

We build https://replay.io , a time-travel debugger for JS. The Replay client uses sourcemaps on both the backend and frontend to parse and display info about the recorded application. In turn, we frequently record replays of our own client app to debug how it’s working.

I’ve made two replays of our codebase built with Next 12.2.6-canary.2 , switching only between using Babel and SWC to transpile:

If you open up debugger/src/actions/event-listeners.ts in both files, you can see that the Babel file correctly maps all of the lines that were hit:

image

In the SWC-built version, the line hit info is off by 3-4 lines.

image

If you open up the sourcemap visualizer tool for both versions, the Babel version is correct:

image

and the SWC version is wrong:

image

Expected Behavior

Using SWC to compile our application would generate sourcemaps that accurately map the original source.

Link to reproduction

https://github.com/replayio/devtools/tree/bugfix/FE-434-next-sourcemaps

To Reproduce

  1. Clone https://github.com/replayio/devtools
  2. Check out commit https://github.com/replayio/devtools/commit/61605c25401897a3486545111e5f9157ef5e3886 , part of the bugfix/FE-434-next-sourcemaps branch.
  3. Run yarn to install
  4. Use Babel to build the app: yarn build, and check the build output + sourcemaps
  5. Check out the tip of the bugfix/FE-434-next-sourcemaps branch, which just disables .babelrc to use SWC for building
  6. Use SWC to build the app: yarnbuild, and check the build output + sourcemaps

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 18 (10 by maintainers)

Commits related to this issue

Most upvoted comments

This should be resolved with https://github.com/swc-project/swc/pull/6561, though that’ll need to be rolled into the next release.