|
1 | | -const |
2 | | - browserSync = require('browser-sync'), |
3 | | - gulp = require('gulp'), |
4 | | - gulpAutoprefixer = require('gulp-autoprefixer'), |
5 | | - gulpCleanCss = require('gulp-clean-css'), |
6 | | - gulpDest = require('gulp-dest'), |
7 | | - gulpPlumber = require('gulp-plumber'), |
8 | | - gulpRename = require('gulp-rename'), |
9 | | - gulpSass = require('gulp-sass'), |
10 | | - gulpSourcemaps = require('gulp-sourcemaps'), |
11 | | - gulpStylus = require('gulp-stylus') |
| 1 | +// Load plugins |
| 2 | +const browsersync = require('browser-sync').create(); |
| 3 | +const gulp = require('gulp'); |
| 4 | +const autoprefixer = require('autoprefixer'); |
| 5 | +const sass = require('gulp-sass'); |
| 6 | +const stylus = require('gulp-stylus'); |
| 7 | +const cssnano = require('cssnano'); |
| 8 | +const postcss = require('gulp-postcss'); |
| 9 | +const dest = require('gulp-dest'); |
| 10 | +const plumber = require('gulp-plumber'); |
| 11 | +const rename = require('gulp-rename'); |
| 12 | +const sourcemaps = require('gulp-sourcemaps'); |
| 13 | +const cleanCss = require('gulp-clean-css'); |
| 14 | +const merge = require('merge-stream'); |
12 | 15 |
|
13 | | -gulp.task('browser-sync', function () { |
14 | | - browserSync({ |
15 | | - server: { |
16 | | - baseDir: './' |
17 | | - } |
18 | | - }) |
19 | | -}) |
| 16 | +// BrowserSync |
| 17 | +function browserSync(done) { |
| 18 | + browsersync.init({ |
| 19 | + server: { |
| 20 | + baseDir: './' |
| 21 | + }, |
| 22 | + port: 3000 |
| 23 | + }); |
| 24 | + done(); |
| 25 | +} |
20 | 26 |
|
21 | | -gulp.task('bs-reload', function () { |
22 | | - browserSync.reload() |
23 | | -}) |
| 27 | +// BrowserSync Reload |
| 28 | +function browserSyncReload(done) { |
| 29 | + browsersync.reload(); |
| 30 | + done(); |
| 31 | +} |
24 | 32 |
|
25 | | -gulp.task('scss', function () { |
26 | | - gulp.src(['scss/**/*.scss']) |
27 | | - .pipe(gulpPlumber({ |
28 | | - errorHandler: function (error) { |
29 | | - console.log(error.message) |
30 | | - this.emit('end') |
31 | | - }})) |
32 | | - .pipe(gulpSourcemaps.init()) |
33 | | - .pipe(gulpSass()) |
34 | | - .pipe(gulpAutoprefixer()) |
35 | | - .pipe(gulp.dest('css/')) |
36 | | - .pipe(gulpRename({suffix: '.min'})) |
37 | | - .pipe(gulpCleanCss({compatibility: 'ie8'})) |
38 | | - .pipe(gulpSourcemaps.write('.')) |
39 | | - .pipe(gulp.dest('css/')) |
40 | | - .pipe(browserSync.reload({stream: true})) |
41 | | -}) |
| 33 | +// SCSS task |
| 34 | +function scss() { |
| 35 | + return gulp |
| 36 | + .src('./scss/**/[^_]*.scss') |
| 37 | + .pipe(plumber()) |
| 38 | + .pipe(sourcemaps.init()) |
| 39 | + .pipe(sass()) |
| 40 | + .pipe(postcss([autoprefixer(), cssnano()])) |
| 41 | + .pipe(gulp.dest('css/')) |
| 42 | + .pipe(rename({ suffix: '.min' })) |
| 43 | + .pipe(cleanCss({ compatibility: 'ie8' })) |
| 44 | + .pipe(sourcemaps.write('.')) |
| 45 | + .pipe(gulp.dest('css/')) |
| 46 | + .pipe(browsersync.stream()); |
| 47 | +} |
42 | 48 |
|
43 | | -gulp.task('styl', function () { |
44 | | - gulp.src(['./node_modules/sanitize.css/sanitize.css']) |
45 | | - .pipe(gulpDest('tmp', {ext: '.styl'})) |
46 | | - .pipe(gulp.dest('./')) |
47 | | - gulp.src(['styl/**/[^_]*.styl']) |
48 | | - .pipe(gulpPlumber({ |
49 | | - errorHandler: function (error) { |
50 | | - console.log(error.message) |
51 | | - this.emit('end') |
52 | | - }})) |
53 | | - .pipe(gulpSourcemaps.init()) |
54 | | - .pipe(gulpStylus()) |
55 | | - .pipe(gulpAutoprefixer()) |
56 | | - .pipe(gulpRename({suffix: '.styl'})) |
57 | | - .pipe(gulp.dest('css/')) |
58 | | - .pipe(gulpRename({suffix: '.min'})) |
59 | | - .pipe(gulpCleanCss({compatibility: 'ie8'})) |
60 | | - .pipe(gulpSourcemaps.write('.')) |
61 | | - .pipe(gulp.dest('css/')) |
62 | | - .pipe(browserSync.reload({stream: true})) |
63 | | -}) |
| 49 | +// SCSS task |
| 50 | +function styl() { |
| 51 | + var sanitize = gulp |
| 52 | + .src(['./node_modules/sanitize.css/sanitize.css']) |
| 53 | + .pipe(dest('styl', { ext: '.styl' })) |
| 54 | + .pipe(rename({ prefix: '_' })) |
| 55 | + .pipe(gulp.dest('./')); |
64 | 56 |
|
65 | | -gulp.task('styles', ['scss', 'styl']) |
| 57 | + var compile = gulp |
| 58 | + .src(['styl/**/[^_]*.styl']) |
| 59 | + .pipe(plumber()) |
| 60 | + .pipe(sourcemaps.init()) |
| 61 | + .pipe(stylus()) |
| 62 | + .pipe(postcss([autoprefixer(), cssnano()])) |
| 63 | + .pipe(rename({ suffix: '.styl' })) |
| 64 | + .pipe(gulp.dest('css/')) |
| 65 | + .pipe(rename({ suffix: '.min' })) |
| 66 | + .pipe(cleanCss({ compatibility: 'ie8' })) |
| 67 | + .pipe(sourcemaps.write('.')) |
| 68 | + .pipe(gulp.dest('css/')) |
| 69 | + .pipe(browsersync.stream()); |
66 | 70 |
|
67 | | -gulp.task('default', ['browser-sync'], function () { |
68 | | - gulp.watch('scss/**/*.scss', ['scss']) |
69 | | - gulp.watch('styl/**/*.styl', ['styl']) |
70 | | - gulp.watch('*.html', ['bs-reload']) |
71 | | -}) |
| 71 | + return merge(sanitize, compile); |
| 72 | +} |
| 73 | + |
| 74 | +// Watch files |
| 75 | +function watchFiles() { |
| 76 | + gulp.watch('./scss/**/*', scss); |
| 77 | + gulp.watch('./styl/**/*', styl); |
| 78 | + gulp.watch('*.html', browserSyncReload); |
| 79 | +} |
| 80 | + |
| 81 | +// define complex tasks |
| 82 | +const build = gulp.series(gulp.parallel(scss, styl)); |
| 83 | +const watch = gulp.parallel(watchFiles, browserSync); |
| 84 | + |
| 85 | +// export tasks |
| 86 | +exports.scss = scss; |
| 87 | +exports.styl = styl; |
| 88 | +exports.build = build; |
| 89 | +exports.watch = watch; |
| 90 | +exports.default = build; |
0 commit comments