I'm working on a Python Django package whose front-end components employ a bit of CoffeeScript.
Right now, I have a rather brain-dead external script that takes care of the CoffeeScript compilation. It simply runs a coffee
compile command for every *.coffee file in a src/coffee/ directory and stores the output in src/static/js -- this similar to how python ./setup.py build_ext --inplace
stores a C extension's build files in the development source tree.
That works for now, but it's pretty cheesy -- it forces a flat directory structure, and modifies the files in src/static (which is the opposite of what "static" implies).
I want to be maximally pythonic about things, so I looked into modifying distutils.ccompiler.CCompiler
to run coffee
as a subcommand of the setup.py "build_ext" subcommand -- I was envisioning the ability to do things like this:
% python ./setup.py build_coffee
% python ./setup.py build_coffee --inplace
% python ./setup.py build_ext --inplace # implying 'build_coffee --inplace'
... but I found distutils' compiler API to be way too focussed on C compilation nuances that have no analog in this case e.g. preprocessing, linking, etc. I also looked at Cython's code (specifically at Cython's CCompiler
subclass, which preprocesses .pyx files into .c source) but this looked similarly specialized, and not so appropriate for this case.
Does anyone have a good solution for compiling CoffeeScript with a distutils setup.py script? Or, barring that, a good alternative suggestion?
You can roll this into a custom manage.py command. See the official Django documentation here this way the script will be run everytime the server is run always resulting in a clean build of your js.
You could have a pre-commit hook* that compiles coffescript into javascript.
So everytime you commit a change in the coffescript, the javascript version is updated.
*pre commit hook: the way to do it depends on the VCS you use, and depends on you using a sane VCS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With