When xInit attribute and the single spa application name has special characters like - , @, + , : etc. alpine js fails when it tries to execute the xInit function
On execution the single spa help will add a x-init function call to the dom
x-init="singleSpaAlpineXInit.alpinejs-App('alpine-alpinejs-App')" // DOES NOT WORK
Due to this alpinejs throws the following error:
alpine.js:1738 Uncaught ReferenceError: App is not defined
at eval (eval at o (alpine.js:112), <anonymous>:3:75)
at o (alpine.js:112)
at le.evaluateReturnExpression (alpine.js:1581)
at new le (alpine.js:1350)
at Object.initializeComponent (alpine.js:1735)
at alpine.js:1722
at alpine.js:1700
at Array.forEach (<anonymous>)
at Object.discoverUninitializedComponents (alpine.js:1699)
at alpine.js:1721
Possible Solution
create the global function for x-init after normalizing the application name and also initialize the x-init dom accordingly
function normalizeString(str) {
return str.replace(/[^a-z0-9,. ]/gi, '_')
}
let xInitFnName = `${normalizeString(name)}('alpine-${name}')` // where the name is the application name
When
xInit
attribute and the single spa application name has special characters like- , @, + , :
etc. alpine js fails when it tries to execute thexInit
functionTo reproduce:
On execution the single spa help will add a x-init function call to the dom
Due to this alpinejs throws the following error:
Possible Solution
normalizing
the application name and also initialize thex-init
dom accordingly