When I do an electron package, I want to hide the source code

When I was writing ‘electron-builder.yml’,

I’d like to pack with the exception of node_modules.

I wrote electron/package.json to read the bundled source code in electron/lib that occurs at build time.

{
“private”: true,
“name”: “@myproject/app”,
“description”: “testApp”,
“productName”: “Test App”,
“version”: “0.9.0”,
“main”: “lib/backend/electron-main.js”,
“theia”: {
“target”: “electron”,
“frontend”: {
“config”: {
“applicationName”: “Test App”,
“defaultTheme”: “light”
}
}
},

I want to read node_modules from path ‘lib’ where the bundled file is located to start path of option ‘main’. Is there any way??

node : 16.15.1
electron : 23.2.4
electron-builder : 23.0.3

Hey @hod4915

I’m unsure of what you’re trying to achieve. You want to only bundle the lib folder (i.e. only package your obfuscated code), but you also want access to your node_modules? I’m not sure how that can work.

Can you explain in more detail what you’re trying to do?

@msujew

Once electron/lib is created at the time of build, I want to create a package that works with only this source. Because if I include my source code in the ‘files’ of the electron-builder, the capacity of the package results increases and the actual source is exposed to the user. In fact, we’ve seen that theia 1.41.0 version works like this.

What stops you from excluding the src folder from your package? They aren’t supposed to go in there anyway. I don’t really see the problem here.

@msujew Thank you for your answer

After upgrading to version 1.40.0 after using an existing version of ‘theia 1.32.0’, it was confirmed that the main field in electron/package.json changed from “src-gen/backend/electron-main.js” to “lib/backend/electron-main.js”.

I did ‘build’ and ‘year electron start’, but I get this error.

PS C:\Users\project\myApp> yarn electron start
yarn run v1.22.19
$ yarn -s --cwd myProject/electron start
native node modules are already rebuilt for electron

C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:50
  throw new Error('node-loader:\n' + error);
  ^

Error: node-loader:
Error: C:\Users\project\myApp\myProject\electron\lib\backend\native/drivelist.node is not a valid Win32 application.
C:\Users\project\myApp\myProject\electron\lib\backend\native/drivelist.node
    at ../../node_modules/drivelist/build/Release/drivelist.node (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:50:9)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36780:42)
    at module.exports (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:25900:34)
    at ../../node_modules/drivelist/js/index.js (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:24238:27)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36780:42)
    at ../../packages/core/lib/node/env-variables/env-variables-server.js (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:2499:19)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36780:42)
    at ../../packages/core/lib/node/env-variables/index.js (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:2608:14)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36780:42)
    at ../../packages/core/lib/node/backend-application-module.js (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:2285:25)

Node.js v18.12.1
Failed to start the electron application.
Error: backend process exited
    at ChildProcess.<anonymous> (C:\Users\project\myApp\myProject\electron\lib\backend\electron-main.js:1199:28)
    at ChildProcess.emit (node:events:513:28)
    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
Done in 2.10s.

How do you solve it?

In my project, I need a drivelist, canvas, etc.

I upgraded to electron rebuild, node version 18.18.2, but it still doesn’t work.

However, if the main field of package.json is set to electron/src-gen/backend/electron-main.js, it works well. However, the package must start with the lib path.

Have you run theia electron:rebuild before building/bundling the application? The bundling process will copy a few native dependencies that need to be compiled for Electron before running the build process.

Note that running theia electron:rebuild before theia start is no longer necessary, as it has no effect on the bundled resources. We have written this in the changelog.

@msujew

Thank you for your help. But it still doesn’t work well.

My environment
node : 16.15.1
electron : 23.2.4
electron-builder : 23.0.3

Order of execution

  1. Initialize Project
  1. Remove ‘lib’, ‘node_modules’ directory in project
    1. yarn cache clean
    1. scm => discard All
  1. Installation and Build
    1. yarn install
    1. yarn theia rebuild
    1. yarn build
    1. yarn electron start
  1. Result log
PS C:\Users\project\myApp> yarn electron start
yarn run v1.22.19
$ yarn -s --cwd myProject/electron start

C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:50
  throw new Error('node-loader:\n' + error);
  ^

Error: node-loader:
Error: C:\Users\project\myApp\myProject\electron\lib\backend\native/drivelist.node is not a valid Win32 application.
C:\Users\project\myApp\myProject\electron\lib\backend\native/drivelist.node
    at ../../node_modules/drivelist/build/Release/drivelist.node (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:50:9)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at module.exports (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:25900:34)
    at ../../node_modules/drivelist/js/index.js (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:24238:27)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at ../../packages/core/lib/node/env-variables/env-variables-server.js (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:2499:19)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at ../../packages/core/lib/node/env-variables/index.js (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:2608:14)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at ../../packages/core/lib/node/backend-application-module.js (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:2285:25)

Node.js v18.12.1
Failed to start the electron application.
Error: backend process exited
    at ChildProcess.<anonymous> (C:\Users\project\myApp\myProject\electron\lib\backend\electron-main.js:1199:28)
    at ChildProcess.emit (node:events:513:28)
    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
Done in 1.98s.

I think I set the webpack.config.js wrong, but I’m not sure what to do.

++ I just raised the node version to 18.12.1 and tried again, but the same result came out.
+++ This is the webpack.config.js you created.

Actually, I don’t understand webpack very well.

// @ts-check
const configs = require('./gen-webpack.config.js');
const nodeConfig = require('./gen-webpack.node.config.js');

const jsLoader = {
    test: /\.js$/,
    use: ['babel-loader'],
    exclude: /node_modules/,
};

// Frontend configurations
configs.forEach(config => {
    config.module = config.module || { rules: [] };
    config.module.rules = config.module.rules || [];
    config.module.rules.push(jsLoader);
});

// Backend configuration
nodeConfig.config.module = nodeConfig.config.module || { rules: [] };
nodeConfig.config.module.rules = nodeConfig.config.module.rules || [];
nodeConfig.config.module.rules.push({
    test: /\.node$/,
    use: ['node-loader']
});

module.exports = [
    ...configs,
    nodeConfig.config
];

Why do you add another instance of node-loader to the backend config? The generated backend config already contains an instance of that.

Does webpack generate the native binaries correctly into lib/backend/native?

yes
image

However, when opened with a text editor, the drivelist.node file in the electron/backend/native directory is not binary data format,

try {
process.dlopen(module, __dirname + require(“path”).sep + webpack_public_path + “eb2a6c4c704ef5b3a4ccb7fdd3ed7c9c.node”);
} } catch (error) {
throw new Error(‘node-loader:\n’ + error);
}

It comes out like this.

I added another loader because I didn’t know much about webpack, so I Googled it.

++ The actual binary file is electron/backend/eb2a6c4c704ef5b3a4ccb7fdd3ed7c9c.node.

Is this how it’s created?

+++ Initialize webpack.config.js

Yarn install - yarn theia rebuild - yarn build - yarn electron start I got a different error!

So glad to see another error for the first time!

Failed to start the backend application:
Error: Cannot find module '@project\myApp/lib/node/crayo-backend-module'
    at webpackMissingModule (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:26026:117)
    at C:\Users\project\myApp\myProject\electron\lib\backend\main.js:26026:246
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.start (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:3475:20) {
  code: 'MODULE_NOT_FOUND'
}
C:\Users\project\myApp\myProject\electron\lib\backend\main.js:3472
    throw reason;
    ^

Error: Cannot find module '@project\myApp/lib/node/crayo-backend-module'
    at webpackMissingModule (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:26026:117)
    at C:\Users\project\myApp\myProject\electron\lib\backend\main.js:26026:246
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.start (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:3475:20) {
  code: 'MODULE_NOT_FOUND'
}

Node.js v18.12.1
Failed to start the electron application.
Error: backend process exited
    at ChildProcess.<anonymous> (C:\Users\project\myApp\myProject\electron\lib\backend\electron-main.js:1199:28)
    at ChildProcess.emit (node:events:513:28)
    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
Done in 2.16s.

++++ Should I modify webpack.config.js now??

++ The actual binary file is electron/backend/eb2a6c4c704ef5b3a4ccb7fdd3ed7c9c.node.
Is this how it’s created?

Ok, that brings us closer to the problem. The .node files in the native folder shouldn’t be text files but actual OS binaries.

As for the new issue - it seems like your extension code cannot be found. Do you have a reproducible example? I don’t really have an idea why your extension code is missing.

@msujew Let’s look for examples that can be implemented.

@msujew

I think it’s going to be hard for Jae Hyun. Because I’m not using the released version of the canvas module, but I have to use the master branch version of github with napi, the original build process of my version of theia 1.32.0 is (upgraded from theia 1.32.0 to 1.40.0 now)

  1. clone
  2. yarn install
  3. yarn build
  • compile
  • Download the plug-in
  • Browser & Electronic Bundle
  • yarn-s --cwd myProject/myCode Canvas Module Reconfiguration

++ I tried changing the version of canvas to 2.11.2 (the latest version) and building it, but I got the same error.

PS C:\Users\project\myApp> yarn electron start
yarn run v1.22.19
$ yarn -s --cwd myProject/electron start

Failed to start the backend application:
Error: node-loader:
Error: The specified module could not be found.
C:\Users\project\myApp\myProject\electron\lib\backend\native/canvas.node
    at ../../node_modules/canvas/build/Release/canvas.node (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:16:9)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at ../../node_modules/canvas/lib/bindings.js (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:10954:18)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at ../../node_modules/canvas/lib/canvas.js (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:10984:18)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at ../../node_modules/canvas/index.js (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:10216:16)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at ../../node_modules/jsdom/lib/jsdom/utils.js (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:141979:18)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
C:\Users\project\myApp\myProject\electron\lib\backend\main.js:3472
    throw reason;
    ^

Error: node-loader:
Error: The specified module could not be found.
C:\Users\project\myApp\myProject\electron\lib\backend\native/canvas.node
    at ../../node_modules/canvas/build/Release/canvas.node (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:16:9)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at ../../node_modules/canvas/lib/bindings.js (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:10954:18)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at ../../node_modules/canvas/lib/canvas.js (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:10984:18)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at ../../node_modules/canvas/index.js (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:10216:16)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)
    at ../../node_modules/jsdom/lib/jsdom/utils.js (C:\Users\project\myApp\myProject\electron\lib\backend\vendors-node_modules_drivelist_build_Release_drivelist_node-node_modules_stroncium_procfs_lib-0ab3af.js:141979:18)
    at __webpack_require__ (C:\Users\project\myApp\myProject\electron\lib\backend\main.js:36866:42)

Node.js v18.12.1
Failed to start the electron application.
Error: backend process exited
    at ChildProcess.<anonymous> (C:\Users\project\myApp\myProject\electron\lib\backend\electron-main.js:1199:28)
    at ChildProcess.emit (node:events:513:28)
    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
Done in 2.15s.

When bundling native modules, do I have to do something else with webpack.config.js?

Usually, it shouldn’t be necessary to configure any additional stuff. Does the canvas.node file exist in your lib/backend/native folder?

Tomorrows release will release with a fix for some backend bundling - it might be that there’s a warning/issue in your build that the erroneous webpack plugin currently swallows. You can probably test it out tomorrow.

Okay. I solved it with - “node_modules/canvas/**” in files option on “electron-builder.yml”…