|
| 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 assign = require('lodash.assign') |
| 13 | +const browserSync = require('browser-sync') |
| 14 | +const istanbul = require('gulp-babel-istanbul') // Code coverage |
| 15 | +const mocha = require('gulp-mocha') // Unit testing |
| 16 | +const rename = require('gulp-rename') |
| 17 | +require('./test') |
| 18 | + |
| 19 | +const customOpts = { |
| 20 | + entries: ['./jsmimeMain.js'], |
| 21 | + debug: true, |
| 22 | + transform: [['babelify', { ignore: ['./libs/**'] }]], |
| 23 | + ignore: ['./libs/**'], |
| 24 | + standalone: 'jsmime' |
| 25 | +} |
| 26 | + |
| 27 | +const opts = assign({}, watchify.args, customOpts) |
| 28 | + |
| 29 | +/** |
| 30 | + * This task will bundle all other js files and babelify them. |
| 31 | + * If you want to add other processing to the main js files, add your code here. |
| 32 | + */ |
| 33 | +gulp.task('bundle', function () { |
| 34 | + const b = browserify(customOpts) |
| 35 | + b.on('log', gutil.log) |
| 36 | + return b.bundle() |
| 37 | + .on('error', function (err) { |
| 38 | + console.log(err.message) |
| 39 | + browserSync.notify(err.message, 3000) |
| 40 | + this.emit('end') |
| 41 | + }) |
| 42 | + .pipe(plumber()) |
| 43 | + .pipe(source('./jsmimeMain.js')) |
| 44 | + .pipe(rename('jsmime.js')) |
| 45 | + .pipe(buffer()) |
| 46 | + // .pipe(uglify()) |
| 47 | + .pipe(sourcemaps.init({ loadMaps: true })) |
| 48 | + .pipe(sourcemaps.write('./')) |
| 49 | + .pipe(gulp.dest('./')) |
| 50 | +}) |
| 51 | + |
| 52 | +/** |
| 53 | + * This task would bundle js files and also watching them |
| 54 | + */ |
| 55 | +const wb = watchify(browserify(opts)) |
| 56 | +wb.on('log', gutil.log) |
| 57 | +gulp.task('watch-bundle', function () { |
| 58 | + return wb.bundle() |
| 59 | + .on('error', function (err) { |
| 60 | + console.log(err.message) |
| 61 | + browserSync.notify(err.message, 3000) |
| 62 | + this.emit('end') |
| 63 | + }) |
| 64 | + .pipe(plumber()) |
| 65 | + .pipe(source('./jsmimeMain.js')) |
| 66 | + .pipe(rename('jsmime.js')) |
| 67 | + .pipe(buffer()) |
| 68 | + // .pipe(uglify()) |
| 69 | + .pipe(sourcemaps.init({ loadMaps: true })) |
| 70 | + .pipe(sourcemaps.write('./')) |
| 71 | + .pipe(gulp.dest('./')) |
| 72 | +}) |
| 73 | + |
| 74 | +/** |
| 75 | + * This task starts watching the files inside 'src'. If a file is changed, |
| 76 | + * removed or added then it will run refresh task which will run the bundle task |
| 77 | + * and then refresh the page. |
| 78 | + * |
| 79 | + * For large projects, it may be beneficial to separate copying of libs and |
| 80 | + * media from bundling the source. This is especially true if you have large |
| 81 | + * amounts of media. |
| 82 | + */ |
| 83 | +gulp.task('watch', ['watch-bundle'], function () { |
| 84 | + var watcher = gulp.watch('./*.js', ['refresh']) |
| 85 | + watcher.on('change', function (event) { |
| 86 | + console.log('File ' + event.path + ' was ' + event.type + ', running tasks...') |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +/** |
| 91 | + * This task starts browserSync. Allowing refreshes to be called from the gulp |
| 92 | + * bundle task. |
| 93 | + */ |
| 94 | +gulp.task('browser-sync', ['watch'], function () { |
| 95 | + return browserSync({ server: { baseDir: './dist' } }) |
| 96 | +}) |
| 97 | + |
| 98 | +/** |
| 99 | + * This is the default task which chains the rest. |
| 100 | + */ |
| 101 | +gulp.task('default', ['browser-sync']) |
| 102 | + |
| 103 | +/** |
| 104 | + * Using a dependency ensures that the bundle task is finished before reloading. |
| 105 | + */ |
| 106 | +gulp.task('refresh', ['bundle'], browserSync.reload) |
| 107 | + |
| 108 | +function handleError (e) { |
| 109 | + console.error(e.toString()) |
| 110 | + this.emit('end') |
| 111 | +} |
| 112 | + |
| 113 | +gulp.task('pre-test', () => { |
| 114 | + return gulp.src('*.js') |
| 115 | + // Covering files |
| 116 | + .pipe(istanbul()) |
| 117 | + // Force `require` to return covered files |
| 118 | + .pipe(istanbul.hookRequire()) |
| 119 | +}) |
| 120 | + |
| 121 | +gulp.task('test', () => { |
| 122 | + // gulp-mocha needs filepaths so you can't have any plugins before it |
| 123 | + return gulp.src(['test/**/test*.js'], {read: false}) |
| 124 | + .pipe( |
| 125 | + mocha({ |
| 126 | + ui: 'tdd', |
| 127 | + reporter: 'spec', |
| 128 | + globals: ['define'] |
| 129 | + }).on('error', handleError) |
| 130 | + ) |
| 131 | +}) |
| 132 | + |
| 133 | +gulp.task('coverage', ['pre-test'], () => { |
| 134 | + // gulp-mocha needs filepaths so you can't have any plugins before it |
| 135 | + return gulp.src(['test/**/test*.js'], {read: false}) |
| 136 | + .pipe( |
| 137 | + mocha({ |
| 138 | + ui: 'tdd', |
| 139 | + reporter: 'spec', |
| 140 | + globals: ['define'] |
| 141 | + }).on('error', handleError) |
| 142 | + ) |
| 143 | + // Creating the reports after tests ran |
| 144 | + .pipe( |
| 145 | + istanbul.writeReports() |
| 146 | + ) |
| 147 | + // Enforce a coverage of at least 90% |
| 148 | + .pipe( |
| 149 | + istanbul.enforceThresholds({ thresholds: { global: 10 } }) |
| 150 | + ) |
| 151 | +}) |
0 commit comments