neutralinojs / neutralinojs-cli

neu cli for Neutralinojs
https://neutralino.js.org/docs/cli/neu-cli
MIT License
91 stars 57 forks source link

Conflict Hot Reload with Neutralino native API #177

Closed mp3846 closed 4 months ago

mp3846 commented 2 years ago

After enabling Hot Reload and running neu run --frontend-lib-dev Hot Reloading works fine but I can not access global variables like NL_OS !

Here is the result of running Demo project:

Neutralino Hot Reload Conflict With Native API

After running neu run --frontend-lib-dev --window-enable-inspector I get this error in the console:

Uncaught DOMException: Failed to construct 'WebSocket': The URL 'ws://localhost:undefined' is invalid.
at a (http://localhost:3000/neutralino.js:1:580)
at e.init (http://localhost:3000/neutralino.js:1:11526)
at ./src/index.js (http://localhost:3000/static/js/bundle.js:105:19)
at options.factory (http://localhost:3000/static/js/bundle.js:45618:31)
at __webpack_require__ (http://localhost:3000/static/js/bundle.js:45063:33)
at http://localhost:3000/static/js/bundle.js:46200:37
at http://localhost:3000/static/js/bundle.js:46202:12

Every thing is OK when I just run neu run (native API is accessible)

mp3846 commented 2 years ago

As discussed in this bug #909 some variables should be initialized first (for react it should be inside index.js)

import { BrowserRouter } from 'react-router-dom'
import { StrictMode } from 'react'
import App from './App'
import authInfo from '<AppName>/.tmp/auth_info.json' // using a symlink to access folders outside src
import ReactDOM from 'react-dom/client'

const root = ReactDOM.createRoot(document.getElementById('root'))
root.render(
    <StrictMode>
        <BrowserRouter>
            <App />
        </BrowserRouter>
    </StrictMode>
)

if (process.env.NODE_ENV === 'development') {
    window.NL_PORT = authInfo.port
    window.NL_TOKEN = authInfo.accessToken
    window.NL_ARGS = []
}

window.Neutralino.init()

Now we can access Neutralino API but still predefined global variables like NL_OS or NL_CWD are undefined!

Vexcited commented 1 year ago

Now we can access Neutralino API but still predefined global variables like NL_OS or NL_CWD are undefined!

I found a fix for Vite but you can edit my fix according to the build tool you're using, https://github.com/neutralinojs/neutralinojs/issues/909#issuecomment-1374735839

shalithasuranga commented 4 months ago

I think you can solve the issue by using the window. prefix while accessing global variables. Please use the latest neu CLI version which doesn't ask you to manually use the --frontend-lib-dev flag. Thanks :tada: