herberttn / bytenode-webpack-plugin

Compile JavaScript into bytecode using bytenode
MIT License
105 stars 27 forks source link

`electron forge make` yields both a compiled file and a "plain text" file. If the plain text file is removed, the executable does not work anymore #29

Closed raphael10-collab closed 1 year ago

raphael10-collab commented 1 year ago

Example taken from here: https://github.com/herberttn/bytenode-webpack-plugin/tree/main/examples/electron-forge-typescript-webpack

electron forge make yields both a compiled file and a "plain text" file:

raphy@raohy:~/electron-forge-typescript-webpack/out/electron-forge-typescript-webpack-example-linux-x64/resources/app/.webpack/main$ ls -lah
total 44K
drwxrwxr-x 3 raphy raphy 4,0K ago 31 18:02 .
drwxrwxr-x 4 raphy raphy 4,0K ago 31 18:02 ..
-rw-rw-r-- 1 raphy raphy  23K ago 31 18:02 index.compiled.jsc
-rw-rw-r-- 1 raphy raphy 5,6K ago 31 18:02 index.js
drwxrwxr-x 2 raphy raphy 4,0K ago 31 18:02 native_modules

Once I remove the plain text file:

raphy@raohy:~/electron-forge-typescript-webpack/out/electron-forge-typescript-webpack-example-linux-x64/resources/app/.webpack/main$ rm index.js 

the executable does not work anymore :

raphy@raohy:~/electron-forge-typescript-webpack$ ./out/electron-forge-typescript-webpack-example-linux-x64/electron-forge-typescript-webpack-example 
[18682:0831/180643.305136:ERROR:node_bindings.cc(267)] Most NODE_OPTIONs are not supported in packaged apps. See documentation for more details.
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module '/home/raphy/electron-forge-typescript-webpack/out/electron-forge-typescript-webpack-example-linux-x64/resources/app/.webpack/main'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1002:15)
    at n._resolveFilename (node:electron/js2c/browser_init:2:109797)
    at node:electron/js2c/browser_init:2:115663
    at node:electron/js2c/browser_init:2:115906
    at node:electron/js2c/browser_init:2:115910
    at BuiltinModule.compileForInternalLoader (node:internal/bootstrap/loaders:331:7)
    at BuiltinModule.compileForPublicLoader (node:internal/bootstrap/loaders:269:10)
    at loadBuiltinModule (node:internal/modules/cjs/helpers:50:9)
    at Module._load (node:internal/modules/cjs/loader:862:15)
    at f._load (node:electron/js2c/asar_bundle:2:13330)
[18714:0831/180643.608410:ERROR:gpu_memory_buffer_support_x11.cc(49)] dri3 extension not supported.

So... may be I'm missing something...

because I do not understand the benefit and usefulness of producing a JS BInary, if without the plain text file, which is supposed to be removed if we want to hide the plain JS code, the executable does not work

jjeff commented 1 year ago

That looks correct. The index.js file is the loader file. It basically just loads the .jsc file. Is any of your original code contained in the .js file? It should all be compiled into the .jsc file. Electron has no way to load .jsc files, so Bytenode gets loaded through small .js files which basically just require() the .jsc file. It may look more complicated after running through Webpack, but none of your original code should be in there.

raphael10-collab commented 1 year ago

Thank you @jjeff

Looking forward to an example without node integration: https://github.com/herberttn/bytenode-webpack-plugin/issues/28

jjeff commented 1 year ago

Bytenode always requires Node to work. It will not work in an Electron renderer script without Node enabled. However, Node is always enabled in Electron preload scripts (which have access to the renderer scope). So a good practice is to have all of your Bytenode compiled code in your preload scripts.

raphael10-collab commented 1 year ago

Hi @jjeff

I've explained what I've done here: https://github.com/herberttn/bytenode-webpack-plugin/issues/30#issuecomment-1703033897

What should I practically add to the above preload.ts code in order to have my all Bytonode compiled code within preload.ts ?