在Heroku上组合和缩小JS / CSS的最佳方法

时间:2021-08-08 03:44:28

First of all, according to this answer, the :cache => true option on stylesheet_link_tag and javascript_include_tag doesn't work on Heroku. Is this true? I've found :cache => true to work occasionally, but not always (weird!)

首先,根据这个答案,stylesheet_link_tag上的:cache => true选项和javascript_include_tag在Heroku上不起作用。这是真的?我发现:cache => true偶尔会工作,但并不总是(很奇怪!)

Also, what's the best solution here? Ideally it would seamlessly combine and minify all CSS / JS. Heroku Asset Packager claims to do this -- are there better options?

另外,这里最好的解决方案是什么?理想情况下,它可以无缝地组合和缩小所有CSS / JS。 Heroku Asset Packager声称这样做 - 有更好的选择吗?

8 个解决方案

#1


23  

I'm using Jammit on Heroku. Works Great. You can locally build your assets and check in to heroku. use

我在Heroku上使用Jammit。效果很好。您可以在本地构建您的资产并登记到heroku。使用

jammit --force

the current version 0.5.1 has issues working on heroku but you can install the fixed version from git://github.com/documentcloud/jammit.git

当前版本0.5.1在heroku上有问题,但您可以从git://github.com/documentcloud/jammit.git安装修复版本

If you are using Rails 3, specify the below in your bundler Gemfile:

如果您使用的是Rails 3,请在捆绑器Gemfile中指定以下内容:

gem "jammit", :git => "git://github.com/documentcloud/jammit.git"

For Rails 2.*

对于Rails 2. *

config.gem "jammit", :source => "git://github.com/documentcloud/jammit.git"

Good Luck.

祝你好运。

#2


8  

I've found that adding a git pre–commit hook which compiles and packs assets, then adds them to the current commit comes in handy in this case.

我发现在这种情况下添加一个git预提交钩子来编译和打包资产,然后将它们添加到当前提交就派上用场了。

Mine using Jammit looks something like this (in .git/hooks/pre-commit):

我使用Jammit看起来像这样(在.git / hooks / pre-commit中):

jammit
rake barista:brew
git add public/assets/*
git add public/javascripts/*

Like this all your assets will be packed for you and you don't have to worry anymore about it.

像这样,您的所有资产都将为您打包,您不必再担心它了。

#3


4  

GitHub has a good answer for this, and I'm sure you could modify Heroku's deployment scripts to integrate:

GitHub有一个很好的答案,我相信你可以修改Heroku的部署脚本来集成:

http://github.com/blog/551-optimizing-asset-bundling-and-serving-with-rails

http://github.com/blog/551-optimizing-asset-bundling-and-serving-with-rails

#4


3  

I haven't tried it on heroku yet, but Sprockets might be good for that. Also, in the past, I've had more luck with

我还没有在heroku上尝试过,但是Sprockets可能对此很好。而且,在过去,我有更多的运气

:cache => 'all.css'
:cache => 'all.js'

instead of 'true'

而不是'真'

#5


1  

It's a different way to manage your CSS/Javascript but you may want to check out the Rails plugin shoebox.

这是管理CSS / Javascript的另一种方式,但您可能想查看Rails插件鞋盒。

Shoebox can do combining, minifying, and caching.

Shoebox可以进行组合,缩小和缓存。

#6


0  

The project name says it all:

项目名称说明了一切:

http://github.com/amasses/heroku_asset_packager

http://github.com/amasses/heroku_asset_packager

#7


0  

Here are the config options to compress your assets.

以下是压缩资产的配置选项。

http://guides.rubyonrails.org/asset_pipeline.html#customizing-the-pipeline

http://guides.rubyonrails.org/asset_pipeline.html#customizing-the-pipeline

config.assets.css_compressor = :yui
config.assets.js_compressor = :uglifier
config.assets.compress = true


gem 'uglifier'
gem 'yui-compressor'

#8


0  

There are probably various ways to do this, but what works for me is to minify before pushing. Then I use a subtree to keep my build files separate from the "source" files. So, for example, if you build to a folder called "dist", you can push to a subtree called heroku/master like this:

可能有多种方法可以做到这一点,但对我有用的是在推动之前缩小。然后我使用子树将我的构建文件与“源”文件分开。因此,例如,如果您构建一个名为“dist”的文件夹,您可以像这样推送到名为heroku / master的子树:

git subtree push --prefix dist heroku master

Just don't forget to ensure that the dist folder is not ignored (it often is, by default) - so edit your .gitignore file accordingly.

只是不要忘记确保不会忽略dist文件夹(默认情况下通常是这样) - 因此请相应地编辑.gitignore文件。

The --prefix command ensures that the dist folder effectively becomes the "root" folder from the point of view of that branch.

--prefix命令确保dist文件夹从该分支的角度有效地成为“根”文件夹。

#1


23  

I'm using Jammit on Heroku. Works Great. You can locally build your assets and check in to heroku. use

我在Heroku上使用Jammit。效果很好。您可以在本地构建您的资产并登记到heroku。使用

jammit --force

the current version 0.5.1 has issues working on heroku but you can install the fixed version from git://github.com/documentcloud/jammit.git

当前版本0.5.1在heroku上有问题,但您可以从git://github.com/documentcloud/jammit.git安装修复版本

If you are using Rails 3, specify the below in your bundler Gemfile:

如果您使用的是Rails 3,请在捆绑器Gemfile中指定以下内容:

gem "jammit", :git => "git://github.com/documentcloud/jammit.git"

For Rails 2.*

对于Rails 2. *

config.gem "jammit", :source => "git://github.com/documentcloud/jammit.git"

Good Luck.

祝你好运。

#2


8  

I've found that adding a git pre–commit hook which compiles and packs assets, then adds them to the current commit comes in handy in this case.

我发现在这种情况下添加一个git预提交钩子来编译和打包资产,然后将它们添加到当前提交就派上用场了。

Mine using Jammit looks something like this (in .git/hooks/pre-commit):

我使用Jammit看起来像这样(在.git / hooks / pre-commit中):

jammit
rake barista:brew
git add public/assets/*
git add public/javascripts/*

Like this all your assets will be packed for you and you don't have to worry anymore about it.

像这样,您的所有资产都将为您打包,您不必再担心它了。

#3


4  

GitHub has a good answer for this, and I'm sure you could modify Heroku's deployment scripts to integrate:

GitHub有一个很好的答案,我相信你可以修改Heroku的部署脚本来集成:

http://github.com/blog/551-optimizing-asset-bundling-and-serving-with-rails

http://github.com/blog/551-optimizing-asset-bundling-and-serving-with-rails

#4


3  

I haven't tried it on heroku yet, but Sprockets might be good for that. Also, in the past, I've had more luck with

我还没有在heroku上尝试过,但是Sprockets可能对此很好。而且,在过去,我有更多的运气

:cache => 'all.css'
:cache => 'all.js'

instead of 'true'

而不是'真'

#5


1  

It's a different way to manage your CSS/Javascript but you may want to check out the Rails plugin shoebox.

这是管理CSS / Javascript的另一种方式,但您可能想查看Rails插件鞋盒。

Shoebox can do combining, minifying, and caching.

Shoebox可以进行组合,缩小和缓存。

#6


0  

The project name says it all:

项目名称说明了一切:

http://github.com/amasses/heroku_asset_packager

http://github.com/amasses/heroku_asset_packager

#7


0  

Here are the config options to compress your assets.

以下是压缩资产的配置选项。

http://guides.rubyonrails.org/asset_pipeline.html#customizing-the-pipeline

http://guides.rubyonrails.org/asset_pipeline.html#customizing-the-pipeline

config.assets.css_compressor = :yui
config.assets.js_compressor = :uglifier
config.assets.compress = true


gem 'uglifier'
gem 'yui-compressor'

#8


0  

There are probably various ways to do this, but what works for me is to minify before pushing. Then I use a subtree to keep my build files separate from the "source" files. So, for example, if you build to a folder called "dist", you can push to a subtree called heroku/master like this:

可能有多种方法可以做到这一点,但对我有用的是在推动之前缩小。然后我使用子树将我的构建文件与“源”文件分开。因此,例如,如果您构建一个名为“dist”的文件夹,您可以像这样推送到名为heroku / master的子树:

git subtree push --prefix dist heroku master

Just don't forget to ensure that the dist folder is not ignored (it often is, by default) - so edit your .gitignore file accordingly.

只是不要忘记确保不会忽略dist文件夹(默认情况下通常是这样) - 因此请相应地编辑.gitignore文件。

The --prefix command ensures that the dist folder effectively becomes the "root" folder from the point of view of that branch.

--prefix命令确保dist文件夹从该分支的角度有效地成为“根”文件夹。