Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Coffeescript with Flask in Production

I have a Flask app that I'm running in production. Right now it has a big ugly js file that I'd like to break out and rewrite in something like Coffeescript. I was considering something like Flask-Cake to simplify the CoffeeScript compilation. However, I don't know how something like that would work for production. I should probably have a script that compiles the coffeescript files before deploying, right? I've never worked on a system with this particular layout -- uncompiled server-side but compiled client-side. What's the standard procedure here?

like image 282
NickAldwin Avatar asked Mar 22 '23 08:03

NickAldwin


1 Answers

You are probably looking for Flask-Assets.

Example from the website:

from flask import Flask
from flask.ext.assets import Environment, Bundle

app = Flask(__name__)
assets = Environment(app)

js = Bundle('jquery.js', 'base.js', 'widgets.js',
            filters='jsmin', output='gen/packed.js')
assets.register('js_all', js)

This would automatically concatenate jquery.js, base.js and widgets.js in your static folder, pipe them through jsmin and save the result in static/gen/packed.js.

This compilation is by default always happening when one of the source files changes. Watching the files in production is kinda expensive in production (and would require a coffeescript compiler to be installed on the server!), so there is a configuration value to disable the monitoring.

Another plugin that is more lightweight, but in my experience also less powerful is Flask-Makestatic.

like image 78
Markus Unterwaditzer Avatar answered Apr 02 '23 07:04

Markus Unterwaditzer