Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use vue-loader without vue-cli

I am trying to put together the absolute bare minimum example of a Vue project that uses webpack to transpile .vue files.

My goal is to understand each build step in detail. Most tutorials advise to use vue-cli and use the webpack-simple config. And although that setup works, it seems overkill for my simple purpose. For now I don't want babel, linting or a live web server with hot module reloading.

A minimal example with just import Vue from 'vue' works! Webpack compiles the vue library and my own code into one bundle.

But now, I want to add vue-loader to the webpack config, so that .vue files will get transpiled. I have installed vue loader:

npm install vue-loader
npm install css-loader
npm install vue-template-compiler 

And I have added vue-loader to the webpack config:

var path = require('path')

module.exports = {
  entry: './dev/app.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  }
};

I have created hello.vue

<template>
  <p>{{greeting}} World</p>
</template>

<script>
export default {
    data:function(){
        return {
            greeting:"Hi there"
        }
    }
}
</script>

And in my app I import 'hello'

import Vue from 'vue'
import hello from "./hello.vue";

    new Vue({
      el: '#app',
      template:`<div><hello></hello></div>`,
      created: function () {   
        console.log("Hey, a vue app!")
      }
    })

The loader does not seem to pick up the .vue files, I get the error:

Module not found: Error: Can't resolve './hello.js' 

EDIT

When trying to import hello from 'hello.vue' I get the error:

Unknown custom element: <hello> - did you register the component correctly?

Am I missing a step? Am I importing the .vue component the right way? How do I use the hello.vue component from app.js?

like image 920
Kokodoko Avatar asked Sep 29 '17 16:09

Kokodoko


People also ask

How do I run vue project without command line?

You can add Vue functionality to existing project without Vue CLI. Preferably a project using Webpack as bundler...then it is pretty simple. You need 3 packages: vue@next, @vue/compiler-sfc and vue-loader and add some rule configuration in your webpack. config.

How does vue-loader work?

It's a webpack loader that supports defining Vue. js components in single files known as single-file components (SFCs). These files have the extension . vue and the vue-loader transpiles them into JavaScript so the browser can understand.

How do you use vue loading overlay?

Before you can use this loader plugin in your app, you will need to configure it to work as a plugin. import Vue from 'vue // import the module import Loading from 'vue-loading-overlay'; import 'vue-loading-overlay/dist/vue-loading. css'; // define the plugin and pass object for config Vue.


2 Answers

First, you are not importing the file correctly. You should import it like so:

import Hello from './hello.vue'

Secondly, after you import the component you'll still need to register it somehow. Either do this globally Vue.component('hello', Hello), or on the Vue instance:

new Vue({
  el: '#app',
  template:`<div><hello></hello></div>`,
  components: { 'hello': Hello },
  created: function () {   
    console.log("Hey, a vue app!")
  }
})

As a side note, if you want to be able to import the file without having to specify the .vue extension, you can specify that the .vue extension should be resolved in your config file.

In that case, the resolve object in your config file should look like this:

resolve: {
  alias: {
    'vue$': 'vue/dist/vue.esm.js'
  },
  extensions: ['.js', '.vue', '.json']
}

Here's the documentation on resolve.extensions.

like image 121
thanksd Avatar answered Oct 10 '22 14:10

thanksd


In addition to @thanksd answer:

As of vue-loader v15, a plugin is required:

// webpack.config.js
const VueLoaderPlugin = require('vue-loader/lib/plugin')

module.exports = {
  module: {
    rules: [
      // ... other rules
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      }
    ]
  },
  plugins: [
    // make sure to include the plugin!
    new VueLoaderPlugin()
  ]
}

https://vue-loader.vuejs.org/guide/

like image 28
lukebearden Avatar answered Oct 10 '22 13:10

lukebearden