我有一个electron react应用程序(使用create-react-app构建),它在开发模式下运行良好,但当我使用electron-packager发布它时,.css和.js文件都出现错误“无法加载资源:net::错误文件未找到”。
查看开发者工具网络选项卡,我可以看到它正在寻找 *file:///static/js/main.83d4505c.js * 和 *file:///static/css/main.00ef8ad8.css * 中的文件,而文件/文件夹结构为:
- 构建
- 静态
- 日本
- main.83d4505c.js
- CSS
- main.00ef8ad8.css
- 应用程序-达尔文-x64
- 应用程序(可执行应用程序)
- 公众
- 来源
- ...
我在主窗口中使用以下代码:
mainWindow.loadURL(
isDev
? 'http://localhost:3000'
: `file://${path.join(__dirname, '../build/index.html')}`
);
我已经尝试按照https://github.com/electron/electron/issues/1769上的建议将"homepage": "./",
添加到包文件中,但没有帮助。
Package.json
{
"name": "app",
"version": "0.1.0",
"private": true,
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"electron-dev": "concurrently \"BROWSER=none yarn start\" \"wait-on http://localhost:3000 && electron .\""
},
"homepage": "./",
"main": "public/electron.js",
"directories": {
"buildResources": "build",
"app": "build"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"dependencies": {
"@emotion/core": "^11.0.0",
"@emotion/react": "^11.9.3",
"@emotion/styled": "^11.9.3",
"@mui/icons-material": "^5.8.4",
"@mui/material": "^5.8.5",
"@mui/styled-engine-sc": "^5.8.0",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^13.0.0",
"@testing-library/user-event": "^13.2.1",
"chokidar": "^3.5.3",
"electron-is-dev": "^2.0.0",
"electron-reload": "^2.0.0-alpha.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"styled-components": "^5.3.5",
"web-vitals": "^2.1.0"
},
"devDependencies": {
"concurrently": "^7.2.2",
"electron": "^19.0.4",
"electron-builder": "^23.0.3",
"electron-packager": "^15.5.1",
"prettier": "1.17.0",
"wait-on": "^6.0.1"
}
}
1条答案
按热度按时间ff29svar1#
你需要使用HashRouter,我有同样的问题,但我修复了它.
enter image description here
enter image description here