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

ESM wrapper not initialized before accessing module exports #3357

Open
Methuselah96 opened this issue Aug 30, 2023 · 2 comments
Open

ESM wrapper not initialized before accessing module exports #3357

Methuselah96 opened this issue Aug 30, 2023 · 2 comments

Comments

@Methuselah96
Copy link

Methuselah96 commented Aug 30, 2023

Issue description

esbuild is failing to make sure that the ESM wrapper (i.e., the init_* function) is getting called before accessing the variables exported from the module in some cases.

I've created a reproduction of this happening.

Including both ESM and CJS is necessary to trigger the creation of ESM wrappers. Code splitting is also used in the reproduction and is likely necessary to trigger the bug.

The problematic output file is dist/Popper.js:

import {
  Popper
} from "./chunk-67J4M5PZ.js"; // <--- Imports Popper before ESM wrapper `init_styled` is called.
import "./chunk-IBEMUV7X.js"; // <--- Calls ESM wrapper `init_styled`.
import "./chunk-5KLWG3PA.js";
export {
  Popper
};

Running dist/Popper.js throws an error (i.e., TypeError: styled is not a function) because the styled module has not been initialized.

Downstream issues

This issue causes many problems for Vite and Remix users when using MUI (which is how I wound up here). Some unresolved issues in other repos related to this are:

Related issues

@Methuselah96 Methuselah96 changed the title Re-exported module not initialized ESM initialization wrapper not called before accessing module exports Aug 30, 2023
@Methuselah96 Methuselah96 changed the title ESM initialization wrapper not called before accessing module exports ESM wrapper not initialized before accessing module exports Aug 30, 2023
@hmd-ai
Copy link

hmd-ai commented Sep 12, 2023

Thanks @Methuselah96 for summarising this issue, did you find a way to get around this? e.g. by changing esbuild options?

@Methuselah96
Copy link
Author

Methuselah96 commented Sep 12, 2023

I ended up patching the package.json of @mui/icons-material with yarn patch to add an exports field that exposes the ESM files.

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