如何与gulp一起使用vue.js?

时间:2016-11-24 22:51:36

标签: gulp vue.js

我正在使用gulp处理一个小项目,我想了解vue.js,所以我想在这个项目中使用vue.js,但我不知道如何在gulpfile中配置vue.js的.js

我想知道如何配置gulpfile以使用vue.js

我的gulpfile.js

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']);

3 个答案:

答案 0 :(得分:2)

Vueifybrowserify转换; gulp-browserify不再维护,但我认为您仍然可以在安装后添加vueify作为转换:

gulp.task('browserify', function () {
    return gulp.src('src/js/main.js')
        .pipe(plumber())
        .pipe(browserify({
            debug: !env.p,
            transform: ['vueify']
       }))
      .pipe(gulpif(env.p, uglify()))
        .pipe(gulp.dest('build/js'));
});

答案 1 :(得分:0)

检查一下: 这是一个允许您为vue.js设置gulp任务的程序包。

https://www.npmjs.com/package/gulp-vueify

答案 2 :(得分:0)

使用:

.pipe(babel({presets: ['es2015']}))

正常的gulp任务帮助我连接并uglify我的vue.js文件。

如果这是你的意思,那么你还需要用npm安装babel并添加:

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