最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Electron Forge with react? - Stack Overflow

programmeradmin7浏览0评论

Is there any simple way I can setup an app with Electron-Forge and React? I am usin the webpack template but don't know what to do to get jsx to work. I have the react stuff in renderer.js

Is there any simple way I can setup an app with Electron-Forge and React? I am usin the webpack template but don't know what to do to get jsx to work. I have the react stuff in renderer.js

Share Improve this question edited Jun 20, 2020 at 23:43 tadman 212k23 gold badges235 silver badges265 bronze badges asked Jun 20, 2020 at 17:57 I'veGotRootI'veGotRoot 3412 silver badges8 bronze badges 3
  • Does this help ? ankitbko.github.io/2019/08/… – Harmandeep Singh Kalsi Commented Jun 20, 2020 at 17:59
  • 1 Well, I didn't wan't to use typescript and I figured it out. – I'veGotRoot Commented Jun 20, 2020 at 23:25
  • This guide covers the topic well and thoroughly and is confirmed working as of July 29th, 2024: sitepoint./electron-forge-react-build-secure-desktop-app – Max Hudson Commented Jul 29, 2024 at 18:22
Add a ment  | 

2 Answers 2

Reset to default 16

I figured it out,

    yarn create electron-app test --template=webpack
    cd test

Then I installed babel with:

    yarn add @babel/core babel-loader @babel/preset-env @babel/preset-react --d

and react with:

    yarn add react react-dom

Created a .babelrc in project root with the following code:

    {"presets": ["@babel/preset-env", "@babel/preset-react"]}

and added the following to webpack.rules.js:

    {
    test: /\.(js|jsx)$/,
    exclude: /node_modules/,
    use: {
      loader: "babel-loader"
    }
  }

changed renderer.js to renderer.jsx and changed stuff in package.json from this:

    "@electron-forge/plugin-webpack",
      {
        "mainConfig": "./webpack.main.config.js",
        "renderer": {
          "config": "./webpack.renderer.config.js",
          "entryPoints": [
            {
              "html": "./src/index.html",
              "js": "./src/renderer.js",
              "name": "main_window"
            }
          ]
        }
      }

to this:

    "@electron-forge/plugin-webpack",
      {
        "mainConfig": "./webpack.main.config.js",
        "renderer": {
          "config": "./webpack.renderer.config.js",
          "entryPoints": [
            {
              "html": "./src/index.html",
              "js": "./src/renderer.jsx",
              "name": "main_window"
            }
          ]
        }
      }

and finally replaced renderer.jsx with this:

    import './index.css';
    import React from 'react';
    import ReactDOM from 'react-dom';
    console.log('Loaded React.');
    ReactDOM.render(<div>Test.</div>, document.getElementById('root'));

and replaced index.html with this:

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title>Hello World!</title>
    
      </head>
      <body>
        <div id="root"></div>
      </body>
    </html>

1.Create Webpack template using

yarn create electron-app my-new-app --template=webpack && cd my-new-app

2.Install dependencies

yarn add --dev @babel/core @babel/preset-react babel-loader

3.Add the following to webpack.rules.js

 {
    test: /\.jsx?$/,
    use: {
      loader: 'babel-loader',
      options: {
        exclude: /node_modules/,
        presets: ['@babel/preset-react']
      }
    }
  },
  resolve: {
    extensions: [".js", ".jsx"]
  },

4.Add React and React-dom

yarn add react react-dom

For more details refer electron-forge documentation

发布评论

评论列表(0)

  1. 暂无评论