cookiecutter-django icon indicating copy to clipboard operation
cookiecutter-django copied to clipboard

React support through Webpack

Open anchpop opened this issue 6 years ago • 17 comments

Description

It'd be really nice to have React as a built-in option, or have documentation on how to hack it in. I see this was suggested in #466, and seems to have been merged, but I saw no option for it when running cookiecutter (it also does not appear in cookiecutter.json). I've seen ccokiecutter-webpack but it hasn't been updated in 2 years which makes me nervous.

Rationale

Lots of people prefer Webpack over Gulp. Also React is pretty good, but it's but a pain to use with Gulp. Even a working webpack setup, with no built-in react support, would be nice.

anchpop avatar Jun 20 '18 22:06 anchpop

Lots of people prefer Webpack over Gulp.

True.

Also React is pretty good, but it's but a pain to use with Gulp

True.

Even a working webpack setup, with no built-in react support, would be nice.

True.

Yet...

webyneter avatar Jun 20 '18 22:06 webyneter

have documentation on how to hack it in

This is a workable alternative, to my mind, write an external documentation somewhere and link it from our docs. Once again, we, the contributors and the community as a whole have yet to find a one-size-fits-all step-by-step third-party Cookiecutter Django-React integration guide -- why not to pioneer, @anchpop? :)

webyneter avatar Jun 20 '18 22:06 webyneter

Just use create-web-app and build it in a new directory or even a separate repo. That's the best way, as it means you aren't dependent on us maintainers to configure Webpack for you, instead you get the best webpack people in the world doing it for you.

The same technique applies to VueJS. Just use Vue CLI instead of having a tight integration with this project.

pydanny avatar Jun 21 '18 00:06 pydanny

Oops, this should be documented somewhere as @webyneter suggested.

pydanny avatar Jun 21 '18 00:06 pydanny

Good recent post: https://fractalideas.com/blog/making-react-and-django-play-well-together-single-page-app-model/

browniebroke avatar Jun 21 '18 10:06 browniebroke

https://github.com/anchpop/annotator-tots/blob/master/tutorial/howToSwitchToWebpack.md I got something working, this is how I did it. I'm no React expert so this is very likely not the best way. Pretty painless, though. Could someone take a look to see if they spot any errors?

I also explain how to set up react to work with django (which is non-obvious)

anchpop avatar Jun 23 '18 17:06 anchpop

Update: I now use this, which I wrote and use in production. It is very similar to pydanny's cookiecutter-django.

anchpop avatar Aug 11 '18 14:08 anchpop

This is awesome, @anchpop. Any chance you could do it for VueJS? 😜

pydanny avatar Aug 11 '18 15:08 pydanny

Thank you @anchpop. I will definitely have a look at your setup. I been working to create this with the option of creating a "Hybrid app" (Django page as a React Component). SPAs with code splitting should make the "hybrid app" unnecessary for performance seekers.

Afrowave avatar Aug 11 '18 21:08 Afrowave

@pydanny I will once I eventually learn Vue.js 😉

anchpop avatar Aug 14 '18 19:08 anchpop

@Afrowave I agree. I hadn't seen code splitting done well by any React Django app, this setup should theoretically make it pretty simple (don't forget it also does SSR).

anchpop avatar Aug 14 '18 19:08 anchpop

There is one project for similar purposes - what separates frontend and backend https://github.com/hersonls/frango

I took it as base, and did what is more suitable for our team.

Right now I have separate directory frontend directory for frontend where everything happens

image

This model works very good with webpack and gulp and also provides really good separation, so we may provide access to templates / static to frontend team, without providing access to source code of backend.

Maybe you are interested to add this schema to your django template.

77cc33 avatar Oct 26 '18 22:10 77cc33

I actually forked this repo and added an option for react using create-react-app. Try it like this:

cookiecutter https://github.com/chopdgd/cookiecutter-django-reactjs

genomics-geek avatar Mar 19 '19 00:03 genomics-geek

@genomics-geek any plans to setup Docker with your react version?

caleybrock avatar Apr 17 '19 00:04 caleybrock

@caleybrock - not in the short term. I maintain that cookiecutter for fun and for some of the projects I work on at my day job. It is definitely something I would like to add support for eventually though.

genomics-geek avatar Apr 18 '19 12:04 genomics-geek

Just use create-web-app and build it in a new directory or even a separate repo. That's the best way, as it means you aren't dependent on us maintainers to configure Webpack for you, instead you get the best webpack people in the world doing it for you.

The same technique applies to VueJS. Just use Vue CLI instead of having a tight integration with this project.

Well but how about if we want to use VueJS or React, not as SPAs? Just on one particular page of our Django app.

We cannot do that with separate projects.

lapinvert avatar Jul 02 '20 11:07 lapinvert

Well but how about if we want to use VueJS or React, not as SPAs? Just on one particular page of our Django app.

If you're looking for an example of this, I just published a cookiecutter-vue-django (forked from this repo) that supports VueJS as MPA, allowing mingled usage of Django templates and Vue components.

ilikerobots avatar Jul 17 '20 13:07 ilikerobots