1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| // gulpfile.js
var gulp = require('gulp'); var plumber = require('gulp-plumber'); var rename = require('gulp-rename'); var jshint = require('gulp-jshint'); var concat = require('gulp-concat'); var babel = require('gulp-babel'); var uglify = require('gulp-uglify'); var browserSync = require('browser-sync'); var cssLint = require('gulp-csslint'); var sass = require('gulp-sass'); var autoprefixer = require('gulp-autoprefixer'); var minifycss = require('gulp-minify-css'); var imagemin = require('gulp-imagemin'); var cache = require('gulp-cache'); var del = require('del');
var browserify = require('browserify'); var source = require('vinyl-source-stream');
var JS_SOURCE = 'src/javascript'; var JS_DEST = 'dist/javascript'; var JS_OUTPUT_FILE = 'main.js'; var CSS_SOURCE = 'src/css'; var CSS_DEST = 'dist/css'; var IMAGE_SOURCE = 'src/images'; var IMAGE_DEST = 'dist/images'; var HTML_SOURCE = 'src/asset'; var HTML_DEST = 'dist/asset'; var SERVER_BASE_DIR = './'; var WATCH_FILE_EXTENSIONS = 'src/asset/*.html';
function clean() { // You can use multiple globbing patterns as you would with `gulp.src`, // for example if you are using del 2.0 or above, return its promise return del(['dist']); }
function browser() { browserSync({ ui: { port: 8402 }, server: { baseDir: SERVER_BASE_DIR, index: 'track.html' }, port: 3113 });
// gulp.watch(WATCH_FILE_EXTENSIONS, ['bs-reload']); gulp.watch(WATCH_FILE_EXTENSIONS, browserSync.reload); gulp.watch(JS_SOURCE + '/**/*.js', gulp.series('javascript', 'pack')); gulp.watch(CSS_SOURCE + '/**/*.scss', gulp.series('css')); gulp.watch(HTML_SOURCE + '/**/*.html', gulp.series('html')); }
function reload() { browserSync.reload(); }
function javascript() { return gulp.src(JS_SOURCE + '/**/*.js') .pipe(plumber({ errorHandler: function (error) { console.log(error.message); } })) .pipe(jshint()) .pipe(jshint.reporter('default')) .pipe(concat(JS_OUTPUT_FILE)) .pipe(babel()) .pipe(rename({suffix: '.min'})) // .pipe(uglify()) .pipe(gulp.dest(JS_DEST + '/')) .pipe(browserSync.reload({stream: true})) }
function css() { return gulp.src(CSS_SOURCE + '/**/*.scss') // .pipe(plumber({ // errorHandler: function (error) { // console.log(error.message); // generator.emit('end'); // } // })) .pipe(cssLint()) .pipe(sass()) .pipe(autoprefixer('last 2 versions')) .pipe(rename({suffix: '.min'})) // .pipe(minifycss()) .pipe(gulp.dest(CSS_DEST + '/')) .pipe(browserSync.reload({stream: true})) }
function images() { return gulp.src(IMAGE_SOURCE + '/**/*') // .pipe(cache(imagemin({optimizationLevel: 3, progressive: true, interlaced: true}))) .pipe(gulp.dest(IMAGE_DEST + '/')); }
function html() { console.log('watched html') return gulp.src(HTML_SOURCE + '/**/*') .pipe(gulp.dest(HTML_DEST + '/')); }
function watch() { gulp.watch(JS_SOURCE + '/**/*.js', ['javascript']); gulp.watch(CSS_SOURCE + '/**/*.scss', ['css']); gulp.watch(HTML_SOURCE + '/**/*.html', ['html']); }
// browserify function pack() { var b = browserify({ entries: ["src/javascript/track.js", "src/javascript/utils.js"], transform: ['babelify'] });
return b.bundle() .pipe(source("bundle.js")) .pipe(gulp.dest("dist/js")); }
gulp.task(pack);
gulp.task(clean); gulp.task(browser); gulp.task(reload); gulp.task(javascript); gulp.task(css); gulp.task(html); gulp.task(images); gulp.task(watch);
gulp.task('default', gulp.series('clean', gulp.parallel('html', 'images', 'css', 'javascript'), 'pack', 'browser'));
|