capacitor-community / electron

Deploy your Capacitor apps to Linux, Mac, and Windows desktops, with the Electron platform! 🖥️
https://capacitor-community.github.io/electron/
MIT License
318 stars 58 forks source link

Bug: getCapacitorElectronConfig() not working, due to minification's issue #200

Closed NoxFly closed 1 year ago

NoxFly commented 1 year ago

Describe the bug

The getCapacitorElectronConfig() in electron/index.ts returns an empty object {} while the file exists and the configuration isn't empty.

To Reproduce

  1. Just create any type of Ionic app, then add the @capacitor-community/electron platform. This creates the electron/ folder.
  2. Open electron/capacitor.config.ts, see that's not an empty config object.
  3. Open electron/src/index.ts, look where is called getCapacitorElectronConfig() and place a console.log() just below.
  4. Execute, and note that the logged object is empty.

Expected behavior

Having the real configuration object.

Screenshots

image

Desktop (please complete the following information):

Additional context

It seems that in node_modules/@capacitor-community/electron/dist/core/index.js, in this minified file, getCapacitorElectronConfig will call the Y() function.

I just tried some things on it and I found something.

I just developed it a bit so it's cleaner to show you were it is.

Here is the default minified Y function :

function Y(){let e={};return(0,h.existsSync)((0,b.join)(l.app.getAppPath(),"build","capacitor.config.js"))?e=require((0,b.join)(l.app.getAppPath(),"build","capacitor.config.js")).default:e=JSON.parse((0,h.readFileSync)((0,b.join)(l.app.getAppPath(),"capacitor.config.json")).toString()),e.electron&&(g=T(g,[e])),Z(g)}
function Y() {
    let e={};

    const path = (0,b.join)(l.app.getAppPath(),"build","capacitor.config.js");
    const pathJSON = (0,b.join)(l.app.getAppPath(),"capacitor.config.json")

    console.log('config exists ? -> ', (0,h.existsSync)(path));

    return (0,h.existsSync)(path)
        ? e = require(path).default
        : e = JSON.parse((0,h.readFileSync)(pathJSON).toString())
        ,e.electron && (g = T(g, [e])),Z(g); // <-- this line
}

The last line is the bug, and returns an empty object. The minifier puts comma , before e.electron and Z(g).

I don't know what's intented here, but removing this last line returns the object well.

Hence, you should check this function. It seems that your code is right but the minifier you use just broke your code here.

image

It's too bad, because I can't use CapacitorSplashScreen, and much more. Hope you'll fix this as soon as possible.

Another problem is that the CapacitorConfig does not have an electron property.

NoxFly commented 1 year ago

I found that following Ionic start project's order, it is not installing the Typescript version of @capacitor-community/electron, but a single javascript minified file, meaning you can't access all definitions etc..., and with function that's not completly working. As if it was giving you an older version.

To fix this problem, you just have to manually download the package, and it will be up to date. It will replace the entire package with the typescript version, and all definitions (interfaces etc...).

Then, in the capacitor.config.ts, you have to change the type CapacitorConfig to CapacitorElectronConfig (and import it).

fromage9747 commented 1 month ago

I found that following Ionic start project's order, it is not installing the Typescript version of @capacitor-community/electron, but a single javascript minified file, meaning you can't access all definitions etc..., and with function that's not completly working. As if it was giving you an older version.

To fix this problem, you just have to manually download the package, and it will be up to date. It will replace the entire package with the typescript version, and all definitions (interfaces etc...).

Then, in the capacitor.config.ts, you have to change the type CapacitorConfig to CapacitorElectronConfig (and import it).

Thanks mate. Was doing my head in!

fromage9747 commented 1 month ago

Now the problem that I face that by only making this change to the Electron version of the capacitor.config file, it gets overwritten by the original capacitor file when performing a build. If I make these modifications to the original file I get presented with the below:

Error: node_modules/@capacitor-community/electron/dist/core/ElectronSplashScreen.d.ts:1:27 - error TS7016: Could not find a declaration file for module 'electron'. '***r/electron/build/src/index.js' implicitly has an 'any' type.