How to build electron application with assets folder by Electron-builder - electron

I have built an application with electron-builder. There was a video in media folder in the development area. But when I built the application I figure out the video folder replaced with asar file in a resource folder in C://Programs.
I want videos to have in media folder in production. How can I do that

You may want to try with asar: false in your package.json file. But, this is not the recommended way to do it.
The best you can do is to add the resources you want to keep outside the asar file with the extraResources directive:
"build": {
"extraResources": [
{
"from": "../media",
"to": "media",
"filter": [
"**/*",
"!*.mp3",
]
}
],
"asar": false,
}

The accepted answer did not work for me... What did work was to specify the directory whose contents I wanted to access post-build in the same format as specified for the files configuration.
electron-builder.json:
{
...
"extraResources": [
"src/assets/my-assets/*"
],
...
}
After the build, you will find your folder at the same location as the asar file (.../app/resources/src/assets/my-assets), under resources. There's no need to specify asar: false.
On the main process, if you use __dirname, it will end with app.asar. The assets can be accessed using:
// Removing 8 characters to remove "app.asar"
let strippedPath = __dirname.substring(0, __dirname.length - 8);
let execPath = '\"' + strippedPath + 'src\\assets\\my-assets' + '\"';
Now execPath can be appended to with whatever needs to be executed from the assets folder.

Related

How do I add an additional postcss plugin via the new #angular/cli v7 angular.json or custom-webpack.config.js?

#angular/cli#7+ allows a customWebpackConfig to be specified to provide custom webpack configuration, such as:
"architect": {
"build": {
"builder": "#angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./build/custom-webpack.config.js",
"mergeStrategies": {
"externals": "prepend"
}
},
...
This file then technically allows you to prepend, append or replace any portion of the webpack configuration. Prior to upgrading to #angular#7.1.3 and #angular/cli#7.1.3 we had ejected the webpack configuration to make some additions. One such addition was the postcss-momentum-scrolling postcss-loader plugin that automatically enabled iOS momentum scrolling on all scrollable containers.
I am seeking support on figuring out how to generate the necessary custom webpack code to load this plugin via the supported customizations allowed by #angular/cli#7+.
Here is a sample of what I have tried in my custom-webpack.config.js file:
const postcssMomentumScrolling = require('postcss-momentum-scrolling');
module.exports = {
module: {
rules: [
{
test: /\.scss$|\.sass$/,
use: [
{
"loader": "postcss-loader",
"options": {
"plugins": [
postcssMomentumScrolling(),
]
}
}
]
},
],
},
};
As soon as I touch the scss chunk of the webpack config, it seems to do a replace instead of a merge or prepend, breaking the build.
I am wondering if anyone has a guide or suggestions on how to see what the initial webpack configuration that #angular/cli generates that is the starting point for modifications and a way to preview/peek at the code to be executed as debugging.
Also, an example of a similar customization would be great.
Thanks!
I think you need to tell to "customWebpackConfig" which portion to merge. Like this:
"mergeStrategies": {
"module.rules": "prepend"
}
In this way you're going to tell to merge with prepend strategy.
According to "custom-webpack" documentation it should default to "append" which doesn't seem the case in your example.
It's been a while since you've put the question but I wanted to actually ask if you have been able to fix it since I'm running in some issues getting my "module.rules" merged...it seems to work only if I set "replace" strategy.

Why TFS Build Step Extension Icon Is Missing?

I created a new extension for TFS following MS tutorial. For some reason when I'm adding Icon to my extension I can see this icon when I'm installing the extension and in the "Extension Manager" page,
But when I choose my extension from the build step menu the image is missing.
In the "vss-extension.json" file I added:
"icons": {
"default": "images/icon.png"
},
"files": [
{
"path": "images",
"addressable": true
},
{
"path": "dist",
"addressable": true,
"packagePath": "scripts"
},
{
"path": "infoTab.html",
"addressable": true
},
{
"path": "node_modules/vss-web-extension-sdk/lib",
"addressable": true,
"packagePath": "lib"
},
{
"path": "buildtask"
}
],
The image file is 32x32
Should this image be reference in the "task.json" file as well?
The accepted answer is not correct for Microsoft Visual Studio Team Foundation Server version 15.105.25910.0. Perhaps it was correct for previous versions.
The image file must be named icon.png.
The image file must be in the same folder as task.json.
The image file should be 32 x 32; no image scaling is applied.
The task.json file does not contain any reference to this file. It is located by using these conventions.
The task itself has its own icon and it must be stored in the same directory as the task.json and must be called icon.png and be 32x32 pixels and optionally an additional icon.svg can be put alongside it. This has to do with the fact that one extension can contain multiple build tasks, each build task then has its own icon. It's not referenced from the task.json, the correct file name will cause it to be picked up.
For an example, check my Azure Pipelines Snyk task. Also, if this is your complete extension manifest, then it's missing the Build task contribution point:
"contributions": [
{
"id": "buildtask",
"type": "ms.vss-distributed-task.task",
"targets": [
"ms.vss-distributed-task.tasks"
],
"properties": {
"name": "buildtask"
}
}

how to configure files paths in VSCode task errors

I configured a task in VSCode to compile a Delphi 2005 dpk. It is working and returning the errors on the "problems view", but it is not showing that errors in the file.
I think it is happening because when I click on an error, I get the error message:
Unable to open 'sr075pro.pas': File not found
(...projectfolder\sr075pro.pas)
But the file is in ...projectfolder\webservices\sr075pro.pas.
I can't find a way to tell to the task that the file is in a subfolder. I tried to use the "relative" option on the "fileLocation" tag without sucess.
The error returned:
Compiling sa_webservices...
Borland Delphi Version 13.0 Copyright (c) 1983,99 Inprise Corporation
sr075pro.pas(99) Error: Undeclared identifier: 'ni'
sa_webservices.dpk(802) Fatal: Could not compile used unit 'sr075pro.pas'
My task configuration:
{
"version": "0.1.0",
"name": "Compilar",
"command": "C:\\Compilers\\compile.bat",
"suppressTaskName": true,
"isShellCommand": true,
"isBuildCommand": true,
"tasks": [
{
"taskName": "Compile sa_webservices",
"isBuildCommand": false,
"isTestCommand": false,
"showOutput": "always",
"args": [
"sa_webservices"
],
"problemMatcher": {
"owner": "external",
"fileLocation": "relative",
"pattern": {
"regexp": "^([\\w]+\\.(pas|dpr|dpk))\\((\\d+)\\)\\s(Fatal|Error|Warning|Hint):(.*)",
"file": 1,
"line": 3,
"message": 5
}
}
}
My compile.bat:
#echo off
#P:
#set arg1=%1
shift
...
if "%arg1%" == "sa_webservices" set arg2="webservices"
...
echo Compiling %arg1%...
cd\%arg2%
dcc32.exe -H -W -Q %arg1%.dpk
Your task configuration is wrong. First of all you don't close all brackets but I guess it's a mistake made by copying and pasting it here on StackOverflow. Otherwise the task configuration wouldn't have worked at all.
Now to the real problem:
DCC32 produces hints and warnings containing relative file paths. These paths are relative to the project file. In your task configuration you define the compiler's output to contain relative paths by setting
"fileLocation": "relative"
Visual Studio Code doesn't know how to build the correct absolute path from the relative paths given by the compiler message. So it guesses your current ${workspaceRoot} (in your case it's projectfolder) would be the absolute path.
This explains why you see errors and warnings which contain wrong file paths. In order to get the correct paths you'll need to tell VSCode the correct path to combine the relative paths with.
You do this by simply adding the correct path to the fileLocation entry in you tasks.json:
"fileLocation": ["relative", "${workspaceRoot}\\webservices"]
The entire tasks.json looks like that:
{
"version": "0.1.0",
"name": "Compilar",
"command": "C:\\Compilers\\compile.bat",
"suppressTaskName": true,
"isShellCommand": true,
"isBuildCommand": true,
"tasks": [
{
"taskName": "Compile sa_webservices",
"isBuildCommand": false,
"isTestCommand": false,
"showOutput": "always",
"args": [
"sa_webservices"
],
"problemMatcher": {
"owner": "external",
"fileLocation": ["relative", "${workspaceRoot}\\webservices"],
"pattern": {
"regexp": "^([\\w]+\\.(pas|dpr|dpk))\\((\\d+)\\)\\s(Fatal|Error|Warning|Hint):(.*)",
"file": 1,
"line": 3,
"message": 5
}
}
}
]
}
It might be easier to find files in the problemMatcher in vscode 1.74, see file location search: v1.74 release notes. There is a new option search for the fileLocation property:
New file location method; search
Previously, problem matchers needed to know exactly where to look for
the problematic files, via the fileLocation property. The supported
methods were absolute, relative, or autoDetect (i.e., check for
relative paths first and opt to absolute paths in case of failure).
However, in workspaces that need to invoke various scripts residing in
nested sub-directories, the developers could have a hard time setting
up their tasks; since such scripts seldom report file paths in a
unified manner (e.g., relative to the workspace's base directory).
To help alleviate the problem, a new file location method, named
search, is introduced in this version. With this method, a deep file
system search will be initiated to locate any captured path. See the
example below on how to setup the search file location method
(although, all parameters are optional):
// ...
"fileLocation": [
"search",
{
"include": [ // Optional; defaults to ["${workspaceFolder}"]
"${workspaceFolder}/src",
"${workspaceFolder}/extensions"
],
"exclude": [ // Optional
"${workspaceFolder}/extensions/node_modules"
]
}
],
// ... } ```
⚠️ Of course, users should be wary of the possibly **heavy file system
searches** (e.g., looking inside `node_modules` directories) and set
the `exclude` property with discretion.

How to get (tray) icon path/image in electron-builder

I'm using electron-react-boilerplate to develop electron app (which uses electron-builder to pack apps).
I want to create tray, but it requires icon path or native image. The question is how to retrieve icon image from electron-builder or how to tell electron-builder to include icons dir into resources (without packing), so I can use:
appIcon = new Tray(iconPath | nativeImage)
I kind of struggled with a solution about non-packaged assets (such as media or JSON config files), mostly because I was not familiar with Electron until now. :)
I built a simple personal tray-only app and I didn't want to repackage every time I change an icon for instance.
If you too plan on using changing/dynamic assets you can distinguish between "development" and "production" using this property:
https://electronjs.org/docs/api/app#appispackaged-readonly
Make sure you have this in your package.json:
"build": {
...
"extraResources": [
"./assets/**"
],
}
https://www.electron.build/configuration/contents#extraresources
Then in your code you can have:
const assetsPath = app.isPackaged ? path.join(process.resourcesPath, "assets") : "assets";
Of course you can also use a different path for storing assets, independent of your packaged app folder, for example your user's home or user's documents:
https://electronjs.org/docs/api/app#appgetpathname
Electron v7.0.1
electron-builder 21.2.0
Firstly you need to tell electron-builder which extra files need copying into your output build. I copy over native drivers for each os like below, but you should be able to adapt this to your needs. The "to": "resources" means you'll be able to use the next code to find the files later.
"build": {
...
"extraFiles": [
{
"from": "resources/${os}/drivers",
"to": "resources",
"filter": [
"**/*"
]
}
],
Then to get access to that path from in electron you can use:
const path = require('path');
const imgPath = path.join(process.resourcesPath, 'image.png')
If you're in the main process you can omit the remote part.
You can then use nativeImage.createFromPath to get a native image:
const nativeImage = require('electron').nativeImage
let image = nativeImage.createFromPath(imgPath)
Thanks, Tim, your answer gave me a good thought. I reused it with some addition depending on how I run my app - form vs code using electron or from installed deb file:
"build": {
...
"extraFiles": [
{
"from": "assets",
"to": "resources",
"filter": [
"**/*"
]
}
]
...
}
And then:
let imgPath = process.env.DEV ? "assets/icon.png" : path.join(process.resourcesPath, "icon.png");
tray = new Tray(imgPath);

Gulp task to bundle bower_components?

I'd appreciate it so much if someone could please tell me a good way to handle bundling bower components. I feel like I've tried everything... Although, I can't seem to find any gulp tasks that handle this already. Could it really not exist?
Let's say it doesn't exist. Worst case scenario is I have to specify the paths of each "dist" file from bower_components folder. (It is annoying that each component seems to have its own "dist" folder... nothing is standardized.)
So even if I do that, I've noticed some components like 'active-support' seem to have require('lodash') and such in them. I think that if I just simply copy that file, it will break because the requires won't resolve.
What am I missing? How do I simply take all bower_components and bundle into a "common.js"... is there a way or is it a clusterfluck?
Update
As pointed out by Alerty, the new Gulp policy seems to be: use bower directly and glob patterns (and hope that the packages maintainers have a proper "ignore" properties).
Previously
You can use main-bower-files or gulp-bower-src to get files from your bower components. They can also use "ignore" or "main" overrides in your own bower file.
This is how I managed it (but see gulpfile.js for project structure, it's not a single gigantic gulpfile) :
https://github.com/notbrain/viceroy/blob/master/gulp/tasks/bower.js
Would be a bit more modular to simply concat all bower deps and then do the uglify() and minifyCSS() tasks separately on dist/ source locations, based on dev/prod env targets, but will have to wait for future updates.
Use main-bower-files:
var gulp = require('gulp');
var mainBowerFiles = require('main-bower-files');
gulp.task('TASKNAME', function() {
return gulp.src(mainBowerFiles())
.pipe(/* what you want to do with the files */)
});
if you have folders like:
-app
-bower
-node_modules
Gulpfile.js
package.json
the solution are:
gulp.task("connect", function () {
connect.server({
root: ["app"],
livereload: true,
port: 8034,
middleware: function (connect) {
return [connect().use("/bower", connect.static("bower"))];
}
});
});
If your project use AMD specification.
You can use gulp-edp bundle the modules.
set module info at module.conf
{
"baseUrl": "src",
"paths": {},
"packages": [
{
"name": "etpl",
"location": "../bower_components/etpl/3.0.1/src",
"main": "main"
},
{
"name": "jquery",
"location": "../bower_components/jquery/1.9.1/src",
"main": "jquery.min"
}
],
"combine": {
"app": true
}
}
gulpfile.js
var gulp = require('gulp');
var edp = require('gulp-edp');
gulp.src(
[
'src/**/*.js'
'bower_components/**/*.js',
'!bower_components/**/{demo,demo/**}',
'!bower_components/**/{test,test/**}'
]
)
.pipe(edp({
getProcessors: function () {
var moduleProcessor = new this.ModuleCompiler();
return [moduleProcessor];
}
}))
.pipe(gulp.dest('dist'));
See EDP wiki for more features.

Resources