Re: Re: How to serve minified React with Cherrypy?

Cameron Simpson <[email protected]> Mon, 27 May 2019 09:31:46 +1000
Newsgroups gmane.comp.python.cherrypy
Message-ID <[email protected]>
On 26May2019 06:13, Edvin Beqari <[email protected]> wrote:
>Thank you Cameron. I went down the rabbit hole of webpack and browsify but
>I just could not get the config right.

I'm surprised. It took me some hours (switched node package managers at 
least once to get to yarn, again courtesy of the various JS setup 
tutorials rabbit hole, ugh). But...

>Instead, I I built the client with the default create-react-app
>configuration (chunks the javascript and creates a template) and I simply
>pointed the server to the build directory.

I started with create-react-app, since I was new to ReactJS. Gahhhh!!!

It makes an obscenely complex tree with your programme source embedded 
deep within it. A nightmare for both navigation and version control 
inclusion/exclusion.

After wasting an hour on it I tossed it and backed off to the bare 
minimum: a node_modules tree, a python source tree and a webapp tree 
with (presently, and possibly permanently) just a single file in it.

And then a Makefile to run browserify on that JS file to make the static 
one in the python app static files directory.

Looking at your code below...

>class Server(object):
>
>@cherrypy.expose
>def index(self):
>return open('../cococlient/build/index.html')
>
>def cors():
>cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
>
>if __name__ == '__main__':
>conf = {
>'/': {
>'tools.staticdir.root': os.path.abspath(os.getcwd())
>},
>'/catalog': {
>'tools.CORS.on': True,
>'tools.response_headers.on': True
>},
>'/static': {
>'tools.staticdir.on': True,
>'tools.staticdir.dir': '../cococlient/build/static'
>}
>}
>
>server = Server()
>server.catalog = Catalog()
>cherrypy.tools.CORS = cherrypy.Tool('before_handler', cors)
>
>cherrypy.quickstart(server, '/', conf)

I suspect all you'd need to do is get browserify or webpack to write out 
your app JS into the static directory you configure above.

(And because it is just one JS file from the same site as the Python app 
there's no CORS stuff.)

I am running browserify in the same directory that has my node_modules 
subdirectory; I think that was all that was needed for it to find 
everything.

Of course one has to fetch whatever modules are required, but that is 
just "yarn install", eg (from my shell history) "yarn install --dev 
@babel/core". Since yarn maintains a "yarn.lock" file with the module 
install state in it, provided you revision control that (commit after 
adding/updating stuff) then the yarn install in a clean checkout is just 
"yarn install" as it gets everything from the yarn.lock).

Cheers,
Cameron Simpson <[email protected]>

-- 
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/20190526233146.GA25407%40cskk.homeip.net.
For more options, visit https://groups.google.com/d/optout.