saurabhnemade / react-twitter-embed

Simplest way to add twitter widgets to your react project.
https://saurabhnemade.github.io/react-twitter-embed/
362 stars 65 forks source link

Dynamic require() usage in "modern" build. #128

Open erictaylor opened 1 year ago

erictaylor commented 1 year ago

The "modern" build of this library causes problems with some build systems (such as Vite) because of the usage of dynamic requires (ex: https://github.com/saurabhnemade/react-twitter-embed/blob/master/src/components/TwitterTweetEmbed.tsx#L41) since "require" is a Node thing.

When shipping to production you end up getting errors about "require" not being defined.

For my project I've manually patched this problem by creating a Yarn patch and refactored all usage of the require('scriptjs') in effects to use dynamic imports instead:

useEffect(() => {
  // ...
  const loadScript = async () => {
    const scriptModule = await import('scriptjs');
    const script = scriptModule.default;

    // ... specific script() call for given useEffect
  };

  loadScript();

  // ... the useEffect return statement
}, [/* the deps */];

I'm happy to provide a PR to make this change @saurabhnemade if desired.

asoulliereHT commented 1 year ago

related issue

https://github.com/saurabhnemade/react-twitter-embed/issues/105

qinhua commented 1 year ago

For me, simply adding a script tag to import require.min.js in index.html works fine.

jjsmclaughlin commented 1 month ago

I moved over to react-twitter-widgets https://www.npmjs.com/package/react-twitter-widgets

It's a drop-in replacement which doesn't have this problem.