require statements early execution issue when using commonjs() plugin with vite #19115
Unanswered
shashank-brightness
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I try to upgrade my old project from webpack to new vite bundler which was made using the awesome alpine js library.
But now i am getting few issues when it comes to importing modules and defining/using Alpine utilities itself. This is my vite.config.js file which i am using
commonjs()
plugin of rollup.and this is some code lines from default entry point which is
src/index.js
and i am trying to define some data there whenalpine:init
event is fired using its cdn based setup.i am initializing and requiring tooltip in
defineData
function as i will need to use globalAlpine
variable there, and it can be only available on window scope whenalpine:init
is fired when alpine finishes execution.However when i check in browser i am getting error that Alpine is not defined in
tooltip.js
. It seems that due to require statement is executing and put as module in pre-bundling step of vite, i can't use the nature of only runtime importing require.If i write
Alpine.data('tooltip', tooltip)
in defineData function itself, (of course with importing it at the top and only on top!!)then it works perfectly fine but as i also need to use some more data i want to modularize it for better organization purpose. I am also getting this issue in other files as well where i need to use Alpine utilities within data itself where Alpine stays undefined.
Also, i am not getting how i can use Alpine.$persist plugin and what name of global i should give inside the rollup options because if i give it a value of
Alpine.$persist
(on 2nd line ofglobals
config inoutput
) then also Alpine is not defined.Beta Was this translation helpful? Give feedback.
All reactions