question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trim whitespaces from texts in vue-template-compiler output

See original GitHub issue

I love the feature of single file components. They allow for proper syntax highlighting and we can write HTML without putting everything in awkward javascript strings. A proper indentation within the template is useful to improve readability of .vue files.

Consider this example:

<template>
  <div>
    <p>
      This is my first text
    </p>
    <p>
      This is my second text
    </p>
  </div>
</template>

Compile it with webpack. The resulting bundle contains a compiled version of the template, you will find the following line:

return _h('div', [_h('p', ["\n    This is my first text\n  "]), " ", _h('p', ["\n    This is my second text\n  "])])

There is this preserveWhitespace option described here. If you configure webpack with loader: 'vue?preserveWhitespace=false', then you get the following:

return _h('div', [_h('p', ["\n    This is my first text\n  "]), _h('p', ["\n    This is my second text\n  "])])

Note that one array entry " " has gone, i.e. the whitespace between </p> and <p> has been removed. But how about the leading and trailing whitespaces in the string "\n This is my first text\n " ?

My Feature Rrequest

Allow to trim these texts. The compilation result should look like this:

return _h('div', [_h('p', ["This is my first text"]), _h('p', ["This is my second text"])])

Changing this line as follows achieves what I want:

       text = inPre || text.trim()
-        ? decodeHTMLCached(text)
+        ? decodeHTMLCached(inPre || preserveWhitespace ? text : text.trim())
         // only preserve whitespace if its not right after a starting tag
         : preserveWhitespace && currentParent.children.length ? ' ' : '';

But I do not know whether it might break more complex layouts, so I posted it as issue here for discussion.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:10
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

16reactions
cerlestescommented, Aug 1, 2017

@yyx990803 Hey Evan. Just to follow up on this issue: we find lots of pieces like this in our generated code:

[_vm._v("\n\t\t\t\t\t\t" + _vm._s(item.title) + "\n\t\t\t\t\t")]

We have thousands of those in our project, so they add up to quite some kBs. Compression should take care of them quite easily, but never the less it’s just not nice to have this in the generated output when we could simply remove them during compile-time.

I’ll see if I can add an option to the template compiler myself and will give you a pull request if I succeed. I’m thinking of something like purgeWhitespace: ['div', 'span', ...]

8reactions
yyx990803commented, Oct 14, 2016

FYI I don’t think this is a good idea, in the sense that it could be “unsafe” - the user may want to ignore whitespaces between tags, but not necessarily trimming all plain text, e.g inside <pre>. Whitespace between tags and whitespace around plain text are different in this sense.

On the other hand I don’t see practical advantage for this. Ignoring additional whitespace nodes has a small perf gain, but trimming text doesn’t do much in that aspect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue trimming white spaces - Stack Overflow
Vue is not trimming spaces. That's just how HTML works. The space is there, see demo below. new Vue({ el: '#app', data: {...
Read more >
Vue.js Form Input Binding trim Modifier - GeeksforGeeks
It trims the text content by removing the whitespace before the text and after the end of the text. It works with any...
Read more >
Am I using preserveWhitespace option of vue-loader correctly?
I'm using vue-loader 15.7.0 and vue-template-compiler 2.6.10. I see there's a whitespace option of vue-template-compiler but I can't see ...
Read more >
@vue/compiler-sfc | Yarn - Package Manager
In template transform, use compileTemplate to compile the raw template into render function code. In style transform, use compileStyle to compile raw CSS...
Read more >
Application API | Leaf PHP
app.config.compilerOptions.comments # ... Adjusts treatment of HTML comments in templates. ... By default, Vue will remove the comments in production. Setting this ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found