Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using custom provider breaks sanity codegen #55

Closed
JamesGriz opened this issue Jun 29, 2023 · 2 comments
Closed

Using custom provider breaks sanity codegen #55

JamesGriz opened this issue Jun 29, 2023 · 2 comments

Comments

@JamesGriz
Copy link

I installed this plugin in my project and added some custom configuration to get my Icons to work, the field looks like so:

import { icons } from '..'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'


  defineField({
    title: 'Icon',
    name: 'icon',
    type: 'iconPicker',
    validation: (Rule) =>
      required ? Rule.required().error('This field is required') : Rule,
    options: {
      configurations: [
        {
          title: 'FA Solid Icons',
          provider: 'fa-pro',
          icons: () =>
            Object.entries(icons).map(([name, Component]) => {
              return {
                name,
                component: () => (
                  <div
                    style={{
                      display: 'flex',
                      alignItems: 'center',
                      justifyContent: 'center',
                      width: '25px',
                      height: '25px',
                    }}
                  >
                    <FontAwesomeIcon icon={Component} />
                  </div>
                ),
                tags: [name],
              }
            }),
        },
      ],
    },
  })

With the icons object being setup like so:

import {
  faSquareFacebook,
  faInstagram,
  faLinkedin,
  faTwitter,
} from '@fortawesome/free-brands-svg-icons'

export const icons = {
  squareFacebook: faSquareFacebook,
  instagram: faInstagram,
  linkedin: faLinkedin,
  twitter: faTwitter,
}

This worked fantastically well as it allowed me limit the icon options in the CMS to the same icons that my user facing ui Icon component was setup to use and I didn't have to worry about updating them in more then one place.

I also have the sanity-codegen plugin installed to generate typescript types based on my schema and the only issue I have had with this approach, is now when I try run npx sanity-codegen I get the following errors:

$ npx sanity-codegen
Error: matchMedia not present, legacy browsers require a polyfill
    at new MediaQueryDispatch (/Users/griz/Repos/ongava-website/node_modules/enquire.js/src/MediaQueryDispatch.js:15:15)
    at Object.<anonymous> (/Users/griz/Repos/ongava-website/node_modules/enquire.js/src/index.js:2:18)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._compile (/Users/griz/Repos/ongava-website/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Object.newLoader [as .js] (/Users/griz/Repos/ongava-website/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/griz/Repos/ongava-website/node_modules/react-slick/lib/slider.js:50:53)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._compile (/Users/griz/Repos/ongava-website/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Object.newLoader [as .js] (/Users/griz/Repos/ongava-website/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/griz/Repos/ongava-website/node_modules/react-slick/lib/index.js:8:38)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._compile (/Users/griz/Repos/ongava-website/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Object.newLoader [as .js] (/Users/griz/Repos/ongava-website/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/griz/Repos/ongava-website/packages/ui/components/SliderEditorial/SliderEditorial.tsx:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._compile (/Users/griz/Repos/ongava-website/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Object.newLoader [as .tsx] (/Users/griz/Repos/ongava-website/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/griz/Repos/ongava-website/packages/ui/components/SliderEditorial/index.ts:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._compile (/Users/griz/Repos/ongava-website/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Object.newLoader [as .ts] (/Users/griz/Repos/ongava-website/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/griz/Repos/ongava-website/packages/ui/index.tsx:16:1)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._compile (/Users/griz/Repos/ongava-website/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I tried installing:

matchmedia-polyfill

and importing it in the file, which did work for the codegen but then had a bunch of other issues with the project, and either way this doesnt seem like a good solution.

If anyone has any insight that would be much appreciated

@christopherafbjur
Copy link
Owner

Hi @JamesGriz will have a look at this in the upcoming weeks

@christopherafbjur
Copy link
Owner

christopherafbjur commented Aug 3, 2023

Hi @JamesGriz! It seems to me based on the npx sanity-codegen script you ran that you're using the v2 version of the sanity-codegen and that you're using a sanity v3 studio based on the usage of defineField.

I tried installing/running sanity-codegen based on the instructions here (alpha version) which supports v3 projects and it generated the types for me correctly in my v3 sanity studio project.

This was my test setup:

./package.json

{
  "name": "sanity-project",
  "private": true,
  "version": "1.0.0",
  "main": "package.json",
  "license": "UNLICENSED",
  "scripts": {
    "dev": "sanity dev",
    "start": "sanity start",
    "build": "sanity build",
    "deploy": "sanity deploy",
    "deploy-graphql": "sanity graphql deploy"
  },
  "keywords": [
    "sanity"
  ],
  "dependencies": {
    "@fortawesome/free-brands-svg-icons": "^6.4.2",
    "@fortawesome/react-fontawesome": "^0.2.0",
    "@sanity/vision": "^3.14.5",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-is": "^18.2.0",
    "sanity": "^3.14.5",
    "sanity-plugin-icon-picker": "^3.2.2",
    "styled-components": "^5.3.9"
  },
  "devDependencies": {
    "@sanity/eslint-config-studio": "^3.0.0",
    "@types/react": "^18.0.25",
    "@types/styled-components": "^5.1.26",
    "eslint": "^8.6.0",
    "sanity-codegen": "^1.0.0-alpha.45",
    "typescript": "^4.9.5"
  },
  "prettier": {
    "semi": false,
    "printWidth": 100,
    "bracketSpacing": false,
    "singleQuote": true
  }
}

./schemas/index.tsx

import React from 'react'
import {defineField} from 'sanity'
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'
import {
  faSquareFacebook,
  faInstagram,
  faLinkedin,
  faTwitter,
} from '@fortawesome/free-brands-svg-icons'

const icons = {
  squareFacebook: faSquareFacebook,
  instagram: faInstagram,
  linkedin: faLinkedin,
  twitter: faTwitter,
}

export const schemaTypes = [
  {
    title: 'Icons',
    name: 'icons',
    type: 'document',
    fields: [
      defineField({
        title: 'Icon',
        name: 'icon',
        type: 'iconPicker',
        options: {
          configurations: [
            {
              title: 'FA Solid Icons',
              provider: 'fa-pro',
              icons: () =>
                Object.entries(icons).map(([name, Component]) => {
                  return {
                    name,
                    component: () => (
                      <div
                        style={{
                          display: 'flex',
                          alignItems: 'center',
                          justifyContent: 'center',
                          width: '25px',
                          height: '25px',
                        }}
                      >
                        <FontAwesomeIcon icon={Component} />
                      </div>
                    ),
                    tags: [name],
                  }
                }),
            },
          ],
        },
      }),
    ],
  },
]

As specified I then installed sanity-codegen npm i --save-dev sanity-codegen@alpha
and generated the types using npx sanity-codegen codegen.

@christopherafbjur christopherafbjur closed this as not planned Won't fix, can't repro, duplicate, stale Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants