ricokahler / sanity-codegen

Generate TypeScript types from your Sanity.io schemas
sanity-codegen-dev.vercel.app
MIT License
270 stars 19 forks source link

Codegen fails when ES6 import found in schema #233

Closed struct78 closed 2 years ago

struct78 commented 2 years ago

Hi there,

Love this plugin, thank you very much for making it!

We've recently installed the Sanity Notes plugin to allow us to add rich messaging/alerts/etc to our schemas, unfortunately this is breaking codegen. To get the notes plugin to work properly we need to rename files to JSX and import React/icons, e.g.

import { AiOutlineWarning } from "react-icons/ai"

export default {
  title: "Click & Collect",
  name: "settingClickAndCollect",
  type: "document",
  fields: [
    {
      name: "note",
      type: "note",
      options: {
        icon: AiOutlineWarning,
        headline: "Hold up!",
        message: (
          <>
            This is some text <a href="https://somedomain.com" target="_blank">with a link</a>
          </>
        ),
        tone: "caution",
      },
    },
    {
      title: "Enable Click & Collect",
      name: "enabled",
      type: "boolean",
    },
  ],
  preview: {
    prepare() {
      return {
        title: "Click & Collect",
      }
    },
  },
}

Now when I run sanity-codegen, I get this error:

import React from "react"
       ^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/dave/Github/bared-footwear-website/admin/src/parts/schema.js:148:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Module._compile (/Users/dave/Github/bared-footwear-website/admin/node_modules/pirates/lib/index.js:99:24)

I tried following the babel.config setup as outlined in the docs to use @babel/preset-env and @babel/preset-react, but that did not solve the issue as I noticed it's already in the default babel config file included with the package.

Using: sanity v2.23.0 node v14.16.0 sanity-codegen v0.9.5

saiichihashimoto commented 2 years ago

@struct78 did you ever find a solution?