Compiling Coffeescript with Flask in Production
Asked Answered
P

2

5

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?

Plumose answered 21/9, 2013 at 21:23 Comment(0)
M
8

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.

Mongrelize answered 22/9, 2013 at 8:12 Comment(0)
O
0

Take a look at DukPy! It's a simple javascript interpreter for Python and can compile CoffeeScript, TypeScript, BabelJS and JSX. Usage is very simple:

import dukpy

dukpy.coffee_compile("CoffeeScript goes here!")

DukPy is the successor to the Python-CoffeeScript package witch is no longer maintained.

Oina answered 28/2, 2022 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.