middleman / middleman-minify-html

A HTML whitespace minifier for Middleman
https://middlemanapp.com/
MIT License
77 stars 13 forks source link

Compress inline CSS and JS #27

Closed mrtnpro closed 9 years ago

mrtnpro commented 9 years ago

Is there a way to compress inline CSS and JS? The Htmlcompressor documentation says there is an option called compress_css and compress_javascript, however when I try to use any one of them I get a runtime error during my build.

That's what I've tried so far:

activate :minify_html, remove_comments: false, compress_css: true

Runtime error:

/Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/configuration.rb:158:in `define_setting': Setting compress_css doesn't exist (RuntimeError)
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/configuration.rb:123:in `[]='
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/extension.rb:101:in `block in setup_options'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/extension.rb:100:in `each'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/extension.rb:100:in `setup_options'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/extension.rb:76:in `initialize'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-minify-html-3.4.0/lib/middleman-minify-html/extension.rb:20:in `initialize'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/core_extensions/extensions.rb:122:in `new'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/core_extensions/extensions.rb:122:in `activate'
    from /Users/martin/Development/Playground/showmethechicks.com/config.rb:61:in `block in initialize'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/uber-0.0.11/lib/uber/options.rb:80:in `instance_exec'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/uber-0.0.11/lib/uber/options.rb:80:in `proc!'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/uber-0.0.11/lib/uber/options.rb:69:in `evaluate_for'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/uber-0.0.11/lib/uber/options.rb:60:in `evaluate'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/hooks-0.4.0/lib/hooks/hook.rb:53:in `execute_callback'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/hooks-0.4.0/lib/hooks/hook.rb:40:in `block in run'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/hooks-0.4.0/lib/hooks/hook.rb:39:in `each'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/hooks-0.4.0/lib/hooks/hook.rb:39:in `inject'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/hooks-0.4.0/lib/hooks/hook.rb:39:in `run'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/hooks-0.4.0/lib/hooks.rb:55:in `run_hook_for'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/hooks-0.4.0/lib/hooks/instance_hooks.rb:6:in `run_hook'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/core_extensions/extensions.rb:165:in `initialize'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/core_extensions/data.rb:33:in `initialize'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/application.rb:188:in `initialize'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/core_extensions/request.rb:49:in `new'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/core_extensions/request.rb:49:in `inst'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/cli/build.rb:95:in `shared_instance'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/cli/build.rb:62:in `build'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/cli.rb:72:in `method_missing'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/command.rb:29:in `run'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/command.rb:126:in `run'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/lib/middleman-core/cli.rb:20:in `start'
    from /Users/martin/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/middleman-core-3.3.7/bin/middleman:18:in `<top (required)>'
    from /Users/martin/.rbenv/versions/2.1.4/bin/middleman:23:in `load'
    from /Users/martin/.rbenv/versions/2.1.4/bin/middleman:23:in `<main>'
Arcovion commented 9 years ago

See https://github.com/middleman/middleman-minify-html/pull/26#issuecomment-57267749, Middleman can handle inline CSS/JS without this extension.

Never made its way into the docs, let me fix that...

mrtnpro commented 9 years ago

Thanks! This worked for me. However, when I use activate :minify_css it also tries to compile inline javascript strings that contain the word style – e.g.:

<javascript>
  return c.innerHTML="x<style>"+b+"</style>"
</javascript>

which results in a sass compiling error:

error  build/index.html
Invalid CSS after "": expected selector or at-rule, was "', rule, '"
(sass):1

I realised that if i split the string style, it works fine, e.g:

return c.innerHTML="x<sty'+'le>"+b+"</sty'+'le>"

Unfortunately this is a 3rd party javascript (modernizr) that I'm adding here, which I'd rather not like to modify or hotfix. Any idea how I could prevent sass from compiling javascript strings that contain the keyword style when using activate :minify_css?

Arcovion commented 9 years ago

See https://github.com/middleman/middleman/pull/1386, soon we can hopefully use Oga to parse HTML instead of the regexes currently in use.