Django:在发布前自动缩小 css/js 文件
我有以下情况:我想在开发期间使用未压缩的 js/css 文件(例如调试 js),但在生产中我想自动切换到该文件的缩小版本。
一些简单的解决方案是放入模板:
<script src="some_js.{% if not debug %}min.{% endif %}js"....
但这需要手动提供此类文件存在并在原始文件更改后进行小型化手动操作。
您如何在您的项目中实现这一目标?有什么工具可以做到这一点吗?
I have following case: I want to use uncompressed js/css files during development (to debug js for example) but on production I want to switch automatically to minified versions of that files.
some simple solution is to put in your template:
<script src="some_js.{% if not debug %}min.{% endif %}js"....
but this require manully providing that such file exist and to do minifaction manullay after original file change.
How do you accomplish this in your projects? Is there any tool for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您是否尝试过 django-compress ?
如果您已经在使用 django-compress,您应该考虑升级到 django-pipeline,这是一个维护良好的分支,具有许多新功能。我鼓励所有使用 django-compress 的人改用 django-pipeline: * django-pipeline 文档
Did you try django-compress ?
If you already are using django-compress, you should have a look at upgrading to django-pipeline, which is a well maintained fork, with a lot of new features. I encourage everyone to who is using django-compress to switch to django-pipeline instead: * django-pipeline documentation
Django-compress 不再维护。请尝试 https://github.com/cyberdelia/django-pipeline 。
Django-compress is no longer being maintained. Try https://github.com/cyberdelia/django-pipeline instead.
我一直在使用 webassets ,到目前为止我非常满意。我真正喜欢它的是,您仍然可以在模板内定义 CSS 和 JS 文件,而不是在项目配置中。
文档可以在以下位置找到:http://elsdoerfer.name/docs/webassets/
I've been using webassets and so far I'm very satisfied. What I really like about it, is that you're still able to define your CSS and JS files inside of your templates, instead of in the project configuration.
Documentation can be found at: http://elsdoerfer.name/docs/webassets/
截至 2016 年底,这些答案大多已经过时。
检查这里的几个选项:
https://gitlab.com/rosarior/awesome-django#asset-management
目前, django-compressor 是一个不错的选择,但还有其他选择取决于你想做什么。我相信 webpack 这些天也变得流行起来。
As of the end of 2016, these answers are mostly outdated.
Check here for a few options:
https://gitlab.com/rosarior/awesome-django#asset-management
At the moment, django-compressor is a good choice, but there are alternatives depending on what you want to do. I believe webpack is becoming popular these days as well.
我编写了 这个 Makefile 来缩小和连接我的 JS 和 CSS 文件。它取决于 YUI 压缩机 JAR。
更新文件后,您仍然需要运行
make
。不过,您可以让它在服务器启动和/或重新加载时运行,或者在 SCM 上设置提交挂钩。当然,您仍然需要
{% if not debug %}
,但在我看来,这是一个很小的代价。显示简单用法:
I wrote this Makefile to minify and concatenate my JS and CSS files. It depends on the YUI Compressor JAR.
After updating a file, you still have to run
make
though. Nevertheless, you can make it run when the server starts and/or reloads, or setup a commit-hook on your SCM.Of course you still need the
{% if not debug %}
, but it's a small price to pay IMO.Showing the simple usage:
刚刚发布了一个开源项目,可以监视目录的更改并自动缩小 JS、自动编译 SASS/SCSS、运行命令行操作等。
查看 http://devWatchr.com/
它在开发过程中在您的系统上使用
python
和pyinotify
运行。Just released an open-source project that watches directories for changes and auto-minifies JS, auto-compiles SASS/SCSS, runs command line operations, etc.
Check it out at http://devWatchr.com/
It runs using
python
andpyinotify
on your system during development.