使用静态文件创建 electron create-react-app 发行版

Create an electron create-react-app distribution with static files

我想要的

我想创建一个读取 json 数据文件的桌面应用程序,然后使用该数据在屏幕上呈现内容。 json数据文件需要打包到应用中

我做了什么

关注一篇文章如何使用 create-react-app 并使其在 electron 中工作

https://medium.freecodecamp.org/building-an-electron-application-with-create-react-app-97945861647c

我卡在什么地方

到目前为止我所做的一切都适用于开发环境。我想进行分发,目前使用创建安装程序的 electron-builder,因此我可以分发应用程序

但是我的数据文件似乎没有打包,或者它们可能是......老实说我不知道​​,但是当 运行 安装后电子应用程序时无法访问

代码

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

const electron = window.require('electron');
const fs = electron.remote.require('fs');

class App extends Component {

    constructor(props) {
        super(props);
        this.state = {files: []}
    }

    componentDidMount() {
        fs.readdir('./data', (err, files) => {
            this.setState({files: files});
        });
    }

    renderFiles = () => {
        return this.state.files.map((file, index) => {
            return (<p key={index}>{file}</p>);
        })
    }

    render() {
        return (
          <div className="App">
            <header className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
              <h1 className="App-title">Welcome to React</h1>
            </header>
            <p className="App-intro">
              To get started, edit <code>src/App.js</code> and save to reload.
            </p>
            {this.renderFiles()}
          </div>
        );
    }
}

export default App;

package.json

{
  "name": "react-electron-poc",
  "version": "0.1.0",
  "private": true,
  "description": "Electron App Poc",
  "author": "Celludriel",
  "devDependencies": {
    "electron": "^1.7.9",
    "foreman": "^2.0.0",
    "react-scripts": "0.8.5",
    "electron-builder": "^20.24.3"
  },
  "dependencies": {
    "react": "^16.1.1",
    "react-dom": "^16.1.1"
  },
  "homepage": "./",
  "main": "src/electron-starter.js",
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "electron": "electron .",
    "dev": "nf start -p 3000",
    "pack": "build --dir",
    "dist": "npm run build && build",
    "postinstall": "install-app-deps"
  },
  "build": {
    "appId": "com.electron.electron-with-create-react-app",
    "win": {
      "icon": "https://cdn2.iconfinder.com/data/icons/designer-skills/128/react-256.png"
    },
    "directories": {
      "buildResources": "public"
    },
    "files": ["**/*", "dist/**/*"],
    "extends": null
  }
}

目录结构

    Directory: C:\Workspaces\react-electron-poc


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       21/07/2018     10:51                .idea
d-----       21/07/2018      9:34                build
d-----       20/07/2018     19:33                data
d-----       20/07/2018     20:22                dist
d-----       20/07/2018     19:12                node_modules
d-----       20/07/2018     17:22                public
d-----       21/07/2018     10:10                src
-a----       20/07/2018     17:22            285 .gitignore
-a----       20/07/2018     19:12         367115 package-lock.json
-a----       20/07/2018     20:21           1058 package.json
-a----       20/07/2018     18:10             56 Procfile
-a----       20/07/2018     17:22         119451 README.md

需要帮助

谁能教我如何在这种设置中打包静态内容?有可能吗,为什么这一切都这么难!!!!!!

我是一名 java 开发人员,涉猎 C#,这两种语言从未让我头疼过……只是为了阅读打包在 JAR 或 DIST 文件夹中的文件!!!!!!! !!!

好吧,看来这个文件夹毕竟是打包在asar文件里的。但是,您需要从应用程序环境变量中访问相关资产。我设法让它与此一起工作。

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

const electron = window.require('electron');
const fs = electron.remote.require('fs');
const app = electron.remote.app;

class App extends Component {

    constructor(props) {
        super(props);
        this.state = {files: []}
    }

    componentDidMount() {
        debugger;
        const appPath = app.getAppPath();
        console.log(appPath);
        const path = app.getPath('userData');
        console.log(path);
        fs.readdir(appPath + '/data', (err, files) => {
            debugger;
            this.setState({files: files});
        });
    }

    renderFiles = () => {
        return this.state.files.map((file, index) => {
            return (<p key={index}>{file}</p>);
        })
    }

    render() {
        return (
          <div className="App">
            <header className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
              <h1 className="App-title">Welcome to React</h1>
            </header>
            <p className="App-intro">
              To get started, edit <code>src/App.js</code> and save to reload.
            </p>
            {this.renderFiles()}
          </div>
        );
    }
}

export default App;

请注意 app.getAppPath(); 它有效,但我越来越相信我宁愿使用 C# 或 [=18= 等成熟技术] 然后继续尝试学习 React 或任何其他网络技术。如果您必须花费 80% 的开发时间来尝试配置您的应用程序然后开发有意义的软件......这是不值得的!