Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spaces are gone in HTML after upgrading to vue-cli 3

I was using vue cli 2 without any custom configuration. Now I upgraded to cli3 and i noticed in the processed HTML it removes ALL whitespace. e.g. if my original html is this:

<div class="link">
    <i class="fa fa-lg" :class="item.icon"/>
    <span class="hidden-sm hidden-xs">{{item.name}}</span>
</div>

the old (cli 2 + webpack) would convert it into this:

<div class="link"><i class="fa fa-lg fa-calendar"/> <span class="hidden-sm hidden-xs">CALENDAR</span></div>

whereas the new one does this:

<div class="link"><i class="fa fa-lg fa-calendar"/><span class="hidden-sm hidden-xs">CALENDAR</span></div>

note the space is gone before <span class... which causes this:

with space

to become this:

without space

My vue.config.js is pretty basic:

// vue.config.js
module.exports = {
  runtimeCompiler: true
}

I know I can just add &nbsp; or make other changes to html itself, but the app is quite large and looking for all these places will take days.

Does anybody know the options I need to ensure it optimizes html the same way it did with the old cli+webpack combo?

like image 786
ierdna Avatar asked Jul 12 '18 11:07

ierdna


People also ask

How do you link vue and HTML?

The simplest way to get started with Vue is to grab the development version script for it and add it to the head tag of your HTML file. Then you can start Vue code inside the HTML file inside of script tags. And have the Vue code connect up with an existing element on your HTML page.

What is index HTML in vue?

Vue CLI generated projects use public/index. html as a template, which contains the headers and tags you'd like to avoid. The only element there required for Vue is <div id="app"></div> , so you could remove everything but that from public/index. html .

Does vue CLI service use webpack?

The vue-cli-service serve command starts a dev server (based on webpack-dev-server) that comes with Hot-Module-Replacement (HMR) working out of the box. In addition to the command line flags, you can also configure the dev server using the devServer field in vue.config.js .


1 Answers

The @VamsiKrishna answer works fine, but preserveWhitespace is deprecated since vue-template-compiler 2.6, you may use new option whitespace instead:

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

You may check other options and valid values here.

Vue 3

On Vue 3, You may use application config, here is the docs:

const app = createApp({});

app.config.compilerOptions.whitespace = 'preserve';
like image 168
Hafez Divandari Avatar answered Sep 28 '22 02:09

Hafez Divandari