IMO this is totally a bug, because:
- native html behaves in a different way.
- the documentation the behaviour should default to preserving the whitespace.
- vue 2 didn't have this problem.
There is a vue compilerOptions
option called whitespace
that according to the documentation should default to 'preserve'
, but it does not.
To set it to 'preserve'
in webpack use this config:
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
compilerOptions: {
// preserve is supposed to be the default
// see: https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#options
// but as of 2022-01-13 (vue 3.2.26)
whitespace: 'preserve',
},
},
},
Or if you are rendering your components on the fly (with dom templates) use this:
const app = Vue.createApp({...});
app.config.compilerOptions.whitespace = 'preserve';
app.mount('#app');
The documentation (at the time of writting):
whitespace
- Type: string
- Valid values: 'preserve' | 'condense'
- Default: 'preserve'
The default value 'preserve'
handles whitespaces as follows:
- A whitespace-only text node between element tags is condensed into a
single space.
- All other whitespaces are preserved as-is.
If set to 'condense'
:
- A whitespace-only text node between element tags is removed if it
contains new lines. Otherwise, it is condensed into a single space.
- Consecutive whitespaces inside a non-whitespace-only text node are
condensed into a single space.
Using condense mode will result in
smaller compiled code size and slightly improved performance. However,
it will produce minor visual layout differences compared to plain HTML
in certain cases.