electron-builder: 3.16.0
I have a project setup using the two package.json structure. Everything works fine now, but I need to dynamically ignore files from the package. To do this, I am switching from the CLI to the API.
My current build
command is:
build --arch ia32 --platform win32 -d
I have changed it to the following script:
'use strict';
const builder = require('electron-builder');
const opts = {
platform: [builder.Platform.WINDOWS],
arch: 'ia32',
dist: true,
devMetadata: {
build: {
win: {
ignore: function (file) {
// TODO: add logic here
console.log(file);
return false;
}
}
}
}
};
builder.build(opts)
.catch((err) => {
console.log(err);
process.exit(1);
});
However, the ignore
field is not being read; all files are packaged and all the filenames are not being printed. Any idea on why this is?
ignore
is an electron-packager option, and, so, must be specified in the build
, not in the os-specific win
.
Could you please explain why and what do you want to ignore, may be we can add some smart logic?
@develar I swear I tried putting it under build
and it didn't either, but it did this time. Thanks!
Could you please explain why and what do you want to ignore, may be we can add some smart logic?
I have environment variables coming from my automated build server that specify if certain addons should be included.
It seems, it can be integrated into electron-builder. We can use some env var to specify glob to ignore files. Propose and it will be implemented.
Is it possible to ignore files without writing a custom build script?
@andresmeidla Use https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#ignore as regexp.
Thanks, @develar, got it working.
Key thing to note is that the paths in the ignore field must be relative to the app path, in case the two package.json setup is used
@develar i think this option is essential. The main reason i want to use this for is to reduce my app size.
Right now my node_modules folder is 150mo big!
The reason is pretty simple, i use a lot of native modules and when i build them there are a lot of "obj" files created.
All those build artifacts need to be ignored.
Even more you can ignore all node modules tests, examples, docs , ....
Right now i can't get it to work, but i'll report on my future tests
@farfromrefug please see 2 previous comments. Do you use Two package.json structure?
The reason is pretty simple, i use a lot of native modules and when i build them there are a lot of "obj" files created.
I don't know, it is safe to ignore object files .o
?
@develar yes i do use the 2 package.json structure.
Well you need to know what you are doing but most build artifacts could and really should be ignored.
It even can be a security issue as some part of the code can be seen in the build artifacts.
To show you an example.
In this directory only opencv.node
should be kept and the whole dir is 100mo big ...
Also all vs projects are packaged right now.
As for getting ignore
to work i cant get it.
But i am a little bit lost:
I use the node API sa shown above.
I also have a "build" property in my project package.json.
Now that "build" property seems to be totally ignore.
to get it to work i have to do this
devMetadata = JSON.parse(readFileSync('./package.json')).build;
Though the doc says that my package.json "build":
"build": {
"asar":false,
"directories": {
"app": "build",
"buildResources": "buildResources"
},
"app-bundle-id": "aryballe.scrip.tester",
"app-category-type": "your.app.category.type",
"iconUrl": "http://www.iconarchive.com/download/i99386/dtafalonso/android-lollipop/Youtube.ico",
"osx": {
"title": "Script tester app",
"background": "buildResources/osx/dmg-background.png",
"icon": "buildResources/osx/icon.icns",
"icon-size": 80,
"target": "zip",
"contents": [
{
"x": 438,
"y": 344,
"type": "link",
"path": "/Applications"
},
{
"x": 192,
"y": 344,
"type": "file"
}
]
},
"win": {
"title": "Aryballe Script tester app"
}
should be loaded and that devMetadata
would override it.
It does not work for me (latest version).
Even worse for the"buildResources" variable to be seen i have to put "buid" as devMetadata
when from the doc, "build" should be a property of devMetadata
Any idea?
Oups got it ! I thought "directories" needed to be a child of "build" and it was not!
Everything working fine now.
Ok so i dropped by app size by around 80% just using the ignore function on the node_modules directory:
function(filePath) {
if (/node_modules/.test(filePath)) {
if (/\/(obj|test.*?|spec.*?|htdocs|demo|dist|example.*?|sample.*?)[\/$]/i.test(filePath)) {
return true;
}
if (/^(\..*|.*\.(sln|pdb|exp|lib|map|md|sh|gypi|gyp|h|cpp|xml|yml|html)|vcxproj.*|LICENSE|README|CONTRIBUTORS|vagrant|Dockerfile|Makefile)$/i.test(path.basename(filePath))) {
return true;
}
}
// console.log(filePath);
};
Could really use glob for this.
keep in mind that some should not be ignore in some app cases (html, xml,...)
Just leaving a comment here for anyone else who lands up searching for how to ignore files. You do that using the files configuration https://www.electron.build/configuration/contents#files.
e.g.
"build": {
"appId": "com.gettrici",
"productName": "Trici",
"mac": {
"category": "public.app-category.developer-tools",
"icon": "resources/images/trici_icon.icns",
"target": "zip",
"asar": false,
"compression": "store",
"files":["!builds/*","!patches/*","!packages/*"]
}
},
It was not immediately clear to me from the documentation how to ignore files.
Most helpful comment
Just leaving a comment here for anyone else who lands up searching for how to ignore files. You do that using the files configuration https://www.electron.build/configuration/contents#files.
e.g.
It was not immediately clear to me from the documentation how to ignore files.