How do I use vueJS with Gulp?

Asked

Viewed 1,526 times

1

I’m working on a little project using Gulp and wanted to learn a little more than vue.js then I want to use in the project, but I can’t find anything clear (for me) on the internet of how to gulpfile.js.

My file is like this:

var env         = require('minimist')(process.argv.slice(2)),
    gulp        = require('gulp'),
    gutil       = require('gulp-util'),
    plumber     = require('gulp-plumber'),
    jade        = require('gulp-jade'),
    browserify  = require('gulp-browserify'),
    browserSync = require('browser-sync'),
    uglify      = require('gulp-uglify'),
    concat      = require('gulp-concat'),
    gulpif      = require('gulp-if'),
    stylus      = require('gulp-stylus'),
    jeet        = require('jeet'),
    rupture     = require('rupture'),
    koutoSwiss  = require('kouto-swiss'),
    prefixer    = require('autoprefixer-stylus'),
    modRewrite  = require('connect-modrewrite'),
    imagemin    = require('gulp-imagemin'),
    karma       = require('gulp-karma'),
    cache       = require('gulp-cache'),
    rsync       = require('rsyncwrapper').rsync;


// Call Jade for compile Templates
gulp.task('jade', function () {
    return gulp.src('src/templates/*.jade')
        .pipe(plumber())
        .pipe(jade({pretty: !env.p}))
        .pipe(gulp.dest('build/'));
});

gulp.task('copy', function() {
    return gulp.src(['src/*.html', 'src/*.txt'])
        .pipe(gulp.dest('build/'))
});

// Call Uglify and Concat JS
gulp.task('js', function () {
    return gulp.src('src/js/**/*.js')
        .pipe(plumber())
        .pipe(concat('main.js'))
        .pipe(gulpif(env.p, uglify()))
        .pipe(gulp.dest('build/js'));
});

// Call Uglify and Concat JS
gulp.task('browserify', function () {
    return gulp.src('src/js/main.js')
        .pipe(plumber())
        .pipe(browserify({debug: !env.p}))
        .pipe(gulpif(env.p, uglify()))
        .pipe(gulp.dest('build/js'));
});

// Call Stylus
gulp.task('stylus', function () {
    gulp.src('src/styl/main.styl')
    .pipe(plumber())
        .pipe(stylus({
            use:[koutoSwiss(), prefixer(), jeet(), rupture()],
            compress: env.p,
        }))
        .pipe(gulp.dest('build/css'));
});

// Call Imagemin
gulp.task('imagemin', function () {
    return gulp.src('src/img/**/*')
        .pipe(plumber())
        .pipe(cache(imagemin({optimizationLevel: 3, progressive: true, interlaced: true})))
        .pipe(gulp.dest('build/img'));
});

// Call Watch
gulp.task('watch', function () {
    gulp.watch('src/templates/**/*.jade', ['jade']);
    gulp.watch('src/styl/**/*.styl', ['stylus']);
    gulp.watch('src/js/**/*.js', [(env.fy) ? 'browserify' : 'js']);
    gulp.watch('src/img/**/*.{jpg,png,gif}', ['imagemin']);
});

gulp.task('browser-sync', function () {
    var files = [
       'build/**/*.html',
       'build/css/**/*.css',
       'build/img/**/*',
       'build/js/**/*.js',
    ];

    browserSync.init(files, {
        server: {
            baseDir: './build/',
        },
    });
});

// Rsync
gulp.task('deploy', function () {
    rsync({
        ssh: true,
        src: './build/',
        dest: 'user@hostname:/path/to/www',
        recursive: true,
        syncDest: true,
        args: ['--verbose'],
    },
        function (erro, stdout, stderr, cmd) {
            gutil.log(stdout);
        });
});

// Default task
gulp.task('default', [(env.fy) ? 'browserify' : 'js', 'jade', 'copy', 'stylus', 'imagemin', 'watch', 'browser-sync']);

// Build and Deploy
gulp.task('build', [(env.fy) ? 'browserify' : 'js', 'jade', 'copy', 'stylus', 'imagemin', 'deploy']);

1 answer

1

Your file is already assembling all other resources, would only need to do the same with the .vue.

For that there is the package Gulp-vueify To use it you must download it next to the Babel:
npm install gulp-vueify vueify-insert-css babel-core babel-plugin-transform-runtime babel-preset-es2015 --save-dev

And add to task in his gulpfile.js:

var vueify = require('gulp-vueify');

gulp.task('vueify', function () {
    // Troque aqui para a pasta onde está contendo seus arquivos .vue
    return gulp.src('components/**/*.vue')
        .pipe(vueify())
        .pipe(gulp.dest('./dist'));
});

Add to task vueify in the default and get to work!

You can point to task js for the final folder of .vue (in the example is the dist) and be able to enjoy the uglify and Concat in those files as well.

* This without the use of Browserify

Browser other questions tagged

You are not signed in. Login or sign up in order to post.