Vue - webpack vue-loader configuration
Asked Answered
L

3

9

I keep getting error whenever I want to build a file. What is a reason of it? It seems that .vue file is not recognizable by webpack, but webpack configuration file looks properly. webpack error

bundle-app.js   189 kB       1  [emitted]  app
    + 12 hidden modules

ERROR in Unexpected token >
 @ ./app/application.js 7:11-31

webpack.config.js

var path = require("path");

module.exports = {
  context: path.join(__dirname, 'src'),
  entry: {
    app: './app/application.js'
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle-[name].js'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: /src/,
        query: {
          presets: ["es2015"]
        }
      },
      {
        test: /\.vue$/,
        loader: 'vue',
      },
    ]
  },
  vue: {
    loaders: {
      js: 'babel'
    }
  }
}

package.json

"devDependencies": {
    "webpack": "~2.2.1",
    "babel-core": "~6.23.1",
    "babel-loader": "~6.3.1",
    "babel-preset-es2015": "~6.22.0",
    "sass-loader": "~6.0.0",
    "node-sass": "~4.5.0",
    "extract-text-webpack-plugin": "~2.0.0-rc.3",
    "vue-template-compiler": "~2.2.4",
    "css-loader": "~0.27.3",
    "vue-loader": "~11.1.4"
  },
  "dependencies": {
    "vue": "~2.2.4"
  }
}

app/application.js

import Vue from 'vue'
import App from './app.vue'

new Vue({
  el: 'body',
  component: { App }
})

app/app.vue

<template>
  <div id="app">
  </div>
</template>

<script>

    export default {
      data () {
        return {
          msg: 'Hello from vue-loader!'
        }
      }
    }

Lowland answered 19/3, 2017 at 11:45 Comment(8)
Try to move es2015 preset to separated .babelrc file.It should looks something like this { "presets": ["es2015"], "comments": false } For more info check here github.com/bedakb/vuewp/blob/master/.babelrc Also consider to NOT mount vue app to body tag,Radiolarian
@BelminBedak I've done it, but error's still occurringSemolina
Ah, since you are using webpack 2 you can't now omit the -loader keyword.Try change this loader: 'vue' to this loader: 'vue-loader'Radiolarian
Aren't you missing a close </script> after the export default {} ...?Adnopoz
@BelminBedak Still doesn't work.Semolina
@redconservertory Mea culpa, script tag is closed but of course I haven't copy entire script. Sorry :)Semolina
Found typo here component: { App } is should be components - plural.Radiolarian
@BelminBedak I am thankful for your help. The cause of whole mess was extremely silly. I've updated NPM and Node and everything works fine now.Semolina
N
5

There are some extra configs that you need to do, to loader work properly.

I strongly recommend you using the vue-cli for setup all working okay.

npm install -g vue-cli
vue init webpack-simple hello
cd hello
npm install
npm run dev

Basically, at your webpack.config.js, you forgot/made errors in:

1- Loader name should be loader: 'vue-loader' instead of loader: 'vue'.

2- Create an key called resolve, with the content:

alias: {
  vue$: 'vue/dist/vue.common.js',
}

3- And this key vue: ...loader: babel, isn't necessary.

Nicolasanicolau answered 19/3, 2017 at 17:14 Comment(3)
I have multiple entry points in my webpack config file that I've been already working. I haven't wanted create a bigger mess with creating new project via vue-cli. However, in future I will init my vue projects that way. Obrigado :)Semolina
Wow! Does it work in that way? Strange. Anyway, I'm little worried too for using this tools that create everything, but in the case of vue-cli, it's very simple and tiny. Try creating in a demo folder, and see the webpack.config.Nicolasanicolau
Yours is definitely the way to go. However, if you come from the Vue+Webpack tutorial and just want to add vue-loader to the equation, it's enough to just add the following to your webpack config: module: { loaders: [ { test: /\.vue$/, loader: 'vue-loader', }, ] } I really don't know why the Vue documentation is so obscure at this point and forces you to use vue-cli when NPM-installing the modules and adding some lines to the webpack config suffices easily.Wilbanks
I
1

In projects that use vue, individuals do not recommend configuring webpack and vue-loader separately. You can directly use vue official scaffolding, vue-cli. Do not have to consider these configurations, automatically configured.vue-cli

If you just started learning Vue, here's an entry-level demo. Although it is only a small application, but it covers a lot of knowledge points (vue2.0 + vue-cli + vue-router + vuex + axios + mysql + express + pm2 + webpack), including front-end, back-end, database and other sites Some of the necessary elements, for me, learning great significance, would like to encourage each other!

Vue Demo

Icebox answered 8/6, 2017 at 10:20 Comment(0)
P
0

Are you using vue-cli with ESLint? If you do, you need a comma after every element event in the last and the semicolon.

import Vue from 'vue';
import App from './app.vue';

new Vue({
  el: 'body',
  components: { App },
});
Periosteum answered 1/4, 2017 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.