facebook / create-react-app

Set up a modern web app by running one command.
https://create-react-app.dev
MIT License
102.21k stars 26.68k forks source link

Provide a watching build mode that writes to the disk in development #1070

Open andreypopp opened 7 years ago

andreypopp commented 7 years ago

See https://github.com/facebookincubator/create-react-app/issues/1018#issuecomment-261792488 for a discussion which leads to the creation of this issue.

I propose adding a new command (working name build-dev) which watches for changes and rebuild the bundle (effectively, given the current CRA implementation, it should execute webpack --watch), then writes it onto filesystem.

The main motivation is an easier integration path with non-Node.js dev servers: such servers could just serve continuously updating bundle from the filesystem.

The only desired configuration options is an --output, which could probably be specified via a command line argument:

% npm run build-dev -- --output ../static/www

Alternative command name suggestions:

gaearon commented 7 years ago

My concern: the concept of a dev server is already confusing to many people and I've seen countless issues from people trying to find dev server output in the filesystem, adding scripts with hrefs to local files, when they didn't need it.

So I'm worried introducing this feature is useful in some advanced cases but might make everything more confusing in simple cases for people who mistakingly start with build-dev instead because they haven't seen a dev server before when dev server is exactly what they need.

leftdevel commented 7 years ago

(As the OP of #1018) : For me it all boils down to:

@gaearon makes a great point saying that CRA biggest win is saving the developer from configuring the dev server which is the most difficult feature. I'd be happy if at least we could access the dev bundle.js file from the filesystem, I wouldn't care if the dev server is started, I would just ignore it.

My argument is that:

CRA is quite appealing to non Node.js developers. There are many experienced developers in python, php, .net out there, but not all of us/them are that experienced in frontend set ups.

CRA makes it a no-brainer to use react. And to be honest, these days it's way more difficult to set up a React project than an ember or angularjs 1.x project. React API is pretty simple and powerful, but when you introduce React to your organization projects, it's not just React, but all its friends which are hard to master (webpack, babel, presets, linters, etc).

Not sure if you guys have read this blog post yet https://goo.gl/HviDRn, but it's about a company that has a backend in Flask and were evaluating a frontend framework. They talk about how difficult is to keep up with the shifting ecosystem around React, and I'd totally agree on their pain points. At the end they chose Emberjs because of their CLI tools that makes it super easy to integrate.

I think CRA was created with the mindset of helping people that are just starting with React, and playing around with a few simple tutorials, but it has the potential of becoming a profesional tool for people that don't have that much time to spend learning webpack, babel, if to use fetch or superagent, what Promise polyfill to use, etc.

We need something opinionated that just works. Note that I'm totally fine with the already existing features in CRA.

Sorry for the long post. flies away

gaearon commented 7 years ago

Tagging with a milestone so I remember to get back to it.

viankakrisna commented 7 years ago

I think this is the same as https://github.com/facebookincubator/create-react-app/issues/1316 should i just close that?

gaearon commented 7 years ago

Yea, this is the same.

viankakrisna commented 7 years ago

So I'm worried introducing this feature is useful in some advanced cases but might make everything more confusing in simple cases for people who mistakingly start with build-dev instead because they haven't seen a dev server before when dev server is exactly what they need.

Isn't this can be solved with proper documentation and gentle reminder when they use this feature? Would be happy to working on a react-scripts watch PR. I have 'ejected' several project because i need this feature.

gaearon commented 7 years ago

I'm not opposed to adding it. Happy to take a look at a PR.

aeneasr commented 7 years ago

I'm currently using cross-env NODE_ENV=development webpack . --config node_modules/react-scripts/config/webpack.config.dev.js -w to achieve that. However, it requires adding webpack-cli to the node_modules.

The only problem is that appending the hash to the bundle's name bundle.[hash].js makes the entrypoint unpredictable which is why I'm still ejecting the scripts. Any way to resolve that?

dkaplan-pivotal commented 7 years ago

I want to add an additional story to add context to why I'd like this feature: We built our app with server side rendering and waited until we had a feature that would require enough js/ajax to justify a single page app. We got to that point and wanted to replace that one page with a react frontend. The plan would be that react would gradually expand to the rest of the app (if needed, maybe it'd never be needed).

But CRA appears to assume that in development mode, your frontend is a different app from your backend. That's not necessarily true if you want to bring react to an application that already exists. (Even if you go with the dual-app idea, you have to deal with CORS. I think you could argue that CORS complexity outweighs the output file complexity.)

So our workaround is to always build to production and copy the files into our server so they can be rendered. But the minification and source map generation adds an extra 10-15 seconds when we do that every time and we can't use a watch. When you want frequent, fast feedback loops, those 10-15 seconds add up to considerable lost time.

Let me know if I'm doing something wrong here. Maybe there's an obvious workaround here.

viankakrisna commented 7 years ago

@dkaplan-pivotal I have the exact use case! There is a proof of concept here https://github.com/facebookincubator/create-react-app/pull/1616 but i think #1994 & #1588 is a better solution (minimized risk of deploying development script + reload on file change). Good to see that #1994 & #1588 is tagged for 0.9.6.

dkaplan-pivotal commented 7 years ago

Thanks for your reply. @viankakrisna I don't see how #1994 & #1588 add up to a development mode where you can have a single page of your app use react.

viankakrisna commented 7 years ago

1994 and #1588 will allow you to include the development bundle to any of your local sites with live reloading. I've tested it by referencing the bundle on my local wordpress site and it reloads on file change. (haven't tested css hot reload because i'm using styled-components)

ltoshea commented 7 years ago

I'd just like to echo all the above, when loading resources from an external file it's a huge pain for the names to change every time you make a small change and rebuild.

int128 commented 6 years ago

I wrote a script for the watching build mode. https://gist.github.com/int128/e0cdec598c5b3db728ff35758abdbafd

nickretallack commented 6 years ago

Yes please. Having webpack write the files to disk is how I've been developing JS apps for ages. It allows me to make my development and production environments look a lot more similar, routing everything through nginx and treating the resulting files as static.

I don't understand the appeal of running webpack as a server. Is there any advantage to doing that?

ilyanoskov commented 6 years ago

I agree, this would be a very useful feature. Or at least somebody could write up a tutorial on how ti disable webpack-dev-server and have watch mode instead. I have an identical use-case with @nickretallack

jasonblanchard commented 6 years ago

@nickretallack @ilyanoskov - @int128's watch script mostly works for me. I forked it and slightly changed how the webpack config is mutated to avoid a bunch of temp files getting saved in the build directory from HotModuleReplacementPlugin: https://gist.github.com/jasonblanchard/ae0d2e304a647cd847c0b4493c2353d4

This is NOT heavily tested, I've been using it in a side project, so YMMV.

viankakrisna commented 6 years ago

What's the difference between serving the actual file in the disk vs memory like what webpack dev server does?

In the end, you need a script tag in your HTML with a reference to your bundle and run your development build script. It doesn't matter whether the bundle comes from disk vs ram.

I've been advocating a watching mode for a while, but now I can't live without dev server (error overlay and auto reloading worth the hassle of making the dev server works for me)

@gaearon @Timer I think it's worth revisiting #1994 and #1588 or at least a section explaining the benefit of dev server and think about the integration story with backends.

Similar to the deployment and API integration guide, I think CRA can have a backend integration guide? I can write one for WordPress.

jasonblanchard commented 6 years ago

I'd second @nickretallack that for me, it's dev/production parity. If I need to do some work and then construct index.html server-side (to inject state onto the page, set a cookie, server render, etc), I want to always serve index.html from my server, not sometimes from webpack-dev-server.

What's the difference between serving the actual file in the disk vs memory like what webpack dev server does?

@viankakrisna I don't think that much matters. As you said, I suspect serving the bundle from memory would work just fine in my use case. However, the last time I tried to do that with the default output from npm run build (admittedly, a few version ago), I got all sorts of errors in the console, I think because the bundle was expecting to be run in the context of webpack-dev-server and/or the generated index.html file and was polling something that didn't exist where it should have.

viankakrisna commented 6 years ago

I'd second @nickretallack that for me, it's dev/production parity. If I need to do some work and then construct index.html server-side (to inject state onto the page, set a cookie, server render, etc), I want to always serve index.html from my server, not sometimes from webpack-dev-server.

That's also why I open #1616 in the first place, but seeing efforts with #1994 and #1588 I think i can use dev server for my use case.

So instead of advocating a watching build mode, It's more beneficial to make dev server more flexible for this kind of use case.

lukewlms commented 6 years ago

I don't believe anyone has posted this solution: this doesn't write to disk, but in Debug mode you can proxy requests to the development server, which gives you rapid reload after changes.

https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/

radi-ratlh commented 6 years ago

This will be useful for chrome extension devlopment.

spsoto commented 6 years ago

I support the request. I know it's possible to forward requests to the server from webpack, but sometimes it's required to have it the other way.

For example, having a server API that servers the index.html, similar to a production setup. In our stack we have in separate folders the backend and the frontend and will be more useful to (1) start webpack in watch mode, (2) create a symlink to the generated files and start the web server in the other environment. Moreover, this helps having a stack a similar as possible to production, which is always a good practice.

Of course this is completely doable ejecting, but that's not the idea, since CRA is awesome.

praveenav commented 6 years ago

My project uses Spring for the backend. This feature will be helpful.

justinkahrs commented 6 years ago

@praveenav do you have a work around you're using currently? We also are using spring on the backend and got this solution working with some minor tweaks to the build output dir

https://gist.github.com/justinkahrs/3c0ab8b7499dd7d6a19d78298e748064

praveenav commented 6 years ago

Thank you @justinkahrs Currently, we do not have a workaround. We are at the beginning stage. I was searching for a feature and bumped on this issue. Thank you very much for sharing your script. It looks good.

Steve06 commented 6 years ago

Has anybody figured out a workaround (ejecting or not) for chrome extensions using the dev server, i.e., avoiding long production rebuilds? It's not clear to me how to even launch a chrome extension from the dev server, let alone making necessary adjustments.

I can see that https://github.com/jhen0409/react-chrome-extension-boilerplate has implemented a solution where only a few files are provided in a dev folder for the chrome extension to be launched and then any changes to the code are immediately rendered through the server. However, the dependencies of this project are outdated (React 15 etc) and neither is it based on CRA nor does it use the same dev server. It only shows that a solution is possible.

thromera commented 6 years ago

We're currently struggling a lot with @ephommaphavanh, and we came to the same conclusion as you @Steve06. The reloading they are doing looks great, but requires some work to adapt with the updated dependencies. Still looking (it's been 4 hours) for an answer, we're now playing directly with a webpack.config.js.

GiantappMan commented 6 years ago

I am a newbie at JS&React. I try developing chrome extension from create-react-app. I have the same problem as Steve06 said. It's very unfriendly to beginner

Nargonath commented 6 years ago

I released an npm package that allow to write dev build to the disk based on some discussions I've seen in this issue or others: https://www.npmjs.com/package/cra-build-watch. It works for my use case but I tried to make it general enough by accepting some arguments to please other people use case.

It is just meant as a temporary workaround until this moves forward and we can have the feature built-in.

Feel free to give me feedbacks on the github repo if there are stuff to improve or even if it works for you. :wink:

Legym commented 6 years ago

I really hope this turns into a feature.

kirill-konshin commented 6 years ago

Could be very useful for Electron apps that use actual FS.

tecnobrat commented 6 years ago

I've just been using this: nodemon --exec 'react-scripts build' --watch src --watch public

hopewise commented 6 years ago

@tecnobrat @Nargonath How about refresh the browser when a change happen? ex: current opened tab in chrome

devxpy commented 6 years ago

This would super dope for people looking into integration with non-nodejs framweork, like django.

Currently working on a project that let's you do this, but has to use browserify..

EDIT: looks like i can use @Nargonath's thing instead of browserify, Thanks!

nickmccurdy commented 6 years ago

@devxpy You should be able to integrate with any server in development, including Django, by running both react-scripts start and the server while proxying API requests.

devxpy commented 6 years ago

I wanted to integrate them right in the templates. So you could use it without an API at all.

devxpy commented 6 years ago

Yes, I could write a server to handle this for me, but that feels like overkill.

devxpy commented 6 years ago

image For example, Half of that page is just normal django template code. And half of it is react code

I want to make stuff like this one-liner 'testpage' is a directory containing a react project

image

mythz commented 5 years ago

+1 for this to be an officially supported feature. IMO watched builds provides the optimal development for non node.js server apps, proxying with the dev server is too laggy with the more ajax requests an app has the less enjoyable it becomes where often ajax responses get handled out of order.

Nargonath commented 5 years ago

@hopewise Did you manage to get the page reload as you wanted? Wondering if I should get a look at it.

graysonhicks commented 5 years ago

Trying to accomplish this for integrating with templates served with Drupal. Any update with this with v2?

radi-ratlh commented 5 years ago

This will be really useful when developing chrome extensions, instead of using prod bundle and getting incomplete error messages we can use the development bundle and debug the issue.

Nargonath commented 5 years ago

@radi-ratlh in the meantime perhaps the npm package I wrote could be of any help.

radi-ratlh commented 5 years ago

@Nargonath Thank you, I will give it a try

andreisoare commented 5 years ago

If create-react-app supports "Changing the title tag", "Generating Dynamic Tags on the Server", or "Injecting Data from the Server into the Page" for production, it should provide a way to support these features for development.

If it's part of your core for prod, it should be for dev as well and not defer to 3rd party plugins. Without the feature requested here I don't see an easy way to accomplish this.

Is this still not planned for the near future?

dharmendra-sehgal commented 5 years ago

image For example, Half of that page is just normal django template code. And half of it is react code

I want to make stuff like this one-liner 'testpage' is a directory containing a react project

image

Did you find any solutions?

devxpy commented 5 years ago

@dharmendra-sehgal check out my project react-pages.

Feature requests and bug reports welcome!

Friss commented 5 years ago

webpack-dev-middleware has an option writeToDisk and it can be enabled via webpack-dev-server (>=3.1.10 required)

With my PR above to get us to the required version of webpack-dev-server we could then add in some environment var(s) to enable the saving of files to disk. This could be 2 vars, 1 to enable and another to decide what directory to use. By default it uses dist. Or just one var to declare where to save the files and that enables the option to be set.

Nargonath commented 5 years ago

@Friss Wow that'd be really neat! Hope it goes through. 👍