且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

电子 - 不允许加载本地资源

更新时间:2022-11-16 23:22:10

经过大量的试验和错误,但我得到了这个工作.这里有很多我不完全理解的东西,而且很多可能是毫无意义或不好的做法,而且它可能会在下一步全部失败,但如果像我一样,你只是想克服第一个驼峰也许我找到的东西会对你有所帮助.

It took a lot of trial and error but I got this working. There is a lot here that I don't totally understand, and much that might be pointless or bad practice and it might all fall down at the very next step but if, like me, you are just trying to get over the first hump then maybe something I found will help you.

我通过解包 应用程序发现了问题electron-builder 生成的 .asar 文件.它没有包含我的 dist 文件夹中的捆绑代码,而是包含所有项目代码(*.ts *.scss 等).问题只是打包函数打包了错误的东西.
将正确的源代码放入最终应用程序的步骤很简单,但天哪,他们没有半途而废!从我在最初的问题中停止的地方开始,我做了以下......

I found the problem by unpacking the app.asar file that electron-builder produces. Instead of containing the bundled code from my dist folder it contained all the project code (*.ts *.scss etc). The problem was just that the packing functions were packing up the wrong stuff.
The steps to get the right source into the final app are simple when you lay them out but my god they didn't half put up a fight! Starting from where I left off in my initial question I did the following...

记住我的项目结构是angular-cli设置的默认结构

电子特定文件
我将 main.js 移至 src 并将其名称更改为 electron-main.js 只是为了避免与已经存在的 main.ts 混淆.我还将它引用的路径更改回 /index.html.
接下来我也在 src中创建了一个新的应用程序级别 package.json> 并给它以下内容:

Electron specific files
I moved the main.js down into src and changed its name to electron-main.js just to stop any confusion with the main.ts that is already in there. I also changed the path it references back to /index.html.
Next I created a new application level package.json also in src and gave it the following content:

 {
  "name": "application-name",
  "description": "CLI app",
  "author": "Me me me",
  "version": "0.0.1",
  "main": "electron-main.js"
}

angular-cli.json
我将 outDir 更改为 build 纯粹是因为电子似乎输出到 dist 默认情况下,我想在这个阶段进行一些分离.然后我将 package.jsonelectron-main.js 添加到 assets 数组中.

angular-cli.json
I changed the outDir to build purely because electron seems to output to dist by default and I wanted some separation at this stage. Then I addded package.json and electron-main.js to the assets array.

Main package.json
我删除了 "main":"main.js",因为这里显然不再需要它.在 scripts 中,我将测试命令更改为 electron build 以将其指向捆绑代码所在的构建文件夹.
最后,我去了 build 字段并添加以下内容:

Main package.json
I removed the "main":"main.js" as it is apparently no longer needed here. In scripts I changed the test command to electron build to point it at the build folder where the bundled code will be.
Finally, I went to the build field and added the following content:

"directories": {
  "buildResources": "build",
  "app": "build"
}

现在看起来很明显.这只是告诉编译器在哪里寻找构成最终应用程序的资产.它默认为工作目录,这是我的问题.

Seems pretty obvious now. This just tells the compiler where to look for the assets that will make up the final app. It was defaulting to the working directory and that was my problem.

使用此设置,我现在可以运行 ng build 将我的项目与 electron-main.js 一起捆绑到 build 文件夹中,并且package.json.
完成后我可以运行 npm run electron 快速启动测试应用或 npm run pack 构建应用可以从 Finder 启动.

Using this setup I can now run ng build to bundle my project into the build folder along with the electron-main.js and package.json.
This done I can run npm run electron to quickly launch a test app or npm run pack to build an app that can be launched from Finder.

轰隆隆.任务完成.我会在十分钟后回到这里,回答我期待的另一个烦人的问题.这些天我一定很喜欢这个开发者:)

Boom. Job done. I'll be back here in ten minutes with another annoying question I expect. Gotta love the dev these days :)