简体   繁体   English

使用一些生成的密钥将js / css文件最小化为资产的方法

[英]Methods to minify js/css files into assets with some generated key

I am have been building web applications for several years, exploring and looking for ways to improve the web app performance. 我从事Web应用程序的开发已经有好几年了,探索并寻找改善Web应用程序性能的方法。 I would like to ask if there is any method to minify css/js when we launch the web application automatically? 我想问一下,当我们自动启动Web应用程序时,是否有任何方法可以最小化CSS / JS? I do not use any php frameworks, rather i built my own. 我不使用任何PHP框架,而是建立了自己的PHP框架。

I was exploring one web application and i saw they have this 我正在探索一个Web应用程序,我看到他们有这个

<link href="/assets/application-76c372b1409e29d226c9566022d5546f.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/assets/application-cb571d9ff5185e712000e3378494e4ee.js" type="text/javascript"></script>

I seen similar sites that has js and css files stored in /assets/ etc with a name starting with application- too. 我看到过类似的站点,这些站点的js和css文件存储在/ assets /等文件中,名称也以application-开头。 I would like to know if anyone of you know what mechanism they are using? 我想知道你们是否有人知道他们使用的是什么机制? I am sure they compressed their files the same way. 我确定他们以相同的方式压缩文件。

Any idea what framework/programming langugage they are using? 知道他们使用什么框架/编程语言吗?

advice appreciated. 建议表示赞赏。 thanks 谢谢

Often this functionality is built into the web framework that you are using, or an external series of programs like SASS, Compass, Uglify, and Grunt do the work. 通常,此功能内置于您使用的Web框架中,或者由一系列外部程序(如SASS,Compass,Uglify和Grunt)来完成工作。 There are MANY ways to do this, but I'm going to explain the principle and just one way of doing it. 有很多方法可以做到这一点,但是我将解释其原理,只是其中的一种方法。

Generally, the principle is that during your deployment process, you concatenate all of the files together, take a hash (like an MD5 sum) of the content, and create a file name based on that hash. 通常,原则是在部署过程中,将所有文件连接在一起,对内容进行哈希处理(例如MD5总和),然后基于该哈希创建文件名。

So for example, if you had: 因此,例如,如果您有:

  • source/assets/reset.css 源/资产/reset.css
  • source/assets/main.css 源/资产/main.css
  • source/assets/fonts.css 来源/资产/fonts.css

You could clear out the output assets directory, which is separate from the source assets directory so you don't overwrite anything, then concatenate these 3 files together, take the MD5 hash, and then create a file named application-$hashvalue.css, and write the contents to it. 您可以清除输出资产目录,该目录与源资产目录是分开的,因此您不会覆盖任何内容,然后将这三个文件连接在一起,进行MD5哈希处理,然后创建一个名为application- $ hashvalue.css的文件,并将内容写入其中。

Concatenating is helpful, but you could also run it through a minifier such as CSSMin, JSMin, Google's Closure Compiler, and many others. 串联是有用的,但是您也可以通过一个缩小器(例如CSSMin,JSMin,Google的Closure Compiler等)运行它。

Now that's all if you want to do it by yourself, by hand. 现在,这就是您想手动完成的全部操作。 You're of course not the first person to solve this problem, and there are a variety of tools to do this for you. 当然,您不是解决此问题的第一人,并且有多种工具可以帮助您解决此问题。

An general example set of tools to do this for you: 为您执行此操作的一组通用示例工具:

Grunt , as a task runner. 咕unt声 ,作为任务执行者。 Sort of a supervisor of everything else. 有点像其他一切的主管。 Uglify , a minifier for JS SASS + Compass to manage and minify your CSS grunt-contrib-uglify : a plugin to connect Grunt to Uglify grunt-contrib-compass : a plugin to connect Grunt to Compass Uglify ,一个用于JS SASS + Compass的管理程序,用于管理和最小化CSS grunt-contrib-uglify :一个将Grunt连接到Uglify的插件grunt-contrib-compass :一个将Grunt连接到Compass的插件

So then you just run your grunt file and it will compile everything out for you. 因此,您只需运行grunt文件,它将为您编译所有内容。 You could do this during deployments or have a directory watcher running locally. 您可以在部署期间执行此操作,也可以在本地运行目录观察器。

PHP-specific asset management: 特定于PHP的资产管理:

https://github.com/kriswallsmith/assetic https://github.com/kriswallsmith/assetic

Unfortunately that's the only one I know of, as I don't use PHP much anymore. 不幸的是,这是我所知道的唯一一个,因为我不再使用PHP。

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

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