To relieve Windows
problem under the path name is too long issues
, slightly faster require
speed and simplicity hidden source code, we can choose to use packaged into asar
archive, only need to make some minor changes to the source code. Most users can easily implement this function, because it is electron-packager
in 060bdcb042b99b, electron-forge
and electron-builder
, and can be used out of the box.
Generate asar package
asar
tar
style archive format that combines multiple files into one file. Electron
not need to decompress the entire file, you can read any file content from it.
The application can be packaged into asar
according to the following steps:
- Install
asar
:
$ npm install -g asar
- Use
asar pack
package:
$ asar pack your-app app.asar
Use asar package
In Electron
There are two types of APIs
, respectively Node.js
provided Node API
and Chromium
provided Web API
. Both types of API
support reading files from the asar
Node API
Since Electron
in particular hit a patch, Node API
in such fs.readFile
or require
method and the like can be asar
regarded as virtual folders, read asar
inside and read from a file on the file system as the real.
Example:
For example, suppose we have a example.asar
package /path/to
$ asar list /path/to/example.asar
/app.js
/file.txt
/dir/module.js
/static/index.html
/static/main.css
/static/jquery.min.js
Read a file from the asar
const fs = require('fs');
fs.readFileSync('/path/to/example.asar/file.txt');
List all files in the root directory of the asar
const fs = require('fs');
fs.readdirSync('/path/to/example.asar');
Use a module in the asar
const BrowserWindow = require('electron').BrowserWindow;
var win = new BrowserWindow({width: 800, height: 600});
win.loadURL('file:///path/to/example.asar/static/index.html');
Web API
In the Web
page, the file:
protocol can be used to obtain the files in the asar
Like Node API
, the asar
package is regarded as a virtual folder.
Example:
For example, you can use $.get
to get the file:
<script>
var $ = require('./jquery.min.js');
$.get('file:///path/to/example.asar/file.txt', function(data) {
console.log(data);
});
</script>
Treat the asar package as a normal file
In some cases, for example, to asar
package file of 060bdcb042bc6c, we need to read the package file of asar
For this reason we can not use the built-in asar
function and the original fs
module identical original-fs
module.
Example:
const originalFs = require('original-fs')
originalFs.readFileSync('/path/to/example.asar')
You can also set process.noAsar
to true
to disable the support for asar
fs
const fs = require('fs')
process.noAsar = true
fs.readFileSync('/path/to/example.asar')
Node API flaws
Although we have done our best to make asar
package under Node API
tend to the real directory structure as much as possible, there are still some underlying Node API
we cannot guarantee its normal operation.
asar package file is read-only
asar
contents of the package can not be changed, so Node APIs
in those methods can be used to modify the file in treating asar
can not work properly package.
The working directory is invalid in the asar package
Although the asar
package is a virtual folder, there is no real directory structure corresponding to the file system, so it is impossible for us to set the working directory working Directory
to a folder in the asar
asar
the folder in cwd
API
in the form of 060bdcb042bd5a as a parameter will also report an error.
Certain APIs require additional decompressed asar packages
Most fs
can read files or file information from the asar
package without decompression, but when dealing with some underlying system methods that rely on the real file path, Electron
will extract the required files to the temporary directory, and then the temporary directory The real file path is passed to the underlying system method to make it work properly. For this type of API
, the cost will be slightly more.
The following are some API
that need additional decompression:
child_process.execFile
child_process.execFileSync
fs.open
fs.openSync
process.dlopen
Untrue statistics of fs.stat
fs.stat
for the files in the asar
package. The returned Stats
object is not an accurate value because these files do not actually exist in the file system. So apart from file size and file type, we should not rely on the value of the Stats
Execute the binary file in the asar package
Node
that can execute programs in API
, such as child_process.exec
, child_process.spawn
and child_process.execFile
, but only execFile
can execute the programs in the asar
Because exec
and spawn
allow command
replace file
as input, and command
needs to be shell
under 060bdcb042beef, there is currently no reliable method to determine command
is operating in a asar
package, and we can’t guarantee that we can still judge the file in the package. Replace the file path in command
without any side effects.
Add unpackaged files to the asar package
Some Node API
will decompress the file into the file system when invoking it. In addition to efficiency issues, it may also attract the attention of anti-virus software!
To solve this problem, we can use the --unpack
asar
package to exclude some files from being packaged in the asar
package. Here is how to exclude some native
modules for sharing purposes:
$ asar pack app app.asar --unpack *.node
After the above command, in addition to the generated app.asar
package, there is also a app.asar.unpacked
folder that contains excluded files. We need to copy this folder together and provide it to users.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。