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

is there any options to minify handlebars? #72

Closed
keenwon opened this issue Dec 14, 2015 · 6 comments
Closed

is there any options to minify handlebars? #72

keenwon opened this issue Dec 14, 2015 · 6 comments

Comments

@keenwon
Copy link

keenwon commented Dec 14, 2015

image

is there any options to remove those spaces?

@ignoreswing
Copy link

+1
I have the same problem!

@keenwon
Copy link
Author

keenwon commented Jan 14, 2016

I now use Jade

@SimenB
Copy link
Contributor

SimenB commented Jan 24, 2016

Whitespace is upstream handlebars, nothing to do with this loader.

handlebars-lang/handlebars.js#336

@keenwon
Copy link
Author

keenwon commented Jan 25, 2016

Template whitespace may be omitted from either side of any mustache statement by adding a ~ character by the braces.

{{#each nav ~}}
  <a href="{{url}}">
    {{~#if test}}
      {{~title}}
    {{~^~}}
      Empty
    {{~/if~}}
  </a>
{{~/each}}

with this context:

{
  nav: [
    {url: 'foo', test: true, title: 'bar'},
    {url: 'bar'}
  ]
}

results in output sans newlines and formatting whitespace:

<a href="foo">bar</a><a href="bar">Empty</a>

@keenwon keenwon closed this as completed Jan 25, 2016
@ignoreswing
Copy link

Hi, SimenB & Keenwon

Thank you for your prompt reply and helpful suggestion.
"Give expression a " is undoubtedly a good solution.
However, it is too tedious to add every handlebar expression with a '
', i.e., {{~ ~}}.

I found a html-minify plugin which can minify my .hbs code without breaking it. It is based on htmlcompressor
In my production mode, my goal is very simple:

  • Remove all html comments (e.g., )
  • Remove all blanks and new lines

My grunt config is set to:

grunt task:
    grunt.initConfig({
        htmlcompressor: {
           compile: {
               expand: true,
               src   : ['src/templates/**/*.html'],
               dest  : 'compressed',
               options: {
                   type                   : 'html',
                   preserveServerScript   : true,
                   removeQuotes           : true,  //Remove unneeded quotes
                   removeIntertagSpaces   : true,
                   removeSurroundingSpaces: 'min',
                   preserve               : 'custom.hbs.rule'
               }
           }
        },
    });

and custom.hbs.rule (a skipped regular expression) is

    \{\{[^}]+\}\}
    \{\{\{[^}]+\}\}\}

After compression, the compressed hbs code looks like this
2016-01-26 16 17 49

I can easily change my webpack config to load the compressed code, says,

    resolve: {
        alias: {
            'templates'     : __dirname + '/src/templates',           //development mode
            //'templates'   : __dirname + '/compressed/src/templates',//production mode
        }
    },

Alought the plugin cannot support hot module replacement for webpack, it's good enough for me.

@attenzione
Copy link

#46 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants