英文:
Unable to load assets in custom .scss file in Angular Electron application
问题
我正在使用electron packager打包我的angular应用程序。然而,在打开打包后的应用程序时,来自我自定义创建的.scss文件的资源引用不会正确加载,而直接从库资产文件夹引用的其他资源会正确加载。
Angular项目包含应用程序和库。它具有以下结构
根目录
--> 项目
--> 桌面
--> 网页
--> 库-1
--> 库-2
库1有一个fonts.scss文件,引用了这些字体:
字体位于library-1/src/assets/fonts
上图中的资产位于资产文件夹中
我试图从fonts.scss文件中加载的字体之一:
@font-face {
font-family: "OhBaby";
src: url("/assets/fonts/OoohBaby-Regular.ttf");
}
我尝试去掉url路径中的/,但然后Web应用程序不工作,开发中的桌面应用程序也不工作
然后我使用以下命令打包应用程序:
electron-packager ./dist/desktop MyAppName --overwrite --asar --platform=win32 --arch=x64 --icon=projects/desktop/src/assets/logo-accent.ico --prune=true --out=dist --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName="MyAppName"
我在组件中如何引用字体文件:
@import "../../../../../Library-1/src/styles/fonts.scss";
在开发中使用electron的桌面应用程序和我的web应用程序中,fonts.scss文件中的字体可以正常工作,但当我打开打包后的桌面应用程序时就不起作用了。
无法正常工作的字体产生的错误?
正确的路径应该包含resources/app.asar/.../在url中
这里是一个正常工作的资产引用:
assets/images/brands/logo-android.svg
这是我的angular.json文件,我在其中输出我的资产:
"assets": [
"projects/desktop/src/favicon.ico",
"projects/desktop/src/assets",
{
"glob": "**/*",
"input": "./projects/library-1/src/assets",
"output": "/assets/"
}
],
"styles": [
"projects/desktop/src/styles.scss"
],
资产被正确复制到dist文件夹中,如下图所示:
dist/desktop/assets中的文件:
我想要一个解决方案,不需要我将字体和其他在自定义.scss文件中引用的资源移动到library1的styles.scss文件中。
我如何告诉打包器或electron使用正确的路径,或者如何配置Angular使用正确的路径。
英文:
I am packaging my angular application using electron packager. However, when opening the packaged application then the assets referenced from my custom created .scss files does not load correctly whilst my other assets (referenced directly from the libraries assets folder) loads correctly.
The angular project contains applications and libraries. It has the following structure
Root
--> Projects
--> Desktop
--> Web
--> Library-1
--> Library-2
Library 1 has a fonts.scss file which REFERENCES the fonts:
fonts are in library-1/src/assets/fonts
The assets is in the assets folder in the above picture
One of the fonts I am trying to load from the fonts.scss file:
@font-face {
font-family: "OhBaby";
src: url("/assets/fonts/OoohBaby-Regular.ttf");
}
I have tried removing the / from the url path, but then the Web app doesn't work and the desktop application in developent doesnt work
Then I package the application with the following command:
electron-packager ./dist/desktop MyAppName --overwrite --asar --platform=win32 --arch=x64 --icon=projects/desktop/src/assets/logo-accent.ico --prune=true --out=dist --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"MyAppName\""
How I reference the fonts file in a component:
@import "../../../../../Library-1/src/styles/fonts.scss";
The desktop appplication with electron in development and my web application the fonts from the fonts.scss file works correctly, but not when I open the packaged desktop application.
The error that is produced for a font that is not working?
The correct path should contain resources/app.asar/.../ in the url
Here is an asset reference that work:
assets/images/brands/logo-android.svg
Here are my angular.json where I ouput my assets:
"assets": [
"projects/desktop/src/favicon.ico",
"projects/desktop/src/assets",
{
"glob": "**/*",
"input": "./projects/library-1/src/assets",
"output": "/assets/"
}
],
"styles": [
"projects/desktop/src/styles.scss"
],
The assets are copied over correctly to the dist folder as seen in this image:
I would like a solution that does not require me to move my fonts and other assets referenced in custom .scss files to the styles.scss file of library1.
How can I tell the packager or electron to use the correct paths or how can I configure Angular to use the correct paths.
答案1
得分: 0
问题与 Electron 如何打包应用程序以及 SCSS 如何解释资源路径有关。SCSS 无法理解 Electron 应用程序的上下文,它只是将 URL 视为字符串处理,并保持不变。当尝试打包应用程序时,这会导致问题,因为路径上下文发生变化,开发服务器的相对路径与生产结构不匹配。
处理这个问题的最佳方法是在 SCSS 文件中使用相对路径而不是绝对路径,并确保字体与 Electron 应用程序正确捆绑。
@font-face {
font-family: "OhBaby";
src: url("./assets/fonts/OoohBaby-Regular.ttf");
}
确保这些字体在构建过程中复制到 dist/desktop 文件夹中。可以通过在 angular.json 文件的 assets 数组中添加一个部分来完成:
"assets": [
"src/favicon.ico",
"src/assets",
{ "glob": "**/*", "input": "projects/Library-1/src/assets/fonts", "output": "/assets/fonts" }
]
确保你的 "Electron 的 BrowserWindow 的 webPreferences 对象" 配置为使用正确的预加载脚本或启用 nodeIntegration:
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: false,
},
});
在 preload.js 文件中,然后可以将所需的 Electron API 暴露给前端。
const { contextBridge } = require('electron')
contextBridge.exposeInMainWorld(
'electron',
{
getAssetsPath: () => {
return process.resourcesPath + '/app.asar/';
}
}
)
在你的 Angular 代码中,然后可以像这样使用这个 Electron API:
const assetsPath = window.electron.getAssetsPath();
只要在 SCSS 中使用相对路径来引用字体文件,并确保它们在构建过程中正确复制,就应该可以正常工作。如果字体没有被复制,你需要找到一种方法将它们包含在构建输出中...
英文:
The issue is related to how Electron packages the application and how SCSS interprets the paths for assets. SCSS doesn't understand the context of the Electron app, it merely processes the url as a string and leaves it as-is. This causes an issue when you try to package the application because the path context changes, the relative paths from the development server do not match up with the production structure.
The best way to handle this problem is to use relative paths in your SCSS file instead of absolute paths, and to ensure that the fonts are being bundled correctly with the Electron app.
@font-face {
font-family: "OhBaby";
src: url("./assets/fonts/OoohBaby-Regular.ttf");
}
Ensure that these fonts are copied to the dist/desktop folder during the build process. This can be done by adding a section to the assets array in the angular.json file:
"assets": [
"src/favicon.ico",
"src/assets",
{ "glob": "**/*", "input": "projects/Library-1/src/assets/fonts", "output": "/assets/fonts" }
]
Make sure your "Electron's BrowserWindow's webPreferences object" is configured to use the correct preload script or to enable nodeIntegration:
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: false,
},
});
In the file preload.js, you can then expose the required Electron API to your front-end.
const { contextBridge } = require('electron')
contextBridge.exposeInMainWorld(
'electron',
{
getAssetsPath: () => {
return process.resourcesPath + '/app.asar/';
}
}
)
In your Angular code, you can then use this Electron API like:
const assetsPath = window.electron.getAssetsPath();
Using relative paths for font files in your SCSS should work as long as those fonts are being copied over correctly during the build process. If they're not being copied, you will need to find a way to include them in your build output...
答案2
得分: 0
不要使用/assets
,只使用assets/
。第一个选项暗示了资产位于根目录,但可能并不在那里。如果你开始调整base-href
命令行参数,这就变得相关了,我怀疑这可能是你问题的原因。
根据你的评论,我怀疑你可能没有正确地将你在angular.json
文件下提到的资产文件进行部署。
确保你的 "architect.build.options" 包括你提到的 scss 文件。有两个相关的部分:
"assets": […],
"styles": […]
如果你在 assets
下包含你的文件,你必须通过相对路径 assets/
引用这些文件。如果你从其他地方链接到文件本身,这很方便。
如果你将它包含在你的 styles
下,Angular 将会将它捆绑、缩小并嵌入到 index.html
下的 <style>
标签中,这将被输出,你不能真正使用 <link>
标签,因为不会产生 .css
文件。
英文:
Don't use /assets
use just assets/
. The first option implies that assets is in the root and it might not be there. That becomes relevant if you start messing with the base-href
command line argument and I suspect it might be the cause of your problems.
Given your comment I suspect that perhaps you're not properly shipping the assets you've mentioned under your angular.json
file.
Make sure that your "architect.build.options" include your scss files you're mentioning. There are two sections that are relevant:
"assets": […],
"styles": […]
If you include your files under assets
you have to reference the files through the relative assets/
paths. This is handy if you're linking to the file itself from other places.
If you include it under your styles
Angular will bundle, minify and inline into a <style>
tag under the index.html
that will be outputted and you cannot really use <link>
tags as there'll be no .css
file produced.
答案3
得分: 0
用^替换在scss或css文件中引用资源时的/。
例如:
"^assets/fonts/OoohBaby-Regular.ttf"
英文:
Replace the / with a ^ when referencing an asset in a scss or css file.
e.g.
"^assets/fonts/OoohBaby-Regular.ttf"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论