Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript template option #1

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
177d204
Fix warning for duplicated keys
le0pard Dec 25, 2014
4d793ad
Merge pull request #21 from le0pard/master
paolochiodi Jan 14, 2015
7436fd2
disable remove_http_protocol by default
rmoriz Mar 13, 2015
22cf66c
Unindent fenced code blocks.
AndrewKvalheim Mar 23, 2015
0846510
Document the preserve_patterns option.
AndrewKvalheim Mar 23, 2015
f376a80
compress custom js templates
paolochiodi Mar 31, 2015
14983bb
Merge branch 'preserve_patterns' of https://github.com/AndrewKvalheim…
paolochiodi Mar 31, 2015
83111e7
Merge branch 'AndrewKvalheim-preserve_patterns'
paolochiodi Mar 31, 2015
07a2fd2
Merge branch 'sane-defaults' of https://github.com/rmoriz/htmlcompres…
paolochiodi Mar 31, 2015
67ede76
Merge branch 'rmoriz-sane-defaults'
paolochiodi Mar 31, 2015
fdcc0cd
bump version
paolochiodi Mar 31, 2015
a4ad783
add ruby 2.2 to travis build
paolochiodi Apr 1, 2015
4935b48
Update README.md
bolmaster2 Apr 5, 2015
f5487e9
Merge pull request #28 from blmstr/patch-1
paolochiodi Apr 5, 2015
cf13ebc
Place CDATA tags between comments to avoid confusing older browsers.
gustavolobo Jul 2, 2015
b56e2f0
fix a SyntaxError in README.md example.
strcmp Jul 20, 2015
73e5c12
Merge pull request #34 from strcmp/patch-1
paolochiodi Jul 20, 2015
d5fca6a
Merge pull request #33 from firstbanco/cdata-with-comments
paolochiodi Sep 9, 2015
3e2ec38
bump version
paolochiodi Sep 9, 2015
e722390
Add warning for bad performances
paolochiodi Sep 5, 2016
677f6a5
add option to disable remove space inside tags
paolochiodi Sep 23, 2016
80c45aa
document new flag
paolochiodi Sep 23, 2016
2247b66
remove 1.8 from travis, basically dropping support for it
paolochiodi Sep 23, 2016
fde8d98
Merge branch 'master' of github.com:paolochiodi/htmlcompressor
paolochiodi Sep 23, 2016
6ae5234
bring back 1.8.7
paolochiodi Sep 23, 2016
d87551e
bump version
paolochiodi Sep 23, 2016
c35e42f
compressor presence check
GeorgeGorbanev Dec 16, 2017
ca5f23e
Merge pull request #39 from GeorgeGorbanev/compressor-presence-check
paolochiodi Dec 28, 2017
53592bb
Reinstert skipped sections before building the rest of the html
danielcooper Apr 9, 2015
442e1ae
Merge branch 'danielcooper-master'
paolochiodi Dec 28, 2017
3e739ae
add warning about performances
paolochiodi Dec 28, 2017
8e1c04d
bump version
paolochiodi Dec 28, 2017
5f20fd1
add recent ruby versions for travis
paolochiodi Dec 28, 2017
4c88494
Fix readme style
paolochiodi Dec 28, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
language: ruby
rvm:
- 2.1.1
- 2.0.0
- 2.5
- 2.4
- 2.3
- 2.2
- 2.1
- 2.0
- 1.9.3
- jruby-18mode # JRuby in 1.8 mode
- jruby-19mode # JRuby in 1.9 mode
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
0.3.1 - 23/09/2016
Add option flag to disable "remove spaces inside tags"
0.3.0 - 09/09/2015
Fix CDATA errors
0.2.0 - 32/03/2015
Remove warnings in ruby 2.2
Support for custom html templates
Disable remove_http_protocol by default in the middleware
0.1.2 - 04/09/2014
Preserve custom attributes similar to simple attributes, such as ng-disabled (fixes #12)
0.1.1 - 08/05/2014
Expand Down
169 changes: 99 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
Htmlcompressor provides tools to minify html code.
It includes
- HtmlCompressor::Compressor class which is a raw port of [google's htmlcompressor](http://code.google.com/p/htmlcompressor/)
- HtmlCompressor::Rack a rack middleware to compress html pages on the fly
- HtmlCompressor::Rack a rack middleware to compress html pages on the fly.
Beware that the compressor has proved to be too slow in many circunstances to be used for on the fly or real time execution. I encourage you to use it only for build-time optimisations or decrease the number of optimisations that are run at execution time.

Please note that Htmlcompressor is still in alpha version and need some additional love.

Expand All @@ -22,62 +23,92 @@ Using the compressor class is straightforward:
The compressor ships with basic and safe default options that may be overwritten passing the options hash to the constructor:

```ruby
options = {
:enabled => true,
:remove_multi_spaces => true,
:remove_comments => true,
:remove_intertag_spaces => false,
:remove_quotes => false,
:compress_css => false,
:compress_javascript => false,
:simple_doctype => false,
:remove_script_attributes => false,
:remove_style_attributes => false,
:remove_link_attributes => false,
:remove_form_attributes => false,
:remove_input_attributes => false,
:remove_javascript_protocol => false,
:remove_http_protocol => false,
:remove_https_protocol => false,
:preserve_line_breaks => false,
:simple_boolean_attributes => false
}
options = {
:enabled => true,
:remove_spaces_inside_tags => true,
:remove_multi_spaces => true,
:remove_comments => true,
:remove_intertag_spaces => false,
:remove_quotes => false,
:compress_css => false,
:compress_javascript => false,
:simple_doctype => false,
:remove_script_attributes => false,
:remove_style_attributes => false,
:remove_link_attributes => false,
:remove_form_attributes => false,
:remove_input_attributes => false,
:remove_javascript_protocol => false,
:remove_http_protocol => false,
:remove_https_protocol => false,
:preserve_line_breaks => false,
:simple_boolean_attributes => false,
:compress_js_templates => false
}
```

Using rack middleware is as easy as:
Htmlcompressor also ships a rack middleware that can be used with rails, sinatra or rack.

**However keep in mind that compression is slow (especially when also compressing javascript or css) and can negatively impact the performance of your website. Take your measurements and adopt a different strategy if necessary**

Using rack middleware (in rails) is as easy as:

```ruby
config.middleware.use HtmlCompressor::Rack, options
```

And in sinatra:

```ruby
config.middleware.use HtmlCompressor::Rack, options
use HtmlCompressor::Rack, options
```

The middleware uses a little more aggressive options by default:

```ruby
options = {
:enabled => true,
:remove_multi_spaces => true,
:remove_comments => true,
:remove_intertag_spaces => false,
:remove_quotes => true,
:compress_css => false,
:compress_javascript => false,
:simple_doctype => false,
:remove_script_attributes => true,
:remove_style_attributes => true,
:remove_link_attributes => true,
:remove_form_attributes => false,
:remove_input_attributes => true,
:remove_javascript_protocol => true,
:remove_http_protocol => true,
:remove_https_protocol => false,
:preserve_line_breaks => false,
:simple_boolean_attributes => true
}
options = {
:enabled => true,
:remove_multi_spaces => true,
:remove_comments => true,
:remove_intertag_spaces => false,
:remove_quotes => true,
:compress_css => false,
:compress_javascript => false,
:simple_doctype => false,
:remove_script_attributes => true,
:remove_style_attributes => true,
:remove_link_attributes => true,
:remove_form_attributes => false,
:remove_input_attributes => true,
:remove_javascript_protocol => true,
:remove_http_protocol => false,
:remove_https_protocol => false,
:preserve_line_breaks => false,
:simple_boolean_attributes => true
}
```

Rails 2.3 users may need to add
```ruby
require 'htmlcompressor'
require 'htmlcompressor'
```

## Javascript template compression

You can compress javascript templates that are present in the html.
Setting the `:compress_js_templates` options to `true` will by default compress the content of script tags marked with `type="text/x-jquery-tmpl"`.
For compressing other types of templates, you can pass a string (or an array of strings) containing the type: `:compress_js_templates => ['text/html']`.
Please note that activating template compression will disable the removal of quotes from attributes values, as this could lead to unexpected errors with compiled templates.


## Custom preservation rules

If you need to define custom preservation rules, you can list regular expressions in the `preserve_patterns` option. For example, to preserve PHP blocks you might want to define:

```ruby
options = {
:preserve_patterns => [/<\?php.*?\?>/im]
}
```

## CSS and JavaScript Compression
Expand All @@ -87,48 +118,46 @@ In order to minify in page javascript and css, you need to supply a compressor i
A compressor can be `:yui` or `:closure` or any object that responds to `:compress`. E.g.: `compressed = compressor.compress(source)`

```ruby
class MyCompressor

class MyCompressor

def compress(source)
return 'minified'
end

def compress(source)
return 'minified'
end

options = {
:compress_css => true,
:css_compressor => MyCompressor.new,
:compress_javascript => true,
:javascript_compressor => MyCompressor.new
}
end

options = {
:compress_css => true,
:css_compressor => MyCompressor.new,
:compress_javascript => true,
:javascript_compressor => MyCompressor.new
}
```

Please note that in order to use yui or closure compilers you need to manually add them to the Gemfile

```ruby
gem 'yui-compressor'
gem 'yui-compressor'

...
...

options = {
:compress_javscript => true,
:javascript_compressor => :yui,
:compress_css => true
:css_compressor => :yui
}
options = {
:compress_javascript => true,
:javascript_compressor => :yui,
:compress_css => true,
:css_compressor => :yui
}
```

```ruby
gem 'closure-compiler'
gem 'closure-compiler'

...
...

options = {
:compress_javascript => true,
:javascript_compressor => :closure
}
options = {
:compress_javascript => true,
:javascript_compressor => :closure
}
```

## Statistics
Expand Down
2 changes: 1 addition & 1 deletion htmlcompressor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |gem|

gem.add_development_dependency 'yui-compressor', '~> 0.9'
gem.add_development_dependency 'closure-compiler', '~> 1.1'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rake', '~> 10.3.2'
gem.add_development_dependency 'minitest', '~> 5.0'

gem.files = `git ls-files`.split($\)
Expand Down
Loading