Include dll file in Electron Builder - electron

I have a dll-file that I need to use for a separate process that is started by the Main process in my Electron project.
So I want to include this dll in my electron project and I'm using electron-builder to build the project. The problem is that I don't know how to include the file without it being added in the asar package, which is not reachable from the separate process directly. When I use app.getPath() to get the path for the running instance it will look something like this:
C:\installPath\myProject\resources\app.asar\my.dll
my electron-builder.json currently looks like this:
{
"productName": "myApp",
"directories": {
"output": "release/"
},
"files": [
"**/*",
"my.dll"
],
"win": {
"icon": "dist",
"artifactName": "myApp.${ext}",
"target": [
"portable"
]
}
}
Are there any way to get my.dll included in the resources folder (or somewhere else) instead of in the app.asar?
Thanks!

Solution1:
You should be able to do it with "extraResources" param, put your .dll under resources folder, then create config param named "extraResources" under targeted dist.
The installation process should copy the file(s) to app’s resources directory
https://www.electron.build/configuration/contents#extraresources
Solution2:
You can download .dll file from a web server then move it to the desired place with fs.

If you Keep the DLL file in the Dist folder, Electron-builder tool will include that file into the app.asar file.
This is the limitation of that because if you trying to load the dll file using ffi module or your own written native node module then it will not load the file from app.asar file. so you have to keep the dll file outside of the app.asar file manually or make this copy setup part of your build system.

Related

ExtJS7: Disable resource compression for CMD packages in package.json

We have a custom ExtJS CMD package created and used for main application. CMD package uses external js that is already minified/compressed. However on production build for app ExtJS again compresses already compressed file, leading to errors in app wherever those external resources are referenced.
Do we have any flag that turns of compression in package.json for externally included resources?
Well, one answer would be to simply not package the external JS with your app, but to serve it separately. Guessing that's not what you want to do, though...
So, to package the file and not have it be compressed, you edit the app.json. In particular, the production block, where there should be a js entry. Make it look something like this:
"production": {
"js": [
{"path": "app.js", "bundle": true},
{"path": "external_file_that_should_not_be_compressed.js", "compress": false }
],
...
},

Electron builder and asset files

I have an issue building for distribution an electron app, on macOS.
I have some folders that are in the root dir of my project, next to my index.js, which I need to be included in my packaged app.
After trying by hardcoding each and every file inside of package.json, which didn't help, I ended up with the following block
"extraResources": [
{
"from": "files/",
"to": "files/",
"filter": [
"**/*"
]
},
{
"from": "assets/",
"to": "assets/",
"filter": [
"**/*"
]
},
{
"from": "accounts/",
"to": "accounts/",
"filter": [
"**/*"
]
}
]
If I run the .app it appears that the files are not there. Opening the Contents/Resources, I see they are there, so, toying around I discovered that the app is looking inside the app.asar container, where obviously the files are not.
I found a small hack by adding a couple of '..' in front of all my paths, so it goes outside the app.asar container but I really think this is a bit idiotic to be the solution to my issue.
Plus, I plan to build this for multiple platforms and I have a feeling that if I go through with this hacky way of fixing it, I'll have to create a hack for each platform, which I'd rather not do...
if you have files that you are using in the html (in my example assets/css etc.), or requiring in any file and have a folder structure like this, the folders would be automaticly in the asar.
Note: asar is read only package, you cant add, remove or edit files after creating this file.
the using of the css files in the assets/css folder is simple:
use the relative path from your html file and it would work on all plattforms.
if you have files that are not related to your project, or not used in your project but should be available in the packaged app:
you must use "extraFiles" in your package.json
extraFiles would copy the files to the app content directory on Mac OS, on Windows and Linux it will copy it to the app root directory. If you use extraResources it will copy the files to Contents/Resources on Mac and on Windows it will copy the files to resources Folder.
"extraFiles": [
"files",
"assets",
"accounts"
]
note: empty folders would not be copied
in your app use something like this module to get the root path of your app, and use your files with this path
alternative: use process.execPath and replace your executable name to get the correct path
based on your comment:
if you must load this files (css/js) in your app html, you can load dynamicly on the onload event with the solution i provided you above.

Include non-electron folder as part of the NSIS installation

I've been trying to add additional functionality to the electron installer, where I copy some files that are packaged inside the installer, but I receive a non-descriptive error when I try to compile my electron project to create the installer i.e. I get:
* writing effective config
* packaging
* building
x [object Object]
Here is what my script looks like:
!macro customInstall
Rename "$APPDATA\myfolder\img" "$APPDATA\myfolder\img-old"
SetOutPath "$APPDATA\myfolder"
File /nonfatal /a /r "additional_files\*"
CreateShortcut "$SMSTARTUP\mylink.lnk" "$INSTDIR\mylink.exe"
!macroend
Basically everything works except the file copy part. When I remove that part the project builds and compiles into an installer with no problems.
I've also tried to use CopyFiles instead of SetOutPath and File and it works as expected when I place the additional_files folder into the same folder as the installation (dist folder), but I want the folder to be packaged inside the installer. However, I cannot get the additional_files to be packaged with the installation.
I believe it's a location issue, that is, that the NSIS script cannot locate the additional_files/ folder. I've tried modifying the package.json file by adding to the files section the additional_files/ folder and placing it in the root of the project.
I've even tried placing it in the build folder where my installer.nsh script resides, but with no luck.
File looks for files relative to the directory where the .nsi is by default. /NOCD can be used to prevent that but I'm not sure if electron uses that switch.
!cd can be used inside a script to change the directory but I'm not sure if that is going to help you much in this case unless you are willing to use a absolute path and in that case you could just use the absolute path with the File instruction instead.
If you only know where your .nsh file is I suppose you could try File /r "${__FILEDIR__}\additional_files\*"
if you are using electron-builder you have two options inside the settings
extraResources this will copy files into the $INST_DIR/resources folder in your app (this is where the app.asar file is too), and you can access via process.resourcesPath, ex:
extraResources: [
{ from: './dist/ThirdPartyNotices.txt', to: 'ThirdPartyNotices.txt' },
]
extraFiles this would do the same but place the files into the $INST_DIR root folder of your installation ex:
extraFiles: [
{ from: './distrib/mytool.exe', to: 'mytool.exe' },
],
to get the root folder you can use something like remote.app.getAppPath().replace('resources\\app.asar', '').replace('resources/app.asar', '');
all info on: https://www.electron.build/configuration/configuration#overridable-per-platform-options

Writing to a packaged app folder in Electron

I'm creating a portable Electron app that writes some files to the program folder. It works pretty well when I package it without the --asar option in Electron-packager, which leaves the resources folder with plain html + js files.
Now when I try to compile it with the --asar option, so that it packages the resources folder into one file, I can't access the program directory any more with the following code:
remote.app.getAppPath()
This now returns the path of the asar file, so I can't really write to the application folder any more. Is there any way around this?
No, you cannot modify the asar of a running Electron app. You should be saving your config outside of the asar path.

execute main.js from a sub directory

Why on earth can I not move my main.js file into a sub directory and have electron build from the that sub directory? for instance my file structure:
app
|_package.json
|_node_modules
|_src
|_main.js
|_index.html
|_renderer.js
|_...
My package.json:
"main":"main.js",
"scripts" : {
"start": "electron ./src"
}
When I run start electron launches the intro screen("to start your application run ...") and not the application I created at ./src. Is there a package out there that allows for this to happen? is there something I'm missing in the package.json file?
Been a while since I posted to SO but this one got me stumped / its not clearly documented on Electron's site(you'd think it would be in architecture). I do see in the quickstart about file structure but it doesn't say I can't do as I illustrated above^ It's not really a big deal I just hate when Im forced into a specific file structure.
The Electron application I am developing makes also use of a main.js file located in a sub-directory, and this is how it would be documented in your package.json file:
"main":"src/main.js",
"scripts" : {
"start": "electron ."
}
I guess this is because npm needs the correct relative path of the startup script in package.json...
There may be also some relevant information in Writing Your First Electron App.

Resources