Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Example #7

Closed
rizrmd opened this issue Feb 16, 2020 · 18 comments
Closed

React Example #7

rizrmd opened this issue Feb 16, 2020 · 18 comments

Comments

@rizrmd
Copy link

rizrmd commented Feb 16, 2020

We need an easy to follow example.
I would suggest a Create-React-App project built using esbuild as an example, this will onboard most of us to esbuild.

@immanuelfodor
Copy link

This. When I saw that incredible benchmark in the readme, I just said, wow, we seriously need it for React build. If you make an example app happen, I think we'll consider migrating to your hobby project as a company 😃

@evanw
Copy link
Owner

evanw commented Feb 16, 2020

I've never used create-react-app before but from what I understand, it's basically a customized webpack config. It's not possible to use esbuild to replace a webpack config generated with create-react-app because it includes a lot of things that esbuild doesn't implement. For example, it appears to support processing CSS in .css/sass/scss files as well as inlining .bmp/gif/jpg/png files as base64 URLs. It also seems to enable JSX syntax in .js files while esbuild requires you to use the .jsx extension.

I'm not trying to have esbuild replace webpack. If you need the complexity of create-react-app/webpack, then you're not going to be able to use esbuild. I'm only intending for esbuild to be appropriate for a certain sweet spot of use cases. Currently this is just bundling JavaScript/JSX files that use CommonJS/ES6 modules. In the future, it will hopefully also include bundling TypeScript files and maybe CSS files. If I get to implementing all of that, it may be possible to manually port an app that uses create-react-app to esbuild, but it may take significant effort depending on how many custom webpack features it uses.

@immanuelfodor
Copy link

If you would try to replace webpack, and you succeed to "reimplement" it in Go, you'd get famous, go to conferences, etc. This is a real opportunity here, you're one step ahead of everybody, and it fills a need in the market. But fair enough, it's your project, it seems you have clear vision about what you want to do with it. I just don't get if you have the talent to accomplish this already, why not make it something truly earthshaking. No matter what, I wish you all the luck you can master, great job what you did here, the most amazing build optimization I've seen in some time!

@arangates
Copy link

@immanuelfodor maybe Google must do that

@immanuelfodor
Copy link

Sooner or later somebody will definitely implement it. 100x increase in build time is well worth the effort. There was another issue for Svelte use case (#8), so I feel even two forks of this repo could coexist: an esbuild-react and an esbuild-svelte variant.

@chowey
Copy link

chowey commented Feb 17, 2020

You guys might be missing the point. Webpack has 10,472 commits and 585 contributors over the past 7 years. Re-writing that in Go from the ground up is not trivial.

The same goes for any JS build tool. You would have to re-write it in Go from the ground up.

I'm already impressed that @evanw is planning Typescript support.

Now, something like transpiling JSX is much simpler. The React guys explain it well. JSX converts directly to function calls in a straightforward way.

I think a React example is a good idea. It doesn't have to be Create-React-App. It can just showcase how to run esbuild with JSX.

@immanuelfodor
Copy link

Sounds good, thumbs up for showing us the way! :)

@evanw
Copy link
Owner

evanw commented Feb 17, 2020

Just using React with esbuild should be pretty simple:

  • Make sure all JSX syntax is put in .jsx files instead of .js files because esbuild uses the file extension to determine what syntax to parse.

  • If you're using TypeScript, run tsc first to convert .tsx files into either .jsx or .js files.

  • If you're using esbuild to bundle React yourself instead of including it with a <script> tag in your HTML, you'll need to pass '--define:process.env.NODE_ENV="development"' or '--define:process.env.NODE_ENV="production"' to esbuild on the command line (see "Process is not defined" error while building react app #12 for details).

If you're using Preact instead of React, you'll also need to pass --jsx-factory=preact.h --jsx-fragment=preact.Fragment to esbuild on the command line.

@evanw
Copy link
Owner

evanw commented Feb 17, 2020

For example, if you have a file called example.jsx with the following contents:

import * as React from 'react'
import * as ReactDOM from 'react-dom'

ReactDOM.render(
  <h1>Hello, world!</h1>,
  document.getElementById('root')
);

Use this for a development build:

esbuild example.jsx --bundle '--define:process.env.NODE_ENV="development"' --outfile=out.js

Use this for a production build:

esbuild example.jsx --bundle '--define:process.env.NODE_ENV="production"' --minify --outfile=out.js

@evanw evanw closed this as completed in e989f9d Feb 18, 2020
@rw3iss
Copy link

rw3iss commented Oct 30, 2020

FYI, for anyone else wanting to replace webpack for a faster building, you don't really have to. Just use the esbuild-loader package, and it will work. The livereload isn't any faster, but it will still build the JS/TS faster, ie:

       {
            test: /\.(j|t)sx?$/,
            include: [PATHS.src], 
            exclude: [PATHS.modules],
            use: [
                {
                    loader: 'esbuild-loader',
                    options: {
                        loader: 'tsx', // Or 'ts' if you don't need tsx
                        target: 'esnext'
                    }
                }
            ]
        },

@IlmariKu
Copy link

I'm creating a ESbuild-version of nano-react-app to this repository

https://github.com/IlmariKu/esbuild-react-app

@zaydek
Copy link

zaydek commented Dec 18, 2020

I’m also messing around with a kind of CRA using esbuild: https://github.com/zaydek/react-ssg. I’m also experimenting with SSG so it’s sort of a CSR / SSG hybrid (SSG for prerendered pages, CRA for everything else).

@josteph
Copy link

josteph commented Dec 30, 2020

Following @rw3iss suggestion, I was able to make CRA based project run blazing fast with esbuild 👍.
So far there is no problem yet, I will update this comment if I found something.

@zaydek
Copy link

zaydek commented Dec 30, 2020

@josteph Can you share which repo?

@bard
Copy link

bard commented Dec 31, 2020

@josteph did you eject or did you use something like craco?

@josteph
Copy link

josteph commented Dec 31, 2020

@zaydek @bard I had to eject the app due to other circumstances. I only replaced the js loader & terser to esbuild loader though.

@cliffordfajardo
Copy link

I wanted to share with folks that you may not likely need to eject from create-react-app to use features like ESbuild.
I highly recommend folks checkout

I was working on a codebase for a few months and its been a PITA maintaining a custom build setup. I like receiving free bug fixes & performance improvements from the upstream create-react-app team and the open source community 💙

@tangdw
Copy link

tangdw commented Apr 26, 2021

I wanted to share with folks that you may not likely need to eject from to use features like ESbuild.
I highly recommend folks checkoutcreate-react-app

I was working on a codebase for a few months and its been a PITA maintaining a custom build setup. I like receiving free bug fixes & performance improvements from the upstream team and the open source community 💙create-react-app

https://github.com/pradel/create-react-app-esbuild

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests