SolidStart prod build fails to start on `require()`
My site uses a library algosdk that has dependencies which utilize require() in them, which cause the production build to fail with the below error.
41 Replies
You need to use import instead of require.
It isn't my code, though, this is buried two dependencies deep in a library
There must be a way to do this because I can use this library in a React + Webpack client side app just fine
But I cannot get a Solid Start build to run
In Webpack 5 I can use some polyfills through react-app-rewired
So perhaps there is a similar approach that could work with SolidStart + Vite?
Maybe this works for you: https://github.com/vitejs/vite/issues/4209#issuecomment-919661782
GitHub
Commonjs import differs between dev & prod · Issue #4209 · vitejs/...
Describe the bug Bug A commonjs module default import will give a function in dev and an object in prod, resulting in an error on prod X is not a function Repro npm run dev Then check the console, ...
Very interesting, let me read this
The very next comment is concerning... esbuild in dev and rollup in prod
Would be great if that was unified
Vite already uses rollup for prod.
yes we are saying the same thing
but the very next comment highlights the inconsistency between dev and prod
GitHub
Commonjs import differs between dev & prod · Issue #4209 · vitejs/...
Describe the bug Bug A commonjs module default import will give a function in dev and an object in prod, resulting in an error on prod X is not a function Repro npm run dev Then check the console, ...
What is concerning you about that? eslint is specifically meant for pre-production purposes?
As a general statement, it is frustrating to have something work in dev and then fail in the prod build
That's the price you pay for the faster development.
That's kind of a flippant thing to say to someone trying to learn Solid/Start
I'm about 30 seconds from going back to React + Next
You're on the growth team? lol
I put a beginner tag on the help thread...
Sorry, I meant to say the price we pay. If you want to use solid with webpack, I can help you to get there, too.
Is the limitation at play here a function of Vite or of Solid Start?
This is a limitation of vite.
Yet also a strength, because the same distinction allows for things like hot module reloading during development.
I am going to read this thread here, too: https://github.com/vitejs/vite/discussions/4921
GitHub
Thoughts on dependencies handling · Discussion #4921 · vitejs/vite
Some thoughts regarding dependencies / SSR / prod vs dev: Apply plugins to non js/ts files found in dependencies during the pre-bundle phase. fixes e.g. #3910 avoids having to manually exclude pack...
I was going to research how to use turbo.build instead of vite for solid, but they currently only support react-style JSX transpilation at the moment and the swc plugin API is not yet stable.
What do you think about this comment? https://github.com/vitejs/vite/discussions/4921#discussioncomment-3762142
GitHub
Thoughts on dependencies handling · Discussion #4921 · vitejs/vite
Some thoughts regarding dependencies / SSR / prod vs dev: Apply plugins to non js/ts files found in dependencies during the pre-bundle phase. fixes e.g. #3910 avoids having to manually exclude pack...
They mention an experimental Vite feature that is disabled by default
Vite
Next Generation Frontend Tooling
It comes with the drawback that your production resources are less optimized than they would be by rollup. How much difference that will make is anybody's guess.
I'm new to this, but a wise person once told me that premature optimization is the root of many problems
You may have noticed that our community is pretty interested in performance.
When you say less optimized, do you mean for build speed or final JS size?
You know Don Knuth?
no lol
should I?
Final JS size.
He was the wise person who came up with that quote.
oh haha thank you I will read about him
Otherwise, he is known for creating the LaTeX typesetting engine.
OK so my "things to try" checklist include the experimental Vite feature, and, failing that, the @rollup/plugin-commonjs plugin
Anyways, back to your current issue. Once you use HMR, the code you are running during development is going to differ from the production code. It's a compromise for the sake of development speed and convenience.
So with that in mind, what do you think about this advice?
Include a minified browser bundle directly in your HTML like so:
<script
src="https://unpkg.com/[email protected]/dist/browser/algosdk.min.js"
integrity="sha384-1gIB0FiLMNmJ7adAfBOdH20Mnw0DZarB9D3PTozUAhKn/uT9CpHdaSbBIpmZTgrU"
crossorigin="anonymous"
></script>
Should I import algosdk normally for development but have the browser bundle utilized from there for production?
We do have a community primitive collection including a "script-loader" primitive: https://github.com/solidjs-community/solid-primitives/tree/main/packages/script-loader
GitHub
solid-primitives/packages/script-loader at main · solidjs-community...
A library of high-quality primitives that extend SolidJS reactivity. - solid-primitives/packages/script-loader at main · solidjs-community/solid-primitives
If you want to load it async in both cases.
Oh very interesting
Not sure if that package will benefit from tree shaking. In that case, it may be better to use it as a dependency.
it probably would
there is a bunch of stuff in it of which I need a small fraction
so that would mean avoid the script-loader?
It seems like my path forward is:
1. Try the experimental Vite feature
2. Try the commonjs plugin
3. Try the script-loader primitive
Only if the benefits of tree shaking were significant.
Sounds like a good plan.
4. could be to ask the developers of the package in question to fix their broken exports.
On this point, the actual issue isn’t even in this library, it is secondary dependencies underneath.
Thanks very much for your help, I have a lot to do now to see if I can get this working
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
lmao
@lexlohr Just to circle back here, I made some basic attempts on 1 & 2 above and then, upon further reflection, decided that using Solid Start fell into the category of "premature optimization" just for SEO on my landing page with no added value to other parts of my product.
I rewrote my app in a vanilla Solid + Vite repo and managed to get it to dev + build + start successfully with some configuration, which I have shared below.
Therefore, I have decided to abandon Solid Start given its current state, added complexity, and limited marginal value for my use case, which is intended to move logic & state to the client as much as possible.
The following vite.config.ts got me up and running from the Solid Typescript Minimal starter template:
Thanks for the feedback.