|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const watchify = require('watchify') |
| 4 | +const browserify = require('browserify') |
| 5 | +const gulp = require('gulp') |
| 6 | +const plumber = require('gulp-plumber') |
| 7 | +// const uglify = require('gulp-uglify') |
| 8 | +const source = require('vinyl-source-stream') |
| 9 | +const buffer = require('vinyl-buffer') |
| 10 | +const gutil = require('gulp-util') |
| 11 | +const sourcemaps = require('gulp-sourcemaps') |
| 12 | +const mocha = require('gulp-mocha') // Unit testing |
| 13 | +const rename = require('gulp-rename') |
| 14 | +const babelify = require('babelify') |
| 15 | +const istanbul = require('gulp-babel-istanbul') |
| 16 | +const clean = require('gulp-clean') |
| 17 | + |
| 18 | +require('./test') |
| 19 | + |
| 20 | +// Browsersync + Gulp.js |
| 21 | +// http://www.browsersync.cn/docs/gulp/ |
| 22 | + |
| 23 | +function compile (watch) { |
| 24 | + let bundler = browserify('./jsmimeMain.js', { |
| 25 | + debug: true, |
| 26 | + standalone: 'jsmime' |
| 27 | + }) |
| 28 | + bundler = bundler.transform(babelify) |
| 29 | + bundler.on('log', gutil.log) |
| 30 | + |
| 31 | + function rebundle () { |
| 32 | + return bundler.bundle() |
| 33 | + .on('error', function (err) { console.error(err); this.emit('end') }) |
| 34 | + .pipe(plumber()) |
| 35 | + .pipe(source('./jsmimeMain.js')) |
| 36 | + .pipe(rename('jsmime.js')) |
| 37 | + .pipe(buffer()) |
| 38 | + // .pipe(uglify()) |
| 39 | + .pipe(sourcemaps.init({ loadMaps: true })) |
| 40 | + .pipe(sourcemaps.write('./')) |
| 41 | + .pipe(gulp.dest('./dist')) |
| 42 | + .pipe(gulp.dest('./')) |
| 43 | + } |
| 44 | + |
| 45 | + if (watch) { |
| 46 | + bundler = watchify(bundler) |
| 47 | + bundler.on('update', function () { |
| 48 | + console.log('-> bundling...') |
| 49 | + rebundle() |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + return rebundle() |
| 54 | +} |
| 55 | + |
| 56 | +gulp.task('bundle', function () { |
| 57 | + return compile(false) |
| 58 | +}) |
| 59 | + |
| 60 | +gulp.task('watch-bundle', function () { |
| 61 | + return compile(true) |
| 62 | +}) |
| 63 | + |
| 64 | +function handleError (e) { |
| 65 | + console.error(e.toString()) |
| 66 | + this.emit('end') |
| 67 | +} |
| 68 | + |
| 69 | +gulp.task('cleanup', () => { |
| 70 | + return gulp.src(['./jsmime.*', './dist/*'], {read:false}) |
| 71 | + .pipe(clean({force: true})) |
| 72 | +}) |
| 73 | + |
| 74 | +gulp.task('test', () => { |
| 75 | + // gulp-mocha needs filepaths so you can't have any plugins before it |
| 76 | + return gulp.src(['test/**/test_*.js'], {read: false}) |
| 77 | + .pipe( |
| 78 | + mocha({ |
| 79 | + ui: 'tdd', |
| 80 | + reporters : ['xunit-file', 'spec'], |
| 81 | + globals: ['define'] |
| 82 | + }).on('error', handleError) |
| 83 | + ) |
| 84 | +}) |
| 85 | + |
| 86 | +gulp.task('coverage-hook', () => { |
| 87 | + return gulp.src(['./*.js', '!jsmime.*']) |
| 88 | + // Covering files |
| 89 | + .pipe(istanbul()) |
| 90 | + .pipe(istanbul.hookRequire()) |
| 91 | +}) |
| 92 | + |
| 93 | +gulp.task('coverage', ['coverage-hook'], () => { |
| 94 | + // gulp-mocha needs filepaths so you can't have any plugins before it |
| 95 | + return gulp.src(['test/**/test_*.js'], {read: false}) |
| 96 | + .pipe( |
| 97 | + mocha({ |
| 98 | + ui: 'tdd', |
| 99 | + reporters : ['xunit-file', 'spec'], |
| 100 | + globals: ['define'] |
| 101 | + }).on('error', handleError) |
| 102 | + ) |
| 103 | + // Creating the reports after tests ran |
| 104 | + .pipe( |
| 105 | + istanbul.writeReports() |
| 106 | + ) |
| 107 | + // Enforce a coverage of at least 90% |
| 108 | + .pipe( |
| 109 | + istanbul.enforceThresholds({ thresholds: { global: 10 } }) |
| 110 | + ) |
| 111 | +}) |
0 commit comments