neutralinojs / neutralino.js

JavaScript API for Neutralinojs

Home Page:https://neutralino.js.org/docs/api/overview

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parcel doesn't work with neutralino.js

Gabriel-Alves-Cunha opened this issue · comments

Hi, I have been on this all day and I've come to the conclusion that it is Parcel that somehow is screwing something up. I may very well be wrong, tho...

I discovered that this:

var NL_OS = "Linux";
var NL_VERSION = "3.0.0";
var NL_APPID = "js.neutralino.muse";
var NL_PORT = 43067;
var NL_MODE = "window";
var NL_TOKEN = "7GWTba8VMWivRcppp5g9V76PkNtFcLwh";
var NL_CWD =
	"/home/gabriel/Documents/VSCode/my_projects/muse/muse_web/muse_neujs";
var NL_ARGS = [
	"bin/neutralino-linux_x64",
	"--load-dir-res",
	"--path=.",
	"--debug-mode",
];

wich I don't know what is supposed to put that there, is never being put on the minified neutralino.js file.
If you try the Neutralino functions on the DevTools, it shows that it is not working.

This is the repo: https://github.com/Gabriel-Alves-Cunha/muse

To reproduce:

  • clone the repo.
  • yarn to install stuff.
  • neu update to install neutralino.
  • yarn dev to have Parcel compile the js and to run neu run.

Do you guys think I should open an issue at Parcel...?

These variables Are Not defined in the Client Library itself, instead they are defined on the compile time. which means the Neutralino Binary you are using already contains these Variables, as soon as you application is executed these variables are assigned values in the Global Scope.

@Gabriel-Alves-Cunha the way Neutralino works is that it hosts a little webserver to serve the files. That webserver special-cases neutralino.js, and injects global vars into it when serving the javascript. The problem is that Parcel compiles the file as a top-level entry point and names the resulting file something else. But neutralino is still looking for "neutralino.js", so the code to inject the vars never gets triggered, and the vars are never injected.

So we need to trick Parcel into ignoring the file for the purposes of compilation, but also make sure it ends up in the build. The way I did this was like this:

  1. Put neutralino.js into a static dir. I set "clientLibrary": "/app/static/neutralino.js", in neutralino.config.json
  2. Install parcel-resolver-ignore and parcel-reporter-static-files-copy as dev dependencies
  3. Configure them to deal with Neutralino (see below).

The first plugin avoids compiling the entry point and the second plugin copies it to the build as a separate file. That means the file ends up in the place neutralino is looking for it with the right name. Then it can do its job of injecting those vars.

.parcelrc:

{
  "extends": "@parcel/config-default",
	"resolvers": [
		"parcel-resolver-ignore", "..."
	],
  "reporters":  [
		"...", "parcel-reporter-static-files-copy"
	]
}

package.json:

  "parcelIgnore": [
    "neutralino.js"
  ]

@pegvin I will say this was a bit tricky to sort out. The process by which Neutralino makes these vars available to the UI isn't super obvious without reading a bit of the Neutralino's code, and that makes troubleshooting it challenging. Perhaps the docs can be improved here?

Please use neutralino.js from NPM: https://www.npmjs.com/package/@neutralinojs/lib

Thanks :)