|
| 1 | +// generated on 2016-01-26 using generator-gulp-webapp 1.1.1 |
| 2 | +require('es6-promise').polyfill(); |
| 3 | + |
| 4 | +import gulp from 'gulp'; |
| 5 | +import gulpLoadPlugins from 'gulp-load-plugins'; |
| 6 | +import browserSync from 'browser-sync'; |
| 7 | +import del from 'del'; |
| 8 | +import {stream as wiredep} from 'wiredep'; |
| 9 | + |
| 10 | +const $ = gulpLoadPlugins(); |
| 11 | +const reload = browserSync.reload; |
| 12 | + |
| 13 | +gulp.task('styles', () => { |
| 14 | + return gulp.src('app/styles/*.css') |
| 15 | + .pipe($.sourcemaps.init()) |
| 16 | + .pipe($.autoprefixer({browsers: ['> 1%', 'last 2 versions', 'Firefox ESR']})) |
| 17 | + .pipe($.sourcemaps.write()) |
| 18 | + .pipe(gulp.dest('.tmp/styles')) |
| 19 | + .pipe(reload({stream: true})); |
| 20 | +}); |
| 21 | + |
| 22 | +gulp.task('scripts', () => { |
| 23 | + return gulp.src('app/scripts/**/*.js') |
| 24 | + .pipe($.plumber()) |
| 25 | + .pipe($.sourcemaps.init()) |
| 26 | + .pipe($.babel()) |
| 27 | + .pipe($.sourcemaps.write('.')) |
| 28 | + .pipe(gulp.dest('.tmp/scripts')) |
| 29 | + .pipe(reload({stream: true})); |
| 30 | +}); |
| 31 | + |
| 32 | +function lint(files, options) { |
| 33 | + return () => { |
| 34 | + return gulp.src(files) |
| 35 | + .pipe(reload({stream: true, once: true})) |
| 36 | + .pipe($.eslint(options)) |
| 37 | + .pipe($.eslint.format()) |
| 38 | + .pipe($.if(!browserSync.active, $.eslint.failAfterError())); |
| 39 | + }; |
| 40 | +} |
| 41 | +const testLintOptions = { |
| 42 | + env: { |
| 43 | + mocha: true |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +gulp.task('lint', lint('app/scripts/**/*.js')); |
| 48 | +gulp.task('lint:test', lint('test/spec/**/*.js', testLintOptions)); |
| 49 | + |
| 50 | +gulp.task('html', ['styles', 'scripts'], () => { |
| 51 | + return gulp.src('app/*.html') |
| 52 | + .pipe($.useref({searchPath: ['.tmp', 'app', '.']})) |
| 53 | + .pipe($.if('*.js', $.uglify())) |
| 54 | + .pipe($.if('*.css', $.cssnano())) |
| 55 | + .pipe($.if('*.html', $.htmlmin({collapseWhitespace: true}))) |
| 56 | + .pipe(gulp.dest('dist')); |
| 57 | +}); |
| 58 | + |
| 59 | +gulp.task('images', () => { |
| 60 | + return gulp.src('app/images/**/*') |
| 61 | + .pipe($.if($.if.isFile, $.cache($.imagemin({ |
| 62 | + progressive: true, |
| 63 | + interlaced: true, |
| 64 | + // don't remove IDs from SVGs, they are often used |
| 65 | + // as hooks for embedding and styling |
| 66 | + svgoPlugins: [{cleanupIDs: false}] |
| 67 | + })) |
| 68 | + .on('error', function (err) { |
| 69 | + console.log(err); |
| 70 | + this.end(); |
| 71 | + }))) |
| 72 | + .pipe(gulp.dest('dist/images')); |
| 73 | +}); |
| 74 | + |
| 75 | +gulp.task('fonts', () => { |
| 76 | + return gulp.src(require('main-bower-files')('**/*.{eot,svg,ttf,woff,woff2}', function (err) {}) |
| 77 | + .concat('app/fonts/**/*')) |
| 78 | + .pipe(gulp.dest('.tmp/fonts')) |
| 79 | + .pipe(gulp.dest('dist/fonts')); |
| 80 | +}); |
| 81 | + |
| 82 | +gulp.task('extras', () => { |
| 83 | + return gulp.src([ |
| 84 | + 'app/*.*', |
| 85 | + '!app/*.html' |
| 86 | + ], { |
| 87 | + dot: true |
| 88 | + }).pipe(gulp.dest('dist')); |
| 89 | +}); |
| 90 | + |
| 91 | +gulp.task('clean', del.bind(null, ['.tmp', 'dist'])); |
| 92 | + |
| 93 | +gulp.task('serve', ['styles', 'scripts', 'fonts'], () => { |
| 94 | + browserSync({ |
| 95 | + notify: false, |
| 96 | + port: 9000, |
| 97 | + server: { |
| 98 | + baseDir: ['.tmp', 'app'], |
| 99 | + routes: { |
| 100 | + '/bower_components': 'bower_components' |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + gulp.watch([ |
| 106 | + 'app/*.html', |
| 107 | + '.tmp/scripts/**/*.js', |
| 108 | + 'app/images/**/*', |
| 109 | + '.tmp/fonts/**/*' |
| 110 | + ]).on('change', reload); |
| 111 | + |
| 112 | + gulp.watch('app/styles/**/*.css', ['styles']); |
| 113 | + gulp.watch('app/scripts/**/*.js', ['scripts']); |
| 114 | + gulp.watch('app/fonts/**/*', ['fonts']); |
| 115 | + gulp.watch('bower.json', ['wiredep', 'fonts']); |
| 116 | +}); |
| 117 | + |
| 118 | +gulp.task('serve:dist', () => { |
| 119 | + browserSync({ |
| 120 | + notify: false, |
| 121 | + port: 9000, |
| 122 | + server: { |
| 123 | + baseDir: ['dist'] |
| 124 | + } |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +gulp.task('serve:test', ['scripts'], () => { |
| 129 | + browserSync({ |
| 130 | + notify: false, |
| 131 | + port: 9000, |
| 132 | + ui: false, |
| 133 | + server: { |
| 134 | + baseDir: 'test', |
| 135 | + routes: { |
| 136 | + '/scripts': '.tmp/scripts', |
| 137 | + '/bower_components': 'bower_components' |
| 138 | + } |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + gulp.watch('app/scripts/**/*.js', ['scripts']); |
| 143 | + gulp.watch('test/spec/**/*.js').on('change', reload); |
| 144 | + gulp.watch('test/spec/**/*.js', ['lint:test']); |
| 145 | +}); |
| 146 | + |
| 147 | +// inject bower components |
| 148 | +gulp.task('wiredep', () => { |
| 149 | + gulp.src('app/*.html') |
| 150 | + .pipe(wiredep({ |
| 151 | + ignorePath: /^(\.\.\/)*\.\./ |
| 152 | + })) |
| 153 | + .pipe(gulp.dest('app')); |
| 154 | +}); |
| 155 | + |
| 156 | +gulp.task('build', ['lint', 'html', 'images', 'fonts', 'extras'], () => { |
| 157 | + return gulp.src('dist/**/*').pipe($.size({title: 'build', gzip: true})); |
| 158 | +}); |
| 159 | + |
| 160 | +gulp.task('default', ['clean'], () => { |
| 161 | + gulp.start('build'); |
| 162 | +}); |
0 commit comments