Hello everyone, this is my first post here, I tried to look for information on Stackoverflow or Copilot but couldn't find any solution. I have a problem with my project, when I'm using astro dev it's working fine, but when I use astro build and try to deploy it says it can't find a variable cause it's undefined. It seems that astro build is removing a line from my code. Does anyone know how to fix it?
#Issue with minification of astro build command
1 messages · Page 1 of 1 (latest)
can you share what part is shown as undefined ?
it's just this line
let stack = 0
Line 111
I noticed that when I use astro build
and go to the minified code, that line dissapears
and that's why it gives me the error of stack being undefined
Hi, if you're referring to the following warning during build, it's just reminding you that a variable called stack is created but not used. It could be safely ignored
src/utils/parseEffect.js:111:7 - warning ts(6133): 'stack' is declared but its value is never read.
I've tried with output server or output static but I can't build the site for some reason
I don't get that message
That's weird. I've cloned you repo and successfully built locally without error. Could you share the error message during build?
Invoke Error {"errorType":"ReferenceError","errorMessage":"stack is not defined","stack":["ReferenceError: stack is not defined"]
the problem is not the build command itself
the problem is after the build command
that it deletes that line
and the site doesn't work
when I deploy the site and try to access it, then it shows that log
I went to check in the minified code and it was deleted, but I don't understand why it does that
can I make Astro not to minify a specific script?
so it uses script the way I coded it?
Issue with minification of astro build command
I changed the title just to make more clear the problem. I guess it was my bad that I didn't explaing correctly enough
Have you tried to remove the astro check from the build script?
I also had the same error, when I removed that astro check it worked.
In your package.json, check the build script build : astro check && astro build remove the astro check and just leave astro build.
So your script will become
build: astro build just
Does the error occur during deployment or invoking Netlify function?
During Invoking netlify function
When I went to ask in their forums first they said I have to use the pnpm flag —shamefully-hoist
But that didn’t fix the problem
It might be helpful to see what the function does. However I couldn't find related code in your repo. Could you point me to the right direction?
What function?
The one at Netlify? I think it’s the one that is made in build time with the adapter of Netlify
Called ssr.js
They all go to .netlify/internal-functions/ I think. Along with the minified code
There is where I noticed that astro build command is removing a line that believes it’s not used, and at build time it removes it, and when I deploy the site and try to run the website, it’s missing that line cause it was deleted in the build
Give me a moment and I’ll try to find again the difference between my script and the minified code
This is the line 111 in my code in parseEffect.js
The second screenshot is from _id_GWE4aFuC.mjs which is created with npm run build
Reading the documentation of Astro I found this
Would this help stop that script from being minified?
or did I missunderstand it?
with the option 'preserve'
Thanks for your patience, I finally start to understand:
The variable stack is actually used, but it is considered unused during build and gets removed because of minification
I guess the reason why it's considered unused is probably because the use of eval combined with these lines potentially accessing or manipulating stack or its related code.
Could you try changing the original code to this and see if it works?
- let stack = 0
+ eval('var stack = 0')
I'm trying it right now
And I'll give a response as quick as possible
Well, right now astro dev is not working as it says that stack is not defined. I'm gonna try to build anyway and deploy on netlify to at least see if that works
Mar 25, 03:45:25 PM: c19f6954 ERROR Invoke Error {"errorType":"TypeError","errorMessage":"Cannot read properties of undefined (reading 'includes')","stack":["TypeError: Cannot read properties of undefined (reading 'includes')"," at file:///var/task/.netlify/functions-internal/ssr/ssr.mjs:1157182:25"," at Array.map (<anonymous>)"," at file:///var/task/.netlify/functions-internal/ssr/ssr.mjs:1157181:381"," at AstroComponentInstance.ListOfItems [as factory] (file:///var/task/.netlify/functions-internal/ssr/ssr.mjs:82:12)"," at AstroComponentInstance.init (file:///var/task/.netlify/functions-internal/ssr/ssr.mjs:1331:33)"," at AstroComponentInstance.render (file:///var/task/.netlify/functions-internal/ssr/ssr.mjs:1336:22)"," at Object.render (file:///var/task/.netlify/functions-internal/ssr/ssr.mjs:1055:22)"," at renderChild (file:///var/task/.netlify/functions-internal/ssr/ssr.mjs:675:17)"]}
and I still get the same error of it being undefined
I'm gonna search on the minified code what happened
Well, now the line wasn't deleted in the minified code
the eval() stayed after the astro build
I'm gonna try sth
I'm just gonna give it a use like console.log it somewhere
so at least I make astro believe it's used
Hey, I think there's something wrong with ListOfItems.astro as well. You probably want to add this so that the itemTypeIds prop would not be undefined
const { title, itemTypeIds = [] } = Astro.props
// ^^^^^
ok, now astro dev works, although I've consoled logged a million 0s xD
I'm gonna do that too
thanks
I didn't noticed
I know that before astro build worked I had to fixed a lot of undefined warnings or any type warnings
but I never got a notification of that line
In order to keep the variable stack existed in both production and development, you could use this even uglier way:
eval('var stack = 0') // For production
var stack = 0 // For development
yeah but eval('var stack = 0') doesn't work in development nor production
Oh, sorry to hear that
I mean, it didn't make a difference
maybe it works, but not with the intention we wanted
OH MY GOD
the console log worked
I think this is by far the most i could help. I'll leave this to others. Good luck!
it's working now
Encyclopedia of Wakfu
I just had to console log a bazzillion 0s
xD
but thanks for the inspiration
No problem! I'm glad it worked somehow 😂