react-dev-inspector icon indicating copy to clipboard operation
react-dev-inspector copied to clipboard

Do you support Nextjs and SWC?

Open givo opened this issue 1 year ago • 6 comments

Hey zthxxx!

First, thank you for the fantastic tool! It has been incredibly useful in my projects.

I'm currently working on a Next.js project that uses SWC for compiling - https://github.com/vercel/next-learn/tree/main/dashboard/final-example, and I've been trying to get react-dev-inspector to work with it. The extension works well with most frameworks, but I'm facing issues when trying to use it with Next.js, especially with server-side components.

I don't want to use babel like in your example in https://github.com/zthxxx/react-dev-inspector/tree/dev/examples/nextjs-custom-server because SWC is much faster but I also afraid that nextjs won't support babel for much longer.

I tried many things to make it work, like configuring next.config.js and .swcrc but nothing worked. Nothing seems to make nextjs + SWC to expose _debugInfo that your extension needs.

.swcrc:

{
  "jsc": {
    "target": "es2021",
    "parser": {
      "syntax": "typescript",
      "tsx": true
    },
    "transform": {
      "react": {
        "runtime": "automatic",
        "development": true,
        "refresh": true
      }
    }
  },
  "sourceMaps": true,
  "minify": false
}

next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack(config, { dev }) {
    config.mode = 'development';
    config.devtool = 'source-map';        // nextjs automatically converts this to eval-source-map
    return config;
  },
  reactStrictMode: true,
  swcMinify: false,
  productionBrowserSourceMaps: true,
};
module.exports = nextConfig;
  • Do you support nextjs with SWC?
  • Is it even possible to get source code of SSR components?

tnx

givo avatar Jun 16 '24 06:06 givo

It supports Next.js with SWC. Configs like your .swcrc are fine. (example in Next.js demo has enabled forceSwcTransforms)

But I haven’t tested server-side components yet. Currently, I think it wrong with supporting server-side components due to Next.js don't eject jsx-source for server-side components for hydrate.

zthxxx avatar Jun 19 '24 11:06 zthxxx

Ok, so I tried adding forceSwcTransforms like this:

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack(config, { dev }) {
    config.mode = 'development';
    config.devtool = 'source-map'; // nextjs automatically converts this to eval-source-map
    return config;
  },
  reactStrictMode: true,
  swcMinify: false,
  productionBrowserSourceMaps: true,
  experimental: {
    forceSwcTransforms: true,
  },
};

module.exports = nextConfig;

It didn't work. I also don't see _debugInfo in client side components

givo avatar Jun 19 '24 14:06 givo

https://stackblitz.com/edit/stackblitz-starters-fgrlqx?file=next.config.js

image image image

zthxxx avatar Jun 19 '24 14:06 zthxxx

So I've set forceSwcTransforms: true in the nextjs dashboard project and this is what I get on a client side component:

image

There are very few client side component in this project, and in addition, when I inspect a client side component I only get the line of the component function like the following:

24 export default function NavLinks() {
25   const pathname = usePathname();
26
27   return (
28     <>
29       {links.map((link) => {
30         const LinkIcon = link.icon;
31          return (
  • I don't get the line number of the inspected HTML element like in your example.
  • By the way, I still don't see _debugInfo or _debugSource in fiber nodes

givo avatar Jun 20 '24 06:06 givo

I think here is the source code position of your react tsx file. (but I don't know what the ?55e5 is)

image

zthxxx avatar Jul 06 '24 12:07 zthxxx

it this support app-router for next.js?

tianyingchun avatar Jul 18 '24 23:07 tianyingchun