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

"Elements" Web Components Support For ESM #2084

Merged
merged 5 commits into from May 8, 2022
Merged

"Elements" Web Components Support For ESM #2084

merged 5 commits into from May 8, 2022

Conversation

aerialist7
Copy link
Contributor

@aerialist7 aerialist7 commented Mar 31, 2022

Hi Team,

we're using @stoplight/elements web components in Vite/React project.
But there is unpleasant error while run build or dev commands:

[vite:resolve] Missing "./web-components.min.js" export in "@stoplight/elements" package
error during build:
Error: Missing "./web-components.min.js" export in "@stoplight/elements" package

That's caused by Vite's strict adherence to the exports field in the package.json.

So here is a fix for this problem.

@netlify
Copy link

netlify bot commented Mar 31, 2022

Deploy Preview for stoplight-elements ready!

Name Link
🔨 Latest commit 351474e
🔍 Latest deploy log https://app.netlify.com/sites/stoplight-elements/deploys/62773b0f1c7a4200096baecd
😎 Deploy Preview https://deploy-preview-2084--stoplight-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Mar 31, 2022

Deploy Preview for stoplight-elements-dev-portal-storybook ready!

Name Link
🔨 Latest commit 351474e
🔍 Latest deploy log https://app.netlify.com/sites/stoplight-elements-dev-portal-storybook/deploys/62773b0fca87f20008754041
😎 Deploy Preview https://deploy-preview-2084--stoplight-elements-dev-portal-storybook.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Mar 31, 2022

Deploy Preview for stoplight-elements-demo ready!

Name Link
🔨 Latest commit 351474e
🔍 Latest deploy log https://app.netlify.com/sites/stoplight-elements-demo/deploys/62773b0f2dc8840007d850be
😎 Deploy Preview https://deploy-preview-2084--stoplight-elements-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@aerialist7 aerialist7 changed the title Elments Web Components Support For Vite/React Elements Web Components Support For Vite/React Apr 1, 2022
@aerialist7 aerialist7 changed the title Elements Web Components Support For Vite/React "Elements" Web Components Support For Vite/React Apr 1, 2022
@aerialist7
Copy link
Contributor Author

aerialist7 commented Apr 2, 2022

@aerialist7
Copy link
Contributor Author

Hi @pamgoodrich @lottamus @Nezteb @marbemac!
Can anyone please review my PR?

Copy link
Contributor

@lottamus lottamus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aerialist7 thanks for the PR. Why did you decide to use the web-components build in your React app versus the React components? The web-components are primarily meant for non-React apps and are less flexible than the React components.

For https://github.com/aerialist7/stoplight-elements-sample/blob/vite-react-web-component/src/App.tsx, instead you could do the following:

import { API } from '@stoplight/elements'
import '@stoplight/elements/styles.min.css'
import './App.css'

function App() {
  return (
    <div className="App">
      <API
        apiDescriptionUrl="https://raw.githubusercontent.com/stoplightio/Public-APIs/master/reference/zoom/openapi.yaml"
      />
    </div>
  )
}

export default App

@aerialist7
Copy link
Contributor Author

aerialist7 commented Apr 8, 2022

Hi @lottamus, thank you for respond!

Sure, I've tried to use React component at the beginning. But the app doesn't work.
Here you can see my sample - https://github.com/aerialist7/stoplight-elements-sample/tree/vite-react-component

Run dev or preview commands and open browser console - Uncaught ReferenceError: require is not defined
Then I've tried to use it as Web Component, but Vite requires strict adherence to the exports field in the package.json.

P.S. Of cause using React component is way better for me and I will be very grateful if you can help

@aerialist7
Copy link
Contributor Author

Hi @lottamus, can you please answer?

@lottamus
Copy link
Contributor

@aerialist7 I've tried your Vitejs example and see it's not working out of the box. I'm not as familiar with Vite and esbuild, so I'll need to investigate to see what config options are needed to support Elements. Ideally it's just supported out of the box 😞

Initial findings:

@aerialist7
Copy link
Contributor Author

aerialist7 commented Apr 16, 2022

@lottamus, thank you!

It would be great and we hope that it will be supported out of the box!
But now we can't even use the Web Component variant without hacks.
So could you please merge my PR and release the new version of Stoplight "Elements"?

@aerialist7
Copy link
Contributor Author

Hi @lottamus, can you please answer?

@lottamus
Copy link
Contributor

@aerialist7 I've been able to successfully run npm run dev on your branch using the following vitejs config:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
import NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],

  optimizeDeps: {
    esbuildOptions: {
      plugins: [
        NodeGlobalsPolyfillPlugin({
          buffer: true,
          process: true,
        }),
        NodeModulesPolyfills(),
      ],
    },
  },
});

Make sure to install the two additional dev deps @esbuild-plugins/node-globals-polyfill and @esbuild-plugins/node-modules-polyfill.

Now that dev is working, I have a better hunch on why the prod build npm run build && serve -s dist is not working, but I've not yet come to the right config. I'm still hitting this issue: vitejs/vite#2672

@aerialist7
Copy link
Contributor Author

aerialist7 commented Apr 23, 2022

@lottamus I applied your configuration here

Thank you, dev works, but with problems:

  1. unable to select anything in the sidebar
  2. warnings in the browser console (mb related to first problem)

Yep, It doesn't work for me in prod either.
Crashes in runtime with the following error:

Uncaught ReferenceError: require is not defined
    at index.4675c23e.js:768:3139
    at index.4675c23e.js:1:644
    at index.4675c23e.js:1044:71129

Copy link
Contributor

@marcelltoth marcelltoth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have write access here anymore but I believe that this change is fundamentally correct, and the fact that the React distribution does or does not work with Vite should be a separate topic. (Tracked in an issue,)

@lottamus We explicitly support using the web components distribution, then why are you so hesitant to approve a PR that makes the statement import '@stoplight/elements/web-components.min.js' legal under the Node ESM spec? Would you like to restrict web components usage to direct script tag usage for some reason?

Now for the bug itself, I think that should be handled separately, but in the meantime I think I have a workaround for @aerialist7: try adding it the old-school way, using a script tag in the <head>. That does work I believe, even when using React, why wouldn't it.

@aerialist7
Copy link
Contributor Author

aerialist7 commented Apr 28, 2022

@lottamus, any news?
May be we really can merge this PR and continue our discussion within separate issue? (e.g. #2076)

@aerialist7 aerialist7 changed the title "Elements" Web Components Support For Vite/React "Elements" Web Components Support For ESM Apr 28, 2022
@aerialist7
Copy link
Contributor Author

@lottamus

@lottamus lottamus enabled auto-merge (squash) May 8, 2022 03:16
@lottamus lottamus merged commit 3bc71b3 into stoplightio:main May 8, 2022
@aerialist7
Copy link
Contributor Author

@lottamus, thank you!
When we can expect a new release?

@aerialist7
Copy link
Contributor Author

@lottamus

@aerialist7 aerialist7 deleted the vite-react-web-component-support branch June 7, 2022 08:19
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

Successfully merging this pull request may close these issues.

None yet

3 participants