如何在React + Electron应用中重定向目标不佳的构建文件?

inn6fuwd  于 2023-01-28  发布在  Electron
关注(0)|答案(2)|浏览(155)

我正在尝试用react & electron构建一个桌面应用,我可以用npm start build构建这个项目,但是当我运行可执行文件时,我得到了一个白屏的节点应用,因为这个应用找不到它需要的资源。

这些错误是:

Failed to load resource: net::ERR_FILE_NOT_FOUND /F:/static/css/main.056419ee.chunk.css:1   
Failed to load resource: net::ERR_FILE_NOT_FOUND 2.eba76f4b.chunk.js:1  
Failed to load resource: net::ERR_FILE_NOT_FOUND main.175818bb.chunk.js:1

F:是正确的驱动器,但我不认为Electron应该在驱动器根目录下查找static目录。我不知道是什么原因导致的,也不知道为什么会有一个前导/。此外,我不知道它为什么要在不同的目录下查找其他两个块文件。所有三个文件都在./build/static/js/中。

  • 这是我的第一个reactjs应用程序,也是我的第一个nodejs应用程序之一,所以我不知道如何调试它,我不是很清楚所有的构建步骤,因为我使用了一些样板文件和教程,没有解释任何东西。
  • 我最初用于入门的命令来自create-react-appp,具体来说是npx create-react-app creative-ontology-editor
  • 当我用npm startnpm run electron-dev运行它时,React部分工作正常。从那里我试图用this tutorialthis package example等东西让电子工作。
  • 我正在考虑将该项目公开,如果这将有助于解决它。

源代码

一个月八个月

下面是内置电子应用程序显示的html:(注:我美化了,但把缩小的js留在了一行中,这样就可以很容易地看到它的其余部分)

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <link rel="shortcut icon" href="/favicon.ico"/>
        <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"/>
        <meta name="theme-color" content="#000000"/>
        <link rel="manifest" href="/manifest.json"/>
        <title>Creative Ontology Editor</title>
        <link href="/static/css/main.056419ee.chunk.css" rel="stylesheet">
    </head>
    <body>
        <noscript>You need to enable JavaScript to run this app.</noscript>
        <div id="root"></div>
        <script>!function(l){function e(e){for(var r,t,n=e[0],o=e[1],u=e[2],f=0,i=[];f<n.length;f++)t=n[f],p[t]&&i.push(p[t][0]),p[t]=0;for(r in o)Object.prototype.hasOwnProperty.call(o,r)&&(l[r]=o[r]);for(s&&s(e);i.length;)i.shift()();return c.push.apply(c,u||[]),a()}function a(){for(var e,r=0;r<c.length;r++){for(var t=c[r],n=!0,o=1;o<t.length;o++){var u=t[o];0!==p[u]&&(n=!1)}n&&(c.splice(r--,1),e=f(f.s=t[0]))}return e}var t={},p={1:0},c=[];function f(e){if(t[e])return t[e].exports;var r=t[e]={i:e,l:!1,exports:{}};return l[e].call(r.exports,r,r.exports,f),r.l=!0,r.exports}f.m=l,f.c=t,f.d=function(e,r,t){f.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},f.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},f.t=function(r,e){if(1&e&&(r=f(r)),8&e)return r;if(4&e&&"object"==typeof r&&r&&r.__esModule)return r;var t=Object.create(null);if(f.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:r}),2&e&&"string"!=typeof r)for(var n in r)f.d(t,n,function(e){return r[e]}.bind(null,n));return t},f.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return f.d(r,"a",r),r},f.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},f.p="/";var r=window.webpackJsonp=window.webpackJsonp||[],n=r.push.bind(r);r.push=e,r=r.slice();for(var o=0;o<r.length;o++)e(r[o]);var s=n;a()}([])</script>
        <script src="/static/js/2.eba76f4b.chunk.js"></script>
        <script src="/static/js/main.175818bb.chunk.js"></script>
    </body>
</html>

一米九一

这是我的项目的package.json。我知道我可能把dev和normal依赖项都弄混了,但我怀疑这是问题的一部分。

{
  "name": "creative-ontology-editor",
  "version": "0.1.0",
  "author": "Joe Bush",
  "description": "An applied ontology organization tool",
  "private": true,
  "main": "public/electron.js",
  "dependencies": {
    "concurrently": "^4.1.0",
    "cross-env": "^5.2.0",
    "electron-is-dev": "^1.0.1",
    "react": "^16.8.4",
    "react-dom": "^16.8.4",
    "react-scripts": "2.1.8",
    "typescript": "^3.4.1",
    "wait-on": "^3.2.0"
  },
  "scripts": {
    "react-build": "react-scripts build",
    "react-test": "react-scripts test",
    "react-eject": "react-scripts eject",
    "react-start": "react-scripts start",
    "electron-build": "electron-builder",
    "release": "yarn react-build && electron-builder --publish=always",
    "build": "yarn react-build && yarn electron-build",
    "electron-dev": "concurrently \"cross-env BROWSER=none yarn react-start\" \"wait-on http://localhost:3000 && electron .\"",
    "dist": "build"
  },
  "build": {
    "appId": "creative-ontology-editor",
    "files": [
      "build/**/*",
      "node_modules/**/*"
    ],
    "directories": {
      "buildResources": "public"
    },
    "linux": {
      "target": [
        "AppImage",
        "deb"
      ]
    },
    "win": {
      "target": "NSIS",
      "icon": "build/icon.ico"
    }
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "electron": "^4.1.3",
    "electron-builder": "^20.39.0"
  }
}

缩写的目录树如下所示

📂creative-ontology-editor
|-- 📂build
|   |-- 📂static
|   |   |-- 📂csss
|   |   +-- 📂js
|   |       |-- 2.eba76f4b.chunk.js
|   |       |-- 2.eba76f4b.chunk.js.map
|   |       |-- main.175818bb.chunk.js
|   |       |-- main.175818bb.chunk.js.map
|   |       |-- runtime~main.a8a9905a.js
|   |       |-- runtime~main.a8a9905a.js.map
|   |
|   |-- asset-manifest.json
|   |-- electron.js
|   |-- index.html
|   |-- manifest.json
|   |-- precache-manifest.825 ... .js
|   +-- service-worker.js
|
|-- 📂dist
|   |-- 📂win-unpacked
|   |-- builder-effective-config.yaml
|   |-- creative-ontology-editor Setup 0.1.0.exe
|   |-- creative-ontology-editor Setup 0.1.0.exe.blockmap
|
|-- 📂node_modules
|-- 📂public
|   |-- electron.js
|   |-- index.html
|   +-- manifest.json
|
|-- 📂src
|   |-- 📂Components
|   |-- App.css
|   |-- App.js
|   +-- index.js
|
+-- package.json

一米十一

{
  "main.css": "/static/css/main.056419ee.chunk.css",
  "main.js": "/static/js/main.175818bb.chunk.js",
  "main.js.map": "/static/js/main.175818bb.chunk.js.map",
  "runtime~main.js": "/static/js/runtime~main.a8a9905a.js",
  "runtime~main.js.map": "/static/js/runtime~main.a8a9905a.js.map",
  "static/js/2.eba76f4b.chunk.js": "/static/js/2.eba76f4b.chunk.js",
  "static/js/2.eba76f4b.chunk.js.map": "/static/js/2.eba76f4b.chunk.js.map",
  "index.html": "/index.html",
  "precache-manifest.82521987a6df63b594987c0f56a4cbb9.js": "/precache-manifest.82521987a6df63b594987c0f56a4cbb9.js",
  "service-worker.js": "/service-worker.js",
  "static/css/main.056419ee.chunk.css.map": "/static/css/main.056419ee.chunk.css.map"
}

./build/manifest.json

{
  "short_name": "COE",
  "name": "Creative Ontology Editor",
  "icons": [
    {
      "src": "favicon.ico",
      "sizes": "64x64 32x32 24x24 16x16",
      "type": "image/x-icon"
    }
  ],
  "start_url": ".",
  "display": "standalone",
  "theme_color": "#000000",
  "background_color": "#ffffff"
}
iswrvxsc

iswrvxsc1#

Kitze's article开始,我的package.json中少了一行:

"homepage": "./"

他甚至解释说:
最后一件事:
在package.json中设置"homepage",否则打包的应用程序将找不到. js和. css文件。

amrnrhlw

amrnrhlw2#

例如,如果要在PUBLIC文件夹中查找静态文件,请在静态文件前面加上process.env.PUBLIC_URL

<link rel="stylesheet" href='/assets/css/style.css'/>

<link rel="stylesheet" href={process.env.PUBLIC_URL+ '/assets/css/style.css'} />

相关问题