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

Make vite.config.ts support pure esm modules without setting type: "module" #6623

Closed
4 tasks done
egoist opened this issue Jan 25, 2022 · 3 comments
Closed
4 tasks done

Comments

@egoist
Copy link
Contributor

egoist commented Jan 25, 2022

Clear and concise description of the problem

In a cjs project (i.e. without type: 'module' in package.json), if you import a package written in pure esm in vite.config.ts, it will panic:

 Error [ERR_REQUIRE_ESM]: require() of ES Module

Since Vite will bundle vite.config.ts in cjs format.

Suggested solution

Switching to https://github.com/egoist/bundle-require:

It outputs the bundled config as .mjs with a random filename (to get rid of import cache) and loaded with dynamic import by default.

// Not sure if this would break existing behavior tho.

Alternative

Just adding type: 'module' to your package.json would also fix this.

Additional context

No response

Validations

@sodatea
Copy link
Member

sodatea commented Jan 25, 2022

Alternative: support vite.config.mts.

@Akryum
Copy link
Contributor

Akryum commented May 1, 2022

Reposting the minimal repro here:
https://github.com/Akryum/vite-config-esm-issue

@bluwy
Copy link
Member

bluwy commented Jun 26, 2022

vite.config.mts supported has landed in #8729. Given the thumbs-up for the alternative I'll go ahead and close this as resolved.

Though I think using bundle-require could potentially still be something we can explore if someone's interested in implementing it.

@bluwy bluwy closed this as completed Jun 26, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jul 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants