Create-react-app: How to use react-scripts in combination with electron?

Created on 10 Dec 2017  Â·  6Comments  Â·  Source: facebook/create-react-app

I try to combine a create-react-app started with react-scripts with electron. Is it possible to setup my electron project to run from localhost in development mode? And how to keep node functionality like fs and __dirname alive through webpack build process?

question

All 6 comments

if you want your renderer process to use actual node or electron APIs, you'll need something like this

Closing as answered, thanks guys!

FWI https://github.com/cdaringe/electron-renderer-react-scripts-target/issues/3
Now it can run both in browser and electron.

For two package.json architecture (one for js, one for native modules), you should be able to ignore src/node_modules.

Here is my updated config-rewired.js

// @flow
const path = require('path');

const ignorePath = function (exclude = [], config) {
  const rule = config.module.rules[0];
  if (!rule) {
    console.log('js related rule not found');
    return config;
  }
  rule.exclude = exclude.concat(rule.exclude || []);
  return config;
};

module.exports = function override(config, env) {
  // Make it run in electron renderer process
  // If we want electron start, we will set cross-env BROWSER=none
  if (process.env.BROWSER === 'none') {
    delete config.node;
    config.target = 'electron-renderer';
  }

  config = ignorePath([path.resolve(__dirname, 'src/node_modules'), path.resolve(__dirname, 'src/~')], config);

  if (env === 'production') {
    console.log('âš¡ Production build with Optimization.');
  }

  return config;
};

Btw, if you need to use native modules, I had to include them using remote like so:

const odbc = require( "electron" ).remote.require( "odbc" );
Was this page helpful?
0 / 5 - 0 ratings

Related issues

stopachka picture stopachka  Â·  3Comments

DaveLindberg picture DaveLindberg  Â·  3Comments

Evan-GK picture Evan-GK  Â·  3Comments

alleroux picture alleroux  Â·  3Comments

onelson picture onelson  Â·  3Comments