Can we make vue.js application without .vue extension component and webpack?

后端 未结 4 720
执笔经年
执笔经年 2020-12-02 13:43

Note: Can we write vue.js large application without using any compiler for code like currently i see all example use webpack now to make vue.js code compatible for b

4条回答
  •  生来不讨喜
    2020-12-02 14:14

    You perfectly can, but with a lot of disadvantages. For example: you cannot easily use any preprocessor, like Sass or Less; or TypeScript or transpile source code with Babel.

    If you don't need support for older browser, you can use ES6 modules today. Almost all browsers support it. See: ES6-Module.

    But Firefox doesn't support dynamic import(). Only Firefox 66 (Nightly) support it and need to be enabled.

    And if that wasn't enough, your web application will not be indexed. It's bad for SEO.

    For example, Googlebot can craw and index Javascript code but still uses older Chrome 41 for rendering, and it's version don't support ES6 modules.

    If that are not disadvantages for you, then you can do this:

    1. Remove any thirty party library import like Vue, VueRouter, etc. And include those in the index.html file using script tags. All global variables are accesible in all es6 modules. For example, remove this line from main.js and all .vue files:

      import Vue from 'vue';
      

      And add this line in your index.html:

      
      
    2. Rewrite all .vue files and change file extension to .js. For example, rewrite something like this:

      
      
      
      

      to something like this:

      let isMounted = false; /* Prevent duplicated styles in head tag */
      
      export default {
          template: `
              
      /* Put an "id" or "class" attribute to the root element of the component. Its important for styling. You can not use "scoped" attribute because there isn't a style tag. */ {{msg}}
      `, mounted: function () { if (!isMounted) { let styleElem = document.createElement('style'); styleElem.textContent = ` #home-page { color: blue; } `; document.head.appendChild(styleElem); isMounted = true; } }, data: function () { return { msg: 'Put home page content here' }; } }
    3. It is all. I put an example in this link

    P.S. Text editing without syntax highlighting can be frustrating. If you use Visual Studio Code you can install Template Literal Editor extension. It allows editing literal strings with syntax highlight. For styles select CSS syntax, and for templates HTML syntax. Unknown tag in HTML are highlighted differently. For solve this, change the color theme. For example, install Brackets Dark Pro color theme or any theme do you like.

    Regards!

提交回复
热议问题