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
SSG mode couses build fail in Vite js with vite-plugin-ssr #3832
Comments
To reproduceWe've recreated a MWE at: https://replit.com/@DmitriiKadchien/React-styled-components-SSG You can fork the replit and run Preview of the issueThe example works correctly in dev but fails to build, where the import of the styled components in the .js assets is pointing to .ts files (output with debug info in collapsible details below): Output of
|
|
I'm facing the same issue. I'm trying to setup Vite 3 with SSR and Styled-components and I'm getting same errors. Does anyone have any idea what could be causing this? I tried everything I could think of. |
|
It seems I found a workaround. I need to do more testing but now it seems it's working. It's related to this workaround. Basically I created a styled-components2.ts file on my root folder like this: And my second part to make it work with Vite, was to add a couple of resolve aliases on vite.config.ts like this: So basically, with the first file we are fixing the styled-components importing issue (importing styled or styled.default depending on the needs), and with the second step we are overriding the import "styled-components" to use our own. Honestly I don't like this workaround much, but at least is a first step to make it work :) |
My solution, based on what @adrifer commented, was to create a wrapper of styled components: // wrapper-styled-components.ts // component.style.ts Thanks for sharing @adrifer. |
|
I think I found a new cleaner workaround for this. The only change I did was to add this section to my In my case just adding this made it work. |
|
@adrifer Can you share your vite config and package.json? Our minimal example on replit from #3832 (comment) does not build with a similar error on the import for react-dom/server (which I also tried adding to noExternal, but still nothing, and similarly if I force everything to be noExternal) For reference, we switched to Next.js which felt overkill but I wish we went with that straight away... |
|
@adrifer i think your last solution is the cleanest way to implement ssr styled-components with vite. I think the reason why it fails is because of styled components "not using conditional exports to tell Node.js which bundle is ESM vs. CommonJS." If styled-components is externalized by vite as it is by default the transpiled output is something like this: Notice how styled-components is imported. The problem with this is that styled is default exported and you need to change from styled to styled.default in order for everything to work as expected.By adding Notice how styled-components is no longer "externalized" and it's now included in vite's pipeline (included the styled-components depedency and transpiled it correctly in the chunk).Why it works in dev but not in prod is maybe because the process is different as said in docs: Dependency pre-bundling only applies in development mode, and uses esbuild to convert dependencies to ESM. In production builds, @rollup/plugin-commonjs is used instead. |

Description
When running
yarn buildin React Vite project with vite-plugin-ssrwith babel-plugin-styled-components it fails with error:
[vite-plugin-ssr:autoFullBuild] Cannot read properties of undefined (reading 'withConfig') [vite-plugin-ssr:autoFullBuild] Cannot read properties of undefined (reading 'withConfig') (x2) error during build: TypeError: Cannot read properties of undefined (reading 'withConfig')without babel-plugin-styled-components it fails with error:
[vite-plugin-ssr:autoFullBuild] styled.div is not a function [vite-plugin-ssr:autoFullBuild] styled.div is not a function (x2) error during build: TypeError: styled.div is not a functionvite.config.ts
/renderer/_default.server.page.tsx
The text was updated successfully, but these errors were encountered: