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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3062420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:30:24  来源:igfitidea点击:

Django: auto minifying css/js files before release

htmldjangoreleaseminify

提问by dzida

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.

我有以下情况:我想在开发过程中使用未压缩的 js/css 文件(例如调试 js),但在生产中我想自动切换到该文件的缩小版本。

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?

你如何在你的项目中做到这一点?有什么工具可以做到这一点吗?

采纳答案by Pierre-Jean Coudert

Did you try http://code.google.com/p/django-compress/?

您是否尝试过http://code.google.com/p/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 already 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 可用资产管理器的相当完整列表,请参阅http://djangopackages.com/grids/g/asset-managers/...

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

回答by Rick Westera

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

Django-compress 不再维护。尝试使用https://github.com/cyberdelia/django-pipeline代替。

回答by heyman

I've been using webassetsand 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.

我一直在使用网络资产,到目前为止我很满意。我真正喜欢它的是,您仍然可以在模板中定义 CSS 和 JS 文件,而不是在项目配置中。

Documentation can be found at: http://elsdoerfer.name/docs/webassets/

可以在以下位置找到文档:http: //elsdoerfer.name/docs/webassets/

回答by melchtheitroad55

As of the end of 2016, these answers are mostly outdated.

截至 2016 年底,这些答案大多已过时。

Check here for a few options: https://gitlab.com/rosarior/awesome-django#asset-management

在此处查看一些选项:https: //gitlab.com/rosarior/awesome-django#asset-management

At the moment, django-compressoris a good choice, but there are alternatives depending on what you want to do. I believe webpack is becoming popular these days as well.

目前,django-compressor是一个不错的选择,但也有其他选择,具体取决于您想要做什么。我相信 webpack 现在也变得流行起来。

回答by jweyrich

I wrote this Makefileto minify and concatenate my JS and CSS files. It depends on the YUI CompressorJAR. After updating a file, you still have to run makethough. Nevertheless, you can make it run when the server starts and/or reloads, or setup a commit-hook on your SCM.

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

Of course you still need the {% if not debug %}, but it's a small price to pay IMO.

当然,您仍然需要{% if not debug %},但支付 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.

回答by MatthewKremer

Just released an open-source project that watches directories for changes and auto-minifies JS, auto-compiles SASS/SCSS, runs command line operations, etc.

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

Check it out at http://devWatchr.com/

http://devWatchr.com/查看

It runs using pythonand pyinotifyon your system during development.

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