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

Dynamic require of "xxx" is not supported #3509

Closed
noBaldAaa opened this issue Nov 28, 2023 · 3 comments
Closed

Dynamic require of "xxx" is not supported #3509

noBaldAaa opened this issue Nov 28, 2023 · 3 comments

Comments

@noBaldAaa
Copy link

noBaldAaa commented Nov 28, 2023

it's my code:

import * as ReactDOM from "react-dom";
import React from "react";

const App = () => {
  return (
    <div>
        hello world
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));

it's my config:

import { htmlPlugin } from "@craftamap/esbuild-plugin-html";

const options = {
  bundle: true,
  external: ["react", "react-dom"],
  plugins: [
    htmlPlugin({
      files: [
        {
         xxx,
          extraScripts: [
             {
               src: "https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js",
             },
             {
               src: "https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js",
             },
          ],
        },
      ],
    }),
};

code run error:

Error: Dynamic require of "react" is not supported
Error: Dynamic require of "react-dom" is not supported

I want to why?
how can fix?

@evanw
Copy link
Owner

evanw commented Nov 29, 2023

Typically this happens when you bundle code that does something like require('react'), you mark react as external (i.e. telling esbuild to exclude it from the bundle), and then run the bundled code in an environment that doesn't implement the require function (such as in a web browser).

To fix this, you need to either not mark react as external, in which case it will be bundled, or you need to implement a require function to tell esbuild where it can find the external dependencies. For example:

<script>
window.require = function(name) {
  if (name === "react") {
    return window.globalReactObjectFromSomewhere
  }
  throw Error("Don't know how to require " + name)
}
</script>

@noBaldAaa
Copy link
Author

Thank you for your reply。I understand its working principle,This is my solution:

    {
      name: "external-plugin",
      setup(build) {
        build.onResolve({ filter: /^react$/ }, (args) => {
          return { path: args.path, namespace: "react" };
        });
        build.onLoad({ filter: /.*/, namespace: "react" }, (args) => {
          return {
            contents: "module.exports=window.React",
          };
        });

        build.onResolve({ filter: /^react-dom/ }, (args) => {
          return { path: args.path, namespace: "react-dom" };
        });
        build.onLoad({ filter: /.*/, namespace: "react-dom" }, (args) => {
          return {
            contents: "module.exports=window.ReactDOM",
          };
        });
      },
    },

@noBaldAaa
Copy link
Author

but I think this is esbuild should solve,this make me very uncomfortable

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