I try and not resort to StackOverflow too often, but this is driving me nuts. I have a create-react-app which I am trying to wrap into the electron framework. I discovered a tutorial online which basically has you create everything from scratch with a barebones webpack config setup, and it works, but only when starting up the webpack dev server first before starting the electron application. This is redundant when you want to package the application, as the package.json start script only runs one command, which is "electron .". So how do I go about including the static production files of my React app into Electron? I have tried it several time before without any success, and the closest I came was pointing (within the main.js entry file for electron) to my static react index.html file. Only problem was, because it was a production build, everything was minified. For the resources within this file to load correctly, I had to manually remove all the first "/" from any src url (you can see how quickly that would become quite cumbersome with larger apps).
So I guess my actual question is:
How do I "serve" the static files of my react application so electron loads it correctly?
(Do also take note that I ejected the application in order to make use of css modules)
PS: Link to the react-electron tutorial - https://medium.com/#Agro/developing-desktop-applications-with-electron-and-react-40d117d97564
By the logic of the tutorial you have to put the static files into the /public folder assets will be loaded relativ from the html document, but this will result in the same issue with paths where you had to manually remove all the first "/" from any src url. The URL's need to be correct when build.
That beeing said i think the tutorial encourages bad practice for electron development, and i think that is part of the issue you have.
You would never start electron from the node_modules path. You do this by electron ./path/to/electronscriptsfolder this will load a index.js/main.js in this folder containing the app definitions.
Webpack is the wrong approche for electron because, you don't need bundling and when packing electron, webpack tends to generate issues with native packages and paths (As you described yourself). You would use electron-compile or a gulp pipeline to do it by your electron specific logic.
There is no babel configuration given even though they are super important for electron. Basically you need two bable configs, one for electron running on a specific node version and one for the chromium running on a specific implementation.
Here an example for electron 1.8.1 which uses Chrome 59 and Node.js 8.2.1
Renderprocess
{
"presets": [
["env", {
"targets": {
"chrome": "59"
}
}]
]
}
Mainprocess
{
"presets": [
["env", {
"targets": {
"node": "8.2.1"
}
}]
]
}
Related
I'm trying to deploy a React TypeScript project to Vercel, building with Vite. However, Vercel doesn't install devDependencies during build time like Heroku does, so it doesn't know what "vite build" is (same for tsc).
I've followed the Vite guide for Vercel deployment.
It feels very odd to include Vite and TypeScript as a normal dependency, so that it'll be included in the bundle. I know it's possible to configure Vite to exclude certain dependencies on your bundle, but is this really the way to do it?
Have a read of these two tweets:
https://twitter.com/dan_abramov/status/1098234219506085889
https://twitter.com/dan_abramov/status/1098234004954857472
He's talking about CRA, but the same principle applies here to vite.
Because vite build produces static HTML/CSS/JS and not a node app, the difference between devDependencies and dependencies is moot.
Strictly speaking, your package.json lists the required dependencies for the code that builds your static bundle to build your app.
It's similar to the way that a factory requires paint, electricity, metal, etc to produce a car, but once the car is finished and has left the factory it's self-contained and has all the paint and metal it needs. Your codebase is (strictly speaking) a factory, and your package.json lists the requirements for the factory, not the car itself.
TLDR; perfectly fine to add it to dependencies, it won't end up in the final bundle.
EDIT: if you're wondering why the distinction between dependencies and devDependencies exists, it's because it's designed to separate what packages are required during ongoing production operation of a node app (eg, for an app that's serving HTTP requests, a core networking module) vs packages that are only needed locally (eg, a code formatter).
When the "production" app is an ephemeral script that runs for a few seconds inside a build pipeline to generate some static HTML/CSS/JS, the difference between dependencies and devDependencies doesn't mean much. It's a common misconception that it has any kind of effect on what ends up inside the static bundle.
I have been trying to come up with an elegant solution to share code between two projects that are closely related to each other.
I have a React web app which simply includes all of the client code and I have an Express app which serves the React web app, but it also acts as an API. Because I use Typescript for both projects, I want to reuse some types. To accomplish that, I created a shared project.
My current folder structures is as follows:
web-app
shared
server
Now I want to link the web-app and server projects with my shared project, but there are some restrictions:
This shared folder should not be uploaded to npm.
I have to be able to still deploy everything with Heroku (which rules out npm link I believe).
I would prefer not to eject my React project.
I am not sure whether my current structure of projects allows the functionality I need, so feel free to also suggest other folder structures.
You can use yarn workspaces for that kind of purpose,
There's a minimal working boilerplate I have created exactly for that kind of configuration:
https://github.com/rok-tel/ts-express-react
take a look at ./packages/shared folder which is consumed both by server (express) and client (react)
Consider use Nx - https://nx.dev/
This is an awesome framework to manage monorepos.
Use their React application boilerplate for the client side project
Use their express project boilerplate to create the API
Use their shared code package boilerplate to create shared code package
Docs for creating the shared code package:
https://nx.dev/l/r/tutorial/07-share-code
If you are using create-react-app to create the web-app then you can easily use NODE_PATH environment variable to do absolute imports without creating node modules.
Create a .env file at the root level (same level as package.json of web-app directory)
Set an environment variable, NODE_PATH to shared/ (NODE_PATH=shared/)
Now instead of doing something like
import { editUser } from ‘../../../shared/actions’;
you can use
import { editUser } from ‘shared/actions’;
Fixing ESLint
Install eslint-plugin-import to prevent eslint from throwing import errors. And update your .eslintrc file as
{
"settings": {
"import/resolver": {
"node": {
"moduleDirectory": ["node_modules", "shared/"]
}
}
}
}
Fixing Flow
Add the following content to your .flowconfig file
[options]
module.system.node.resolve_dirname=node_modules
module.system.node.resolve_dirname=shared
Say the webpack config is as follows
{
entry: path.join(__dirname, 'src', 'index.js'),
output: {
path: path.join(__dirname, 'build'),
filename: 'bundle.js'
},
Now the build from webpack is
and the build from react-scripts build ( static contains css, js and media in seperate folders )
Question: Is there any specific advantage of webpack over react-scripts build? ( including but not limited to performance )
NOTE: package.json is edited to achieve this.
Webpack is a general purpose bundler, with applications beyond React. Before create-react-app, the web was full of examples of setting up a brand new React project which uses webpack as the bundler. It is extremely flexible and can handle things including and beyond what a React application would need. It works for Angular, Vue, NodeJS and even Web Assembly.
But it used to take a while to setup. You will need to understand its working and configure it so that you can transpile your React+ES6 code into plan-vanilla JS. You would need to decide the output structure you like and configure webpack for it. And then also add hot-module-reloading and code-splitting support yourself. During this, you will also need to add other plugins required by Webpack to support all the above :).
This naturally caused some fatigue with folks who were starting with React.
So facebook created cra which internally uses webpack, pre-configured to include all the nice tools to take care of these basics and help you focus on the React part of your code. It hides webpack from you as much as possible, otherwise the build process may break if the configuration is changed by the user.
With that aside, the structural conventions which cra uses should not be having any performance impact over a bare-bones webpack setup. It's just a convention.
Your question should then be, when would I use create-react-app and when would I use Webpack?
As a beginner you might want to stick to cra as you focus on your react app. Eventually there would come a time where what you want to do is not supported by the webpack configuration cra is managing under the hood. A very common example is if you want to write a component library to reuse in other apps. This cannot be done by cra (it's about the whole app :)). You can then switch over to webpack and start learning it.
react-scripts hides all of the webpack configs behind the scenes. The advantage of this is it makes it cleaner and since create-react-app is regularly updated, its easy to stay up to date with React, Webpack, and Babel. The community automatically fixes the issues for you.
In terms of performance, should be the same regardless with react-scripts or with webpack.
Advantages of running only webpack:
Full control of your environment
Can do custom things like server-side rendering easily (still possible with create-react-app
Knowledge of webpack as a skill
Disadvantages of webpack only
Full in charge updating and maintenance of webpack (some webpack versions are not backward compatible or future compatible)
Can be intimidating and can be a headache if you are trying to learn to react quickly.
If you want to customize create-react-app, here is some info
https://auth0.com/blog/how-to-configure-create-react-app/
Here is server-side rendering with create-react-app
https://hackernoon.com/server-side-rendering-with-create-react-app-1faf5a9d1eff
TLDR: Use create-react-app / react-scripts if you want to go to 0-100 as quick as possible for whatever reason
Use just webpack if you enjoy messing around under the hood
I come from a background in Angular, but I wanted to start learning React. I want to make a React front-end for a nodejs Express web service.
In Angular, I could simply build the source code into a static folder with a standard index.html, and it can be deployed on any web server. With React however, every tutorial I see is about running React in its own server using create-react-app and npm start.
I know you can also just use script tags to import the React framework in a static page, but then you can't use JSX syntax, and I would like to. Is it possible to simply build a React project into a static folder with an index.html that can be deployed on any web server?
Yep, you can do what you're describing. If you want to use JSX syntax, you'll need Babel, which transpiles it into standard JavaScript.
You can avoid the complexities of setting it up by using create-react-app to create the app, then running npm build instead of npm start. This will compile everything into a build directory, complete with index.html.
CRA uses its server for development purposes. You don't need CRA for using React of course. But, as your app getting bigger and bigger you will need some more extra tools. For example you want to see your code instantly without refreshing your browser. Here comes the dev server and hot reloading.
CRA uses Webpack behind the scenes. It is a great tool for bundling (obviously) all your files (including css), minifying, uglifying, optimizing your code etc.
For simple code or testing purposes using one file and attaching React and Babel to your file could be enough but for real apps you will need more. Either you will use something like Webpack on your own or you will use CRA and let it do all the extra stuff for you.
I am a beginner with reactjs so I start to create a site, I do all installations, I installed Webpack to generate automatically the bundle.js all it works well and the file bundle.js is well generated but nothing that posster on the index page (page index is empty) and there are also no errors on the console. Somewhat help me please
It can be a bit messy to get up & running if you're not experienced with these technologies and how module bundling in Webpack works.
This is exactly why Facebook created the "create-react-app" solution so that you can get running without any build config and focus on creating the app that you're after instead. You can use it and "eject" to a custom setup anytime and see how they've done it:
create-react-app by Facebook
Here is a guide that you can follow along to create a simple config build:
Setup a React Environment Using webpack and Babel
You can also check out some of the other solutions that the community offers, here is a list of 143 React starter projects:
Find your perfect React starter project