26

I love vue.js because of its simplicity, which means I can hack a quick one-page SPA with modern, intuitive data-binding syntax and no complex toolchain.

I also love the idea of single-file components which means there is a single place (*.vue file) where each component stores DOM, styling and scripted functionality.

However, I want to use single-file components without wasting time on managing a build process every time I put an app together. In short, I want the benefits of component management without the overhead of a build toolchain, which means letting the browser do the heavy lifting for bootstrapping each *.vue file via XMLHttpRequest and DOM rendering. Making sure that we replace module.exports and import calls with corresponding Vue.component() functionality.

I'd love to know if anyone has come across a client-side (only) solution for using *.vue files on the browser. Surely this has been done already?

tony19
  • 125,647
  • 18
  • 229
  • 307
Steven de Salas
  • 20,944
  • 9
  • 74
  • 82
  • 3
    I haven't seen it. If you don't want to waste time on the build process you can use vue-cli https://github.com/vuejs/vue-cli which takes care of all of that for you. – Bill Criswell Nov 27 '16 at 15:52
  • 1
    After using Hot Module Replacement, I'll gladly incur some build overhead/process in order to enhance my productivity. – David L Nov 27 '16 at 16:29
  • 3
    I can understand not wanting to setup the build process from scratch, but not wanting to use one at all is a little extreme. – Bill Criswell Nov 27 '16 at 17:20
  • What back end technology are you using? I'm struggling with exactly the same ideas... – Mariusz Jamro Dec 13 '16 at 18:59
  • 3
    Hi @MariuszJamro, The point is I dont want to use a back-end technology to do the stitching of components for me. I want it all to take place in the browser. – Steven de Salas Dec 13 '16 at 19:15

4 Answers4

12

I'm absolutely certain this doesn't exist yet, because while it might seem relatively easy, certain functionalities would make it quite difficult to implement. For example:

  1. You don't necessarily import just other .vue components, you can import random external dependencies. Which means that the browser now needs to download and parse npm modules, handle their dependencies, etc.
  2. Different sections of your .vue component (template, logic and style) can be written in languages other than HTML, JS and CSS. Which means the browser now also needs to download a compiler/transpiler for Jade, CoffeeScript, LESS or whatever else you're using and run your code through it. Mind, there's no guarantee that such a transpiler written in JavaScript actually exists, because a node module used in a regular build process could be just a wrapper for some external library which can't be run in a browser.
  3. Styling in a .vue component can be scoped, which means that you now need to parse the template of a component to insert randomly generated IDs as element attributes AND parse the styling of the same component to insert those same IDs in your CSS selectors so that your styling ends up being scoped.

And those are just the most obvious ones off the top of my head. Sure, you could severely limit yourself and not use any of those features, but then it's not really a .vue component anymore, is it?

If you really want to avoid a build process at all costs and are willing to accept the limitations of not using any of the features above, why not just use a single JS file:

$(body).append(`<style>
  // styling goes here
</style>`); 

var myTemplate = `
  // template goes here
`; 

Vue.component('my-component', {
  template: myTemplate
  // component logic goes here
})

You have to load them in the correct order, but there you have it, a poor man's single file component.

mzgajner
  • 2,250
  • 1
  • 17
  • 14
  • 2
    Thanks @mzgajner, thoughtful and well expressed. Glad to see someone trying to answer the how rather than the why. Personally I think the client-loader functionality need need be as extensive as server-side to be useful. +1 and flagging as the right answer (.. hopefully until a better answer comes around). – Steven de Salas Apr 17 '17 at 04:14
  • 1
    Thanks, you absolutely have a point. I think the main reason stuff like this doesn't exist is the fact we optimise front ends for low powered mobiles, so we try to do as much as possible during the build, where we're not limited to any specific language/environment. Consequently we end up with tools that would have to be rewritten in JS, so it's easier to just use some kind of hot reload during development. – mzgajner Apr 17 '17 at 07:58
  • This is no longer the correct answer. Users with +10K please vote to undelete answer below from @Oscar Gardiazabal using https://github.com/edgardleal/require-vuejs. Thanks. – Steven de Salas Nov 26 '19 at 12:44
6

Another way is use: http-vue-loader

Load .vue files directly from your html/js. No node.js environment, no build step.

https://cdn.jsdelivr.net/npm/http-vue-loader@1.4.1/src/httpVueLoader.min.js

Same to in unpkg cdn

https://unpkg.com/http-vue-loader

Here a example

<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/http-vue-loader"></script>

<script>
new Vue({
  el: '#app',
  components: {
    'header': httpVueLoader('/components/header.vue'),
    'nav-bar': httpVueLoader('/components/navbar.vue'),
    'aside': httpVueLoader('/components/aside.vue'),
    'content': httpVueLoader('/components/content.vue'),
    'footer': httpVueLoader('/components/footer.vue')
  }
});
</script>

Or you can load your components from external like

'MyHelloWorldComponent': httpVueLoader('https://my-cdn-or.github.io/path/HelloWorld.vue'),

See the example at : https://codepen.io/mikechen2017/pen/wEdbBN/

Steven de Salas
  • 20,944
  • 9
  • 74
  • 82
LT-Sites
  • 375
  • 5
  • 15
  • Things have moved. This is now the correct answer, though the complete solution is that there is more than one library that can do this (ie [require-vuejs](https://github.com/edgardleal/require-vuejs) seems to work similarly). – Steven de Salas Dec 27 '19 at 20:24
2

It's 2020 and Evan You wrote https://github.com/vuejs/vite just last week.

I'd love to know if anyone has come across a client-side (only) solution...

Vite has a server, but it feels like the old days of Web when we just had Notepad. I had run the demo in less than 5 minutes, it's that easy.

  • it covers or aims to cover the finer details that @mzgajner mentions

For now, I would say that it's only gotcha is that you are in Vue 3 beta realm right away if you use it. No Vue 2.x.

akauppi
  • 17,018
  • 15
  • 95
  • 120
  • Great project. But it doesnt really answer the question. `vite` is still a build tool you have to manage separately, not a browser-based solution for performing the stitching. – Steven de Salas Mar 09 '23 at 09:41
2

However, I want to use single-file components without wasting time on managing a build process every time I put an app together. In short, I want the benefits of component management without the overhead of a build toolchain

I share the sentiment and decided to solve this problem with vue-blocks. Just a single script tag to get going, no build tools required, completely client-side.

It can load vue files (with some limitations though) from the server jsfiddle example:

<template src="path/to/vue-file.vue"></template>

Vue Blocks allows you to write multiple vue components in the html document, like so:

<template component="sample-component">
    <div>
        <h1>Sample component</h1>
    </div>
    <style>
    </style>
    <script>
        export default {
            data() {
                return {}
            },
            mounted() {},
            methods: {
                xx() {

                }
            }
        }
    </script>
</template>

A working demo in jsfiddle: https://jsfiddle.net/o48L0y9j/

Joshua Angnoe
  • 1,010
  • 4
  • 11