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

TypeError: <default export> is not a function when using "type": "module" #11

Closed
rschristian opened this issue May 2, 2021 · 4 comments · Fixed by #16
Closed

TypeError: <default export> is not a function when using "type": "module" #11

rschristian opened this issue May 2, 2021 · 4 comments · Fixed by #16

Comments

@rschristian
Copy link
Member

Currently setting up Vite's ESM SSR output when I ran across this.

vite.config.js I believe always loads externals via require() for the moment. Not sure if that's fixable using plain tsc, but worst case scenario the CJS exports just need a little rewrite at build time.

Side note: Man is that a bizarre way of doing things. You can "use" import, but not really.

@marvinhagemeister
Copy link
Member

vite.config.js is internally transpiled via esbuild by vite and it rewrites all import statements to commonjs. The SSR output assumes a commonjs enviornment too: https://github.com/vitejs/vite/blob/e641d8e2970edf925ff1ef69fb0b24d5f5e22f05/packages/vite/src/node/ssr/ssrModuleLoader.ts#L93 . I'm not sure if there is anything we can do about that on the preset side. It seems like making vite aware of an ESM environment should be done in vite.

@rschristian
Copy link
Member Author

rschristian commented May 2, 2021

Oh sure, but hopefully this will change that: vitejs/vite#2157. Not that this means it'll get merged any time soon, but it's in the works™

And I'm not going to argue that this isn't silly, but changing the CJS export to

module.exports = preactPlugin;
preactPlugin["default"] = preactPlugin;

would get the job done. It's what they do for the react-refresh plugin.

@marvinhagemeister
Copy link
Member

Ohh I get it now. I was under the impression that the framework preact could not be found during SSR. Wasn't aware that you were referring to the preset itself. We should be able to do something about that.

@rschristian
Copy link
Member Author

Oops, sorry! Could've written that a lot clearer now that I realize how vague that is, and how it also depends on using that as the name for the default import. Totally blanked there, my bad!

@rschristian rschristian changed the title TypeError: preact is not a function when using "type": "module" TypeError: <default export> is not a function when using "type": "module" May 4, 2021
Mesteery added a commit to Mesteery/preset-vite that referenced this issue Jul 18, 2021
Since Vite templates use ESM, there is no reason for this module to stay in
CommonJS, or to provide a CJS and ESM interface. This only complicates things.

Fixes: preactjs#11.
Closes: preactjs#12.
@Mesteery Mesteery mentioned this issue Jul 18, 2021
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 a pull request may close this issue.

2 participants