Package only certain directories with asar in an Electron application

1.8k views Asked by At

I have an Electron application where I am trying to use asar to package the node_modules and sources directories excluding the other directories.

I noticed when building the application with asar enabled, the whole app directory is packaged. This is not ideal because some executables and DLL's need to be available outside the asar file.

What I've tried

Currently, I have a script that packages the sources and node_modules directories (using asar). This script is executed after Electron-Forge has built the executable. But Electron does not seem to automatically check the asar file for source and node_module files. I receive the error "Error: Cannot find module index.js". This gives the wanted file structure, but does not work with Electron (see "File structure" below).

File structure

File structure before creating executable:

- node_modules/
- sources/
- executable/
- images/

Wanted file structure after creating executable:

- resource/
   - app/
      - executable/
      - images/
      - sources.asar
      - node_modules.asar

or the following (where the app.asar file should only contain the sources/ and node_modules/ directory):

- resource/
   - app/
      - executable/
      - images/
   - app.asar

And it's mainly important that Electron knows when to use the asar file and when to use the files directly. Is it possible to configure it like this, or something similar?

Question

Is there any way to configure Electron/Electron-Forge to only package certain directories into asar files while copying the other directories into the resource/app directory?

1

There are 1 answers

0
Fisnik On

In your package.json in "build" section add the following code:

"build": {
  "asar": true,
  "asarUnpack": [
    "**/thefirstfolder/secondfolder/**/*"
 ],
},

This "**/thefirstfolder/secondfolder/**/*" will unpack everything that is the child of secondfolder