8000 Add option to keep whitespace between elements regardless of new lines (vue-template-compiler) · Issue #11359 · vuejs/vue · GitHub
[go: up one dir, main page]

Skip to content
/ vue Public
  • Notifications < 8000 tool-tip id="tooltip-c962b6c3-b94a-4c1a-8663-8a1bad73d21f" for="repository-details-watch-button" popover="manual" data-direction="s" data-type="description" data-view-component="true" class="sr-only position-absolute">You must be signed in to change notification settings
  • Fork 33.7k

Add option to keep whitespace between elements regardless of new lines (vue-template-compiler) #11359

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

Closed
troxler opened this issue Apr 29, 2020 · 6 comments

Comments

@troxler
Copy link
troxler commented Apr 29, 2020

What problem does this feature solve?

The whitespace option of vue-template-compiler currently allows preserve and condense (default in Vue CLI). While condense will result in smaller code size, it also breaks a common way to structure HTML as it removes all whitespace between elements if it contains new lines.

Please add an option that condenses all whitespace between elements into a single space regardless of whether it contains new lines. Such an option might also be the better default than condense as it is closer to what you would expect from your HTML.

Example using Vue CLI with the default condense option:

<p>
  <span>1</span> <span>a</span>
</p>

<p>
  <span>2</span>
  <span>b</span>
</p>

<p>
  <span>3</span>
  <span>
    c
  </span>
</p>

This results in the following output in the browser:

1 a
2b
3 c 

As you can see, there is no whitespace between 2b. While this behaviour is properly documented in the vue-template-compiler docs, it is not intuitive. Structuring HTML like in 2 is very common and it should not be necessary to restructure it like 3. In addition to being unintuitive, it is currently very hard to find out which package with which configuration and which default options is actually responsible for that behaviour. It is thus super hard to reconfigure your setup according to your needs. And even if you manage to do that, you must decide between no minification with preserve and minification that might break your HTML with condense. I think there should be a middle ground.

What does the proposed API look like?

Introduce an option condense-keep or similar. Using that instead of condense will always keep one whitespace between elements regardless of any new lines.

@troxler
Copy link
Author
troxler commented Apr 29, 2020

When using the Vue CLI, the default condense option can be changed to preserve by adding the following configuration to vue.config.js:

module.exports = {
    chainWebpack: config => {
        config.module
            .rule('vue')
            .use('vue-loader')
            .loader('vue-loader')
            .tap(options => {
                options.compilerOptions.whitespace = 'preserve';
                return options;
            });
    },
};

8000

@diachedelic
Copy link
diachedelic commented Jun 11, 2020

whitespace = 'preserve' fixes the issue if you are only using <span> elements, but if I try either

<p>
  <b>Name:</b>
  <span>Joe</span>
</p>

or

<p>
  <span>Stuff</span>
  <svg>...</svg>
</p>

the space is still removed (using vue-template-compiler@2.6.11)

Edit: actually, I needed to delete ./node_modules/.cache for 'preserve' to fully take effect

@paulmer
Copy link
paulmer commented May 2, 2025

I realize this is an old issue, but as it's still open, I'll add my thoughts: Vue's "condense" functionality is broken in my opinion in that it's overzealous and incompatible with Browser behavior. Whether whitespace can safely be removed is dependent on the properties of the elements around the whitespace. It can be deleted between "block" elements, but not "inline" elements. Since this characteristic depends on the styles applied to the elements, Vue cannot know simply by looking at the source whether or not it is appropriate to remove whitespace. The only safe modification is to collapse multiple spaces and line breaks to a single space (outside of preformatted text of course.)

Absent the safe behavior, we are left with the choice of either using "preserve" and losing any kind of benefit from whitespace removal, or "condense" with the potential for creating unexpected display problems.

If changing "condense" to convert all repeated whitespace characters to a single space is an unacceptable change, then perhaps a third option can be added which doesn't achieve maximal whitespace removal, but also doesn't break the layout.

I also suggest this be made the default behavior when no other preference is selected as it should do no harm and offers a performance benefit.

@leadingGo
Copy link
leadingGo commented May 2, 2025 via email

@zhaoyi-1995
Copy link
zhaoyi-1995 commented May 2, 2025 via email

@troxler
Copy link
Author
troxler commented May 12, 2025

This is indeed an old issue. vue-template-compiler is end-of-life and my preference regarding whitespace handling has changed since. I'd prefer the way JSX does it and necessary whitespace can be added using {' '}.

JSX removes whitespace at the beginning and ending of a line. It also removes blank lines. New lines adjacent to tags are removed; new lines that occur in the middle of string literals are condensed into a single space.

I'm closing this issue. A follow-up issue would likely be more suited for @vue/compiler-core (and @vitejs/plugin-vue).

@troxler troxler closed this as completed May 12, 2025
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

5 participants
CAF
0