|
| 1 | +const gulp = require('gulp'); |
| 2 | +const gulpLoadPlugins = require('gulp-load-plugins'); |
| 3 | +const browserSync = require('browser-sync').create(); |
| 4 | +const runSequence = require('run-sequence'); |
| 5 | +const packageInfo = require('./package.json'); |
| 6 | +const wiredep = require('wiredep').stream; |
| 7 | + |
| 8 | +const $ = gulpLoadPlugins(); |
| 9 | +const reload = browserSync.reload; |
| 10 | + |
| 11 | +const banner = [ |
| 12 | + '/*!', |
| 13 | + ' * <%= package.name %>', |
| 14 | + ' * <%= package.title %>', |
| 15 | + ' * <%= package.url %>', |
| 16 | + ' * @author <%= package.author %>', |
| 17 | + ' * @version <%= package.version %>', |
| 18 | + ' * Copyright ' + new Date().getFullYear() + '. <%= package.license %> licensed.', |
| 19 | + ' */', |
| 20 | +].join('\n'); |
| 21 | + |
| 22 | +gulp.task('images', () => { |
| 23 | + return gulp.src('src/images/**/*') |
| 24 | + .pipe($.cache($.imagemin())) |
| 25 | + .pipe(gulp.dest('app/assets/images')); |
| 26 | +}); |
| 27 | + |
| 28 | +gulp.task('fonts', () => { |
| 29 | + return gulp.src(require('main-bower-files')('**/*.{eot,svg,ttf,woff,woff2}', function (err) { }) |
| 30 | + .concat('src/fonts/**/*')) |
| 31 | + .pipe(gulp.dest('app/assets/fonts')); |
| 32 | +}); |
| 33 | + |
| 34 | +gulp.task('icons', () => { |
| 35 | + return gulp.src('src/icons/**/*.svg') |
| 36 | + .pipe($.iconfont({ |
| 37 | + fontHeight: 1001, |
| 38 | + normalize: true, |
| 39 | + fontName: packageInfo.icons.fontName, // required |
| 40 | + prependUnicode: true, // recommended option |
| 41 | + formats: ['ttf', 'eot', 'woff', 'woff2', 'svg'], // default, 'woff2' and 'svg' are available |
| 42 | + timestamp: Math.round(Date.now() / 1000), // recommended to get consistent builds when watching files |
| 43 | + })) |
| 44 | + .on('glyphs', function (glyphs, options) { |
| 45 | + const opts = { |
| 46 | + glyphs: glyphs.map(function (glyph) { |
| 47 | + return { name: glyph.name, codepoint: glyph.unicode[0].charCodeAt(0) }; |
| 48 | + }), |
| 49 | + fontName: packageInfo.icons.fontName, |
| 50 | + className: packageInfo.icons.className |
| 51 | + }; |
| 52 | + gulp.src('src/templates/helpers/icons-scss.njk') |
| 53 | + .pipe($.consolidate('lodash', opts)) |
| 54 | + .pipe($.rename('_icons.scss')) |
| 55 | + .pipe(gulp.dest('src/styles/helpers')); |
| 56 | + |
| 57 | + gulp.src('src/templates/helpers/icons.njk') |
| 58 | + .pipe($.consolidate('lodash', opts)) |
| 59 | + .pipe(gulp.dest('src/templates')); |
| 60 | + }) |
| 61 | + .pipe(gulp.dest('src/fonts')); |
| 62 | +}); |
| 63 | + |
| 64 | + |
| 65 | +gulp.task('styles', () => { |
| 66 | + return gulp.src('src/styles/*.scss') |
| 67 | + .pipe($.plumber()) |
| 68 | + .pipe($.sass.sync({ outputStyle: 'expanded', precision: 10, includePaths: ['.'] }).on('error', $.sass.logError)) |
| 69 | + .pipe($.autoprefixer({ browsers: ['> 0%'] })) |
| 70 | + .pipe($.header(banner, { package: packageInfo })) |
| 71 | + .pipe(gulp.dest('app/assets/styles')) |
| 72 | + .pipe(reload({ |
| 73 | + stream: true |
| 74 | + })); |
| 75 | +}); |
| 76 | + |
| 77 | +gulp.task('scripts', () => { |
| 78 | + return gulp.src('src/scripts/*.js') |
| 79 | + .pipe($.plumber()) |
| 80 | + .pipe($.babel()) |
| 81 | + .pipe($.header(banner, { package: packageInfo })) |
| 82 | + .pipe(gulp.dest('app/assets/scripts')) |
| 83 | + .pipe(browserSync.stream()); |
| 84 | +}); |
| 85 | + |
| 86 | +gulp.task('wiredep', () => { |
| 87 | + return gulp.src('src/templates/layouts/*.njk') |
| 88 | + .pipe($.plumber()) |
| 89 | + .pipe(wiredep({ |
| 90 | + exclude: [''], |
| 91 | + ignorePath: /^(\.\.\/)*\.\./, |
| 92 | + fileTypes: { |
| 93 | + njk: { |
| 94 | + block: /(([ \t]*)<!--\s*bower:*(\S*)\s*-->)(\n|\r|.)*?(<!--\s*endbower\s*-->)/gi, |
| 95 | + detect: { |
| 96 | + js: /<script.*src=['"]([^'"]+)/gi, |
| 97 | + css: /<link.*href=['"]([^'"]+)/gi |
| 98 | + }, |
| 99 | + replace: { |
| 100 | + js: '<script src="{{filePath}}"></script>', |
| 101 | + css: '<link rel="stylesheet" href="{{filePath}}" />' |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + })) |
| 106 | + .pipe(gulp.dest('src/templates/layouts')); |
| 107 | +}); |
| 108 | + |
| 109 | +gulp.task('views', () => { |
| 110 | + return gulp.src('src/templates/*.njk') |
| 111 | + .pipe($.plumber()) |
| 112 | + .pipe($.nunjucksRender({ path: 'src/templates' })) |
| 113 | + .pipe(gulp.dest('app')) |
| 114 | + .pipe(browserSync.stream()); |
| 115 | +}); |
| 116 | + |
| 117 | +gulp.task('useref', ['views', 'styles', 'scripts'], () => { |
| 118 | + return gulp.src(['app/*.html']) |
| 119 | + .pipe($.useref({ |
| 120 | + searchPath: ['./app', './', 'bower_components'] |
| 121 | + })) |
| 122 | + .pipe($.if(/\.js$/, $.uglify({ |
| 123 | + }))) |
| 124 | + .pipe($.if(/\.css$/, $.cssnano({ |
| 125 | + safe: true, |
| 126 | + autoprefixer: false |
| 127 | + }))) |
| 128 | + .pipe(gulp.dest('dist')); |
| 129 | +}); |
| 130 | + |
| 131 | +gulp.task('build', () => { |
| 132 | + runSequence(['wiredep'], [ |
| 133 | + 'useref', 'images', 'fonts' |
| 134 | + ], () => { |
| 135 | + return gulp.src([ |
| 136 | + 'app/**/*', |
| 137 | + '!app/*.html', |
| 138 | + '!app/**/*.js', |
| 139 | + '!app/**/*.css' |
| 140 | + ]).pipe($.size({ |
| 141 | + title: 'build', |
| 142 | + gzip: true |
| 143 | + })).pipe(gulp.dest('dist')); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +gulp.task('serve', () => { |
| 148 | + runSequence(['wiredep'], [ |
| 149 | + 'views', 'styles', 'scripts', 'images', 'icons', 'fonts' |
| 150 | + ], () => { |
| 151 | + browserSync.init({ |
| 152 | + notify: false, |
| 153 | + port: 8000, |
| 154 | + server: { |
| 155 | + baseDir: ['app', './'] |
| 156 | + }, |
| 157 | + routes: { |
| 158 | + '/bower_components': 'bower_components' |
| 159 | + } |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + |
| 164 | + gulp.watch([ |
| 165 | + 'src/images/**/*' |
| 166 | + ]).on('change', reload); |
| 167 | + |
| 168 | + gulp.watch('src/**/*.{html,njk}', ['views']); |
| 169 | + gulp.watch('src/styles/**/*.scss', ['styles']); |
| 170 | + gulp.watch('src/scripts/**/*.js', ['scripts']); |
| 171 | + gulp.watch('src/fonts/**/*', ['fonts']); |
| 172 | + gulp.watch('src/icons/**/*', ['icons']); |
| 173 | + gulp.watch('src/images/**/*', ['images']); |
| 174 | + gulp.watch('bower.json', ['wiredep', 'fonts']); |
| 175 | +}); |
| 176 | + |
| 177 | +gulp.task('default', ['serve']); |
0 commit comments