Django:在发布前自动缩小 css/js 文件

发布于 2024-09-06 08:59:22 字数 264 浏览 5 评论 0原文

我有以下情况:我想在开发期间使用未压缩的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

破晓 2024-09-13 08:59:22

您是否尝试过 django-compress

参见http://djangopackages.com/grids/g/asset-managers/< /a> 获取 Django 可用资源管理器的相当完整的列表...


如果您已经在使用 django-compress,您应该考虑升级到 django-pipeline,这是一个维护良好的分支,具有许多新功能。我鼓励所有使用 django-compress 的人改用 django-pipeline: * django-pipeline 文档

Did you try django-compress ?

See http://djangopackages.com/grids/g/asset-managers/ for a fairly complete list of available asset managers for Django...

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

写给空气的情书 2024-09-13 08:59:22

Django-compress 不再维护。请尝试 https://github.com/cyberdelia/django-pipeline

Django-compress is no longer being maintained. Try https://github.com/cyberdelia/django-pipeline instead.

ぽ尐不点ル 2024-09-13 08:59:22

我一直在使用 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/

捂风挽笑 2024-09-13 08:59:22

截至 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.

软甜啾 2024-09-13 08:59:22

我编写了 这个 Makefile 来缩小和连接我的 JS 和 CSS 文件。它取决于 YUI 压缩机 JAR。
更新文件后,您仍然需要运行 make。不过,您可以让它在服务器启动和/或重新加载时运行,或者在 SCM 上设置提交挂钩。

当然,您仍然需要 {% if not debug %},但在我看来,这是一个很小的代价。

显示简单用法:

$ make
[css] static/css/first.css
[css] static/css/second.css
[css] static/css/third.css
[css] static/css/and_so_on.css
[tag] @import url("static/css/all.css");
[js] static/js/first.js
[js] static/js/second.js
[js] static/js/third.js
[js] static/js/and_so_on.js
[tag] <script type="text/javascript" src="static/js/all.js"></script>
Done.

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:

$ make
[css] static/css/first.css
[css] static/css/second.css
[css] static/css/third.css
[css] static/css/and_so_on.css
[tag] @import url("static/css/all.css");
[js] static/js/first.js
[js] static/js/second.js
[js] static/js/third.js
[js] static/js/and_so_on.js
[tag] <script type="text/javascript" src="static/js/all.js"></script>
Done.
我家小可爱 2024-09-13 08:59:22

刚刚发布了一个开源项目,可以监视目录的更改并自动缩小 JS、自动编译 SASS/SCSS、运行命令行操作等。

查看 http://devWatchr.com/

它在开发过程中在您的系统上使用 pythonpyinotify 运行。

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 and pyinotify on your system during development.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文