Hi, I'm @ryusei__46.
Electron" is a desktop application development framework that has become a major player in the world in recent years. However, because it is not possible to make source code binary and confidential, many people have been putting off desktop application development with Electron, even though modern applications can be developed with it.
However, using "NW.js," a framework in the same family as Electron, source code can be made binary.
This article is a brief reminder of how to do this.
development environment
- OS:Windows 11 Pro
- node.js v 18.13
- npm v 6.19.1
- nw.js v 0.72.0
Source code compilation (binaryization)
To compile the source code, an officially provided compiler called "nwjc" is required. You need to install the development version package (SDK version) of this compiler nw.js with npm to use it.
npm install nw --nwjs_build_type=sdk -g
The above command will install the SDK version of nw.js globally. If you have not set the path in the environment variable, check and add the path to the "node_module" folder with the "npm root -g" command.
To compile JavaScript files using the "nwjc" command,
nwjc [コンパイルしたいファイルのパス] [コンパイル後のファイル名]
I think it is safe to use ".bin" as the file extension after compilation. Since it is a binary file, frankly speaking, any file extension is acceptable.
To read the binary file after compilation
nw.Window.get().evalNWBin(null, 'コンパイルしたファイルのパス');
method. When the application is distributed, it should be written in a script tag in the html file to execute the js process.
<!-- index.html -->
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>code test</title>
<script defer>
nw.Window.get().evalNWBin(null, 'main.bin');
</script>
</head>
<body>
<h2>Hello World.</h2>
</body>
</html>
Compilation of modules (binaryization)
As the scale of development expands, it is likely that more and more files will be divided and managed as modules, and in such cases, the source files to be loaded as modules can also be compiled.
The ".evalNWBin()" method described in the previous section cannot be used for loading modules. To load a compiled module file, you can compile the source file as a module by adding the "-nw-module" option when executing the "nwjc" command.
nwjc [Path of the file you want to compile] [File name after compilation] --nw-module
Then the compiled module file,
nw.Window.get().evalNWBinModule(null, 'module file', 'module name');
can be loaded using nw.Window.get(). The third argument, "module name," is an arbitrary string that you can specify yourself. It will be loaded with that specified module name, so when using "require()" or "import", ". /[specified module name]" when using "require()" or "import".
// main.js ≡ main.bin
nw.Window.get().evalNWBinModule(null, 'module.bin', 'modulle');
const test_module = require('./module');
console.log(test_module.sum());
// module.js ≡ module.bin
module.exports = {
number: 150,
sum: function() {
return this.number + 500;
}
}