stoplightio / elements

Build beautiful, interactive API Docs with embeddable React or Web Components, powered by OpenAPI and Markdown.
https://stoplight.io/open-source/elements/
Apache License 2.0
1.62k stars 190 forks source link

Create new non-CRA elements example templates #2076

Open Nezteb opened 2 years ago

Nezteb commented 2 years ago

Summary

In https://github.com/stoplightio/elements/issues/2058 we discovered that CRA 5+ does not (and will not) work with the existing elements template repos because CRA templates cannot eject and modify their webpack configs to enable Node.js polyfills.

So we had to update our documentation to pin the npx create-react-app command to 4.0.3, which isn't ideal in the long run.

We're still looking into alternatives to CRA such as:

There is also https://github.com/stoplightio/dev-portal/tree/plugin but DnC didn't create it and it would need more documentation in place before suggesting it to new users.

I have a PoC to test some of these other options but it's broken and needs more work.

Tasks

aerialist7 commented 2 years ago

Hi @Nezteb, I'm looking for using @stoplight/elements as React component in my React+Vite project. But I'm facing different errors while running dev server and production build:

In my opinion, this is due to the lack of Node.js polyfills. I tried to add them according to this article, but still doesn't work.

Do you have any example using @stoplight/elements with Vite.js? Could you please help me?

P.S. I can share my project if that helps.

Yahkob commented 1 year ago

just FYI the directions to use npx create-react-app@4.0.3 my-dir --template @stoplight/elements no longer work. CRA is a headache these days and the react team is not really supporting it (i.e. look at how many issues they have with no answers 😞) I recently tried to use and something as fundamental as using a webpack proxy no longer works

leoperria commented 4 months ago

Hi @aerialist7, very old thread but I had a very similar situation as you and apparently I fixed adding https://www.npmjs.com/package/vite-plugin-node-polyfills plugin to vite config. Hope this helps.

If you use Typescript you also need to silence the error in import statement. I know, very ugly solution, but I was in rush and apparently there is mismatch between types and exports in package.json:

// @ts-ignore
import { API } from "@stoplight/elements";

EDIT: I actually made it work only in dev mode. The bundle still gives me:

Uncaught ReferenceError: Prism is not defined
patrickswijgman commented 4 months ago

@leoperria I'm running in exactly the same issue :( I got one step closed by adding

import 'prismjs'

in main.tsx but then I got require is undefined. So I added this to my vite.config.ts:

  build: {
    commonjsOptions: { transformMixedEsModules: true },
  },

Afterwards I got a weird error that said: can't convert undefined to object

Unfortunately I had to throw in the towel. At the moment we're using swagger-ui-react which does the job for now

AlfonsoCebollero commented 2 months ago

@leoperria I'm running in exactly the same issue :( I got one step closed by adding

import 'prismjs'

in main.tsx but then I got require is undefined. So I added this to my vite.config.ts:

  build: {
    commonjsOptions: { transformMixedEsModules: true },
  },

Afterwards I got a weird error that said: can't convert undefined to object

Unfortunately I had to throw in the towel. At the moment we're using swagger-ui-react which does the job for now

Hello!

I got to the point of giving in the react library too. The massive workaround I took was to use the HTML way of doing it described in stoplight documentation:

https://docs.stoplight.io/docs/elements/a71d7fcfefcd6-elements-in-html

I added these two lines into my index.html pre-bundle file:

<script src="https://unpkg.com/@stoplight/elements/web-components.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements/styles.min.css">

And then I just used the HTML tag for the elements element:

export default function ElementsHtml({ code }) {
  return (
    <div className='w-screen p-1'>
      <elements-api
        apiDescriptionDocument={code}
        router="memory"
        layout="responsive"
      />
    </div>
  )
}

The configuration options are the same as in the react component and it is behaving quite well in my application. Hope this helps although it is not the big thing.

auvansang commented 2 months ago

I can make it works with vite and https://www.npmjs.com/package/vite-plugin-node-polyfills

vite.config.ts

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { nodePolyfills } from 'vite-plugin-node-polyfills'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), nodePolyfills(),],
})
lansfojo commented 5 days ago

Did anyone ever get a production build working to use Elements with Vite? @leoperria's solution also worked for me in dev, but not with a production build, throwing require is undefined and prism import errors.

It seems silly that this library cannot work with a standard React configuration. Am I missing something obvious, or am I stuck with using a raw HTML include?