3

I've been trying whole day to get it to work without any luck so if someone could shine some light that would be very much appreciated.

I'm trying to set up environment for working with ES6 files as well as Vue using Webpack.

I've installed all dependencies, and created the following files:

webpack.config.js

module.exports = {

    entry: './resources/assets/source/js/app.js',
    output: {
        filename: 'app.js'
    },
    devtool: 'source-map',
    resolve: {
        alias: {
            'vue$': 'vue/dist/vue.js'
        }
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: 'babel',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015']
                }
            },
            {
                test: /\.vue$/,
                loader: 'vue'
            }
        ]
    }
};

gulpfile.js

var gulp       = require('gulp'),
    webpack    = require('webpack-stream');

gulp.task('script', () => {

    "use strict";

    return gulp.src('./resources/assets/source/js/app.js')
               .pipe(webpack(require('./webpack.config.js')))
               .pipe(gulp.dest('./public/assets/js/'));

});

gulp.task('default', ['script']);

resources/assets/source/js/app.js

var Vue = require('vue');

import Alert from './components/Alert.vue';

new Vue({

    el: '#app',

    components: { Alert },

    ready() {
        alert('ready');
    }

});

resources/assets/source/js/components/Alert.vue

<template>

    <div :class="alertClasses" v-show="show">
        <slot></slot>
        <span class="Alert__close" @click="show == false">x</span>
    </div>

</template>

<script>

    export default {

        props: ['type'],

        data() {
            return {
                show: true
            };
        },

        computed: {

            alertClasses() {

                var type = this.type;

                return {
                    'Alert': true,
                    'Alert--Success': type == 'success',
                    'Alert--Error': type == 'error'
                };

            }

        }
    };

</script>

When I rung gulp everything is bundled and compiled, but when I run it in the browser I get Uncaught SyntaxError: Unexpected token import pointing to the line within the resources/assets/source/js/app.js file.

After hours of trying to figure out what might be causing it I've run out of ideas and am on the verge of giving up so would appreciate any help.

9
  • Which version are you using for Babel and Webpack? Commented Nov 3, 2016 at 18:17
  • 1
    Its [email protected] and [email protected] Commented Nov 3, 2016 at 18:18
  • 1
    Try removing the quotes from '/\.(js|vue)$/' - it might be parsed as a string at the moment when you want it as a regex. Commented Nov 3, 2016 at 18:20
  • 1
    Use the vue-loader for .vue files instead of Babel by adding another element to the list of loaders in the webpack.config.js. Commented Nov 3, 2016 at 18:25
  • 1
    Can you update the post with your latest config? Commented Nov 3, 2016 at 18:32

2 Answers 2

3

You should remove query object from your webpack.config.js file, and create .babelrc file, that would contain this stuff.

{
  "presets": ["es2015"],
  "plugins": ["transform-runtime"],
  "comments": false
}
Sign up to request clarification or add additional context in comments.

1 Comment

It also works fine without transform-runtime - thanks @Belmin
0

import issue seems to be solved in the comments above but there's one more.

.vue files do not seem to be written in standard JavaScript syntax. In order to import them, we need to somehow transform them to JavaScript.

While looking at the webpack.config.js you might notice, that files with .vue extension do not get transpiled in any way.

You can solve this problem with custom loaders. In this case, the loader you're looking for is vue-loader

Steps to solve the issue:

  • npm install --save-dev vue-loader
  • once done, try updating your webpack.config.js with following module section:

module: { loaders: [ { test: /\.js$/, loader: 'babel-loader', query: { presets: ['es2015'] } }, { test: /\.vue$/, loader: 'vue' }, ], }

More details:

Does it help in any way? If not, please let me know.

15 Comments

Thanks @Maciej - I'm afraid this didn't resolve it. Should the loader be vue or vue-loader ?
Hi @seb, please try once again. I didn't strip the unnecessary apostrophes in the loader tests, the code is now updated.
Yes - I have it exactly as in your example, but without any luck I'm afraid.
Here's a sample webpack.config.js file that uses vue-loader : github.com/BrokingClub/CDN/blob/… and a sample .vue component: github.com/BrokingClub/CDN/blob/… so you can compare your setup
Thanks @Belmin - this did the trick! It worked without the transform-runtime. By removing query block from the babel loader and adding the .babelrc with the preset worked a charm. @Belmin could you add it as an answer so that I can accept?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.