简体   繁体   English

是否有用于打包和缩小多个CSS和JS文件到每个文件的库?

[英]Are there libraries for packing and minifying multiple CSS and JS files into one file each?

According to O'Reilly's High Performance Web Sites (pages 15-16), it's highly recommended to make as few HTTP requests as is possible for high-performance. 根据O'Reilly的高性能网站 (第15-16页),强烈建议尽可能少地提出高性能的HTTP请求。 Thus, is there a library for combining multiple JS files into one file, and a library to do this for CSS as well? 那么,是否有一个用于将多个JS文件合并到一个文件中的库,以及一个用于CSS的库?

For JavaScript, I have at least 4 different libraries being loaded right now, jQuery 1.6.x, a few jQuery plugins, and one application JavaScript file for application logic. 对于JavaScript,我至少有4个不同的库,jQuery 1.6.x,一些jQuery插件和一个应用程序JavaScript文件用于应用程序逻辑。 I also have 2 CSS files, Bootstrap and my application's CSS file. 我还有2个CSS文件,Bootstrap和我的应用程序的CSS文件。

Are there tools to facilitate this? 有没有工具来促进这个? It'd be nice to have a tool I could use from Python or from Bash (I'm on Linux) to "compile" and minify the multiple CSS stylesheets. 有一个我可以从Python或Bash(我在Linux上)使用的工具来“编译”和缩小多个CSS样式表是很好的。

(If this is possible as a Django addon, even better [ie manage.py packify static/bootstrap.min.css static/application.css ... ]) (如果这可以作为Django插件,甚至更好[即manage.py packify static/bootstrap.min.css static/application.css ... ])

What you are looking for is a css and javascript pipeline. 你要找的是一个css和javascript管道。 Its becoming a standard for frameworks to provide this kind of tools. 它成为提供此类工具的框架的标准。 For instance, Rails 3.1 has its own asset pipeline built-in. 例如,Rails 3.1内置了自己的资产管道。

Not only it will merge your css and javascripts into a single pack, but it will also compress them for even further performance boost. 它不仅会将你的css和javascripts合并到一个包中,而且还会压缩它们以进一步提升性能。

Fortunately, django also has its own plugin for that 幸运的是,django也有自己的插件

https://github.com/cyberdelia/django-pipeline https://github.com/cyberdelia/django-pipeline

Of course there is such a tool! 当然有这样的工具! It's called webassets . 它被称为webassets And it integrates fine with Django too. 它也与Django很好地集成在一起

webassets features webassets功能

  • minification (compression) of CSS and JS CSS和JS的缩小(压缩)
  • LESS to CSS 不太喜欢CSS
  • SASS to CSS SASS到CSS
  • compass 罗盘

etc. 等等

There are various methods. 有各种方法。 A great plugin is already mentioned by JavierIEH. JavierIEH已经提到了一个很棒的插件。 But you can easily build one yourself using PHP or any other server language. 但您可以使用PHP或任何其他服务器语言轻松构建一个。

But you will gain even more if you load JQuery and its plugins from Google Libraries API . 但是,如果从Google Libraries API加载JQuery及其插件,您将获得更多收益。 It offers the minified version of many libraries. 它提供了许多库的缩小版本。

The advantage is that it won't use your server. 优点是它不会使用您的服务器。 Browsers will only make a limited number of simultaneous connections to the same server, so it allows your users to load JQuery and your own javascript at the same time. 浏览器只能与同一服务器建立有限数量的同时连接,因此它允许用户同时加载JQuery和您自己的javascript。

And because many sites use the libraries from Google, these files may already exist in the browser cache of your visitor, speeding up the loading even more. 由于许多网站使用Google提供的库,因此这些文件可能已存在于访问者的浏览器缓存中,从而加快了加载速度。

Check this python script. 检查这个python脚本。 It is not built on a framework and can be easily integrated. 它不是基于框架构建的,可以轻松集成。

http://github.com/hkasera/minify http://github.com/hkasera/minify

It minifies js as well as css files too. 它也缩小了js和css文件。 It stores detailed log files illustrating the error and warnings too. 它存储详细的日志文件,说明错误和警告。 Hope it may help! 希望它可能有所帮助!

If you can live with a PHP tool, check out CSS-JS-Booster which packs a few more tricks than simple minification. 如果您可以使用PHP工具,请查看CSS-JS-Booster ,它包含了比简单缩小更多的技巧。 For example embedding small images referenced in your CSS into the CSS (data-uri/mhtml), etc. 例如,将CSS中引用的小图像嵌入到CSS(data-uri / mhtml)等中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM