blitz-js / babel-plugin-superjson-next

Automatically transform your Next.js Pages to use SuperJSON
MIT License
124 stars 15 forks source link

Does this library work with deeply nested objects passed from getServerSideProps? #100

Closed SheaBelsky closed 2 years ago

SheaBelsky commented 3 years ago

I'm trying to implement this library in a project that would pass down date objects within a deeply nested object.

For example:

export const getServerSideProps = () => {
  return {
    props: {
      currentUser: {
        createdAt: new Date(),
        lastUpdated: new Date(),
      }
    }
  }
}

If I have data in this shape, the plugin does not seem to catch the dates and convert them. Is this working as designed? Are there plans to address this in the future, or is this considered out of scope for this library?

SheaBelsky commented 3 years ago

Looks like this is similar to the issue described in: https://github.com/blitz-js/babel-plugin-superjson-next/issues/76#issuecomment-866179513

I tried:

My babel.config.js:

module.exports = {
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    "@babel/plugin-transform-modules-commonjs",
    "babel-plugin-superjson-next",
  ],
  "presets": [
    "next/babel"
  ]
}

My tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "baseUrl": ".",
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext",
      "ESNext.AsyncIterable"
    ],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmit": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": false,
    "target": "ES2020",
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ]
}
Skn0tt commented 3 years ago

Hi @SheaBelsky! I'd love to look into this. Can you provide a reproduction repository for this case? I suspect this is caused by specific syntax in one of your page files, that babel-plugin-superjson-next has trouble picking up at the moment.

SheaBelsky commented 3 years ago

@Skn0tt Working on a reproduction repo now, will post here when it's done!

SheaBelsky commented 3 years ago

@Skn0tt I was able to identify a way to consistently reproduce the error in a minimally viable repo:

https://github.com/SheaBelsky/nextjs-prisma-typegraphql/blob/sheabelsky_superjsonBabelError/pages/user/index.tsx#L6

There seems to be an error when using getServerSideProps in a component that uses the export {default} from syntax. Took me a while to figure out the culprit, but I can fix the error by importing the component manually and then re-exporting it. Please let me know if this is what you needed!

Skn0tt commented 3 years ago

Thanks, that's super helpful! Will take a look at it later this week.

Skn0tt commented 3 years ago

"Later this week" came earlier than expected. Wanna take a look at the PR @SheaBelsky?