Skip to content

Instantly share code, notes, and snippets.

@Insayt
Last active July 8, 2023 17:42
Show Gist options
  • Save Insayt/272c9b81936a03884768 to your computer and use it in GitHub Desktop.
Save Insayt/272c9b81936a03884768 to your computer and use it in GitHub Desktop.
'use strict';
var gulp = require('gulp'),
watch = require('gulp-watch'),
prefixer = require('gulp-autoprefixer'),
uglify = require('gulp-uglify'),
sass = require('gulp-sass'),
sourcemaps = require('gulp-sourcemaps'),
rigger = require('gulp-rigger'),
cssmin = require('gulp-minify-css'),
imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant'),
rimraf = require('rimraf'),
browserSync = require("browser-sync"),
reload = browserSync.reload;
var path = {
build: {
html: 'build/',
js: 'build/js/',
css: 'build/css/',
img: 'build/img/',
fonts: 'build/fonts/'
},
src: {
html: 'src/*.html',
js: 'src/js/main.js',
style: 'src/style/main.scss',
img: 'src/img/**/*.*',
fonts: 'src/fonts/**/*.*'
},
watch: {
html: 'src/**/*.html',
js: 'src/js/**/*.js',
style: 'src/style/**/*.scss',
img: 'src/img/**/*.*',
fonts: 'src/fonts/**/*.*'
},
clean: './build'
};
var config = {
server: {
baseDir: "./build"
},
tunnel: true,
host: 'localhost',
port: 9000,
logPrefix: "Frontend_Devil"
};
gulp.task('webserver', function () {
browserSync(config);
});
gulp.task('clean', function (cb) {
rimraf(path.clean, cb);
});
gulp.task('html:build', function () {
gulp.src(path.src.html)
.pipe(rigger())
.pipe(gulp.dest(path.build.html))
.pipe(reload({stream: true}));
});
gulp.task('js:build', function () {
gulp.src(path.src.js)
.pipe(rigger())
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write())
.pipe(gulp.dest(path.build.js))
.pipe(reload({stream: true}));
});
gulp.task('style:build', function () {
gulp.src(path.src.style)
.pipe(sourcemaps.init())
.pipe(sass({
sourceMap: true,
errLogToConsole: true
}))
.pipe(prefixer())
.pipe(cssmin())
.pipe(sourcemaps.write())
.pipe(gulp.dest(path.build.css))
.pipe(reload({stream: true}));
});
gulp.task('image:build', function () {
gulp.src(path.src.img)
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()],
interlaced: true
}))
.pipe(gulp.dest(path.build.img))
.pipe(reload({stream: true}));
});
gulp.task('fonts:build', function() {
gulp.src(path.src.fonts)
.pipe(gulp.dest(path.build.fonts))
});
gulp.task('build', [
'html:build',
'js:build',
'style:build',
'fonts:build',
'image:build'
]);
gulp.task('watch', function(){
watch([path.watch.html], function(event, cb) {
gulp.start('html:build');
});
watch([path.watch.style], function(event, cb) {
gulp.start('style:build');
});
watch([path.watch.js], function(event, cb) {
gulp.start('js:build');
});
watch([path.watch.img], function(event, cb) {
gulp.start('image:build');
});
watch([path.watch.fonts], function(event, cb) {
gulp.start('fonts:build');
});
});
gulp.task('default', ['build', 'webserver', 'watch']);
@sokoljr
Copy link

sokoljr commented Apr 20, 2017

Starting 'style:build'...
[20:03:02] 'style:build' errored after 1.76 ms
[20:03:02] TypeError: dest.on is not a function
at DestroyableTransform.Readable.pipe (E:\Front End\projects\myPortfolio\node_modules\through2\node_modules\readable-stream\lib_stream_readable.js:489:8)
at Gulp. (E:\Front End\projects\myPortfolio\gulpfile.js:73:10)
at module.exports (E:\Front End\projects\myPortfolio\node_modules\orchestrator\lib\runTask.js:34:7)
at Gulp.Orchestrator._runTask (E:\Front End\projects\myPortfolio\node_modules\orchestrator\index.js:273:3)
at Gulp.Orchestrator._runStep (E:\Front End\projects\myPortfolio\node_modules\orchestrator\index.js:214:10)
at E:\Front End\projects\myPortfolio\node_modules\orchestrator\index.js:279:18
at finish (E:\Front End\projects\myPortfolio\node_modules\orchestrator\lib\runTask.js:21:8)
at module.exports (E:\Front End\projects\myPortfolio\node_modules\orchestrator\lib\runTask.js:60:3)
at Gulp.Orchestrator._runTask (E:\Front End\projects\myPortfolio\node_modules\orchestrator\index.js:273:3)
at Gulp.Orchestrator._runStep (E:\Front End\projects\myPortfolio\node_modules\orchestrator\index.js:214:10)

в чем может быть проблема?

@andreyshr
Copy link

Шикарно! Спасибо. Надо еще сборку свг спрайта туда прикрутить

@AntMartlet
Copy link

Всё по полочкам. Спасибо))

@sobwoofer
Copy link

очень годная статья, спасибо огромное.

@arnygora
Copy link

arnygora commented Jan 1, 2018

Спасибо за Ваши старания, статья и правда шикарная =))

@Spartaqus25
Copy link

У меня не обновляет автоматически когда я сохраняю HTML документ, в чем может быть бок? Подскажите плиз.

@kvibrik
Copy link

kvibrik commented Jul 31, 2018

Предлагаю изменить путь для rimraf, чтобы он не затирал всю папку build и ускорить формирование проекта. Мы же хотим удалять только картинки, поэтому прописать clean: './build/img/.'

Copy link

ghost commented Nov 5, 2018

Спасибки, пригодилось

@Bullseye95
Copy link

почему при написании gulp у меня выдает ошибку? Хотя все сделал по инструкции!

@Bullseye95
Copy link

может кто ответить?

Copy link

ghost commented Apr 14, 2019

λ gulp
assert.js:341
throw err;
^

AssertionError [ERR_ASSERTION]: Task function must be specified
at Gulp.set [as _setTask] (D:\web documents\Projects\crm\node_modules\undertaker\lib\set-task.js:10:3)
at Gulp.task (D:\web documents\Projects\crm\node_modules\undertaker\lib\task.js:13:8)
at Object. (D:\web documents\Projects\crm\gulpfile.js:108:6)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Module.require (internal/modules/cjs/loader.js:723:19)
at require (internal/modules/cjs/helpers.js:14:16)
at execute (C:\Users\example\AppData\Roaming\npm\node_modules\gulp-cli\lib\versioned^4.0.0\index.js:36:18)

как решить проблемму

@Pavel12398
Copy link

λ gulp
assert.js:341
throw err;
^

AssertionError [ERR_ASSERTION]: Task function must be specified
at Gulp.set [as _setTask] (D:\web documents\Projects\crm\node_modules\undertaker\lib\set-task.js:10:3)
at Gulp.task (D:\web documents\Projects\crm\node_modules\undertaker\lib\task.js:13:8)
at Object. (D:\web documents\Projects\crm\gulpfile.js:108:6)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Module.require (internal/modules/cjs/loader.js:723:19)
at require (internal/modules/cjs/helpers.js:14:16)
at execute (C:\Users\example\AppData\Roaming\npm\node_modules\gulp-cli\lib\versioned^4.0.0\index.js:36:18)

как решить проблемму

удали gulp 4.0.0

npm remove gulp

и установи 3.9.1

npm install gulp@3.9.1

@PeterLS
Copy link

PeterLS commented Nov 5, 2020

Обновленный код gulpfile.js для версии Gulp >= 4
`const {task, src, dest, watch, series, parallel} = require('gulp'),
prefixer = require('gulp-autoprefixer'),
uglify = require('gulp-uglify'),
sass = require('gulp-sass'),
sourcemaps = require('gulp-sourcemaps'),
rigger = require('gulp-rigger'),
cssmin = require('gulp-minify-css'),
imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant'),
rimraf = require('rimraf')

const path = {
build: {
html: 'build/',
js: 'build/js/',
css: 'build/css/',
img: 'build/img/',
fonts: 'build/fonts/'
},
src: {
html: 'src/.html',
js: 'src/js/main.js',
style: 'src/style/main.scss',
img: 'src/img/**/
.',
fonts: 'src/fonts/**/
.'
},
watch: {
html: 'src/**/
.html',
js: 'src/js//*.js',
style: 'src/style/
/.scss',
img: 'src/img/**/
.',
fonts: 'src/fonts/**/
.*'
},
clean: './build'
}

task('clean', function (cb) {
rimraf(path.clean, cb)
})

task('html:build', function (done) {
src(path.src.html)
.pipe(rigger())
.pipe(dest(path.build.html))

done()
})

task('js:build', function (done) {
src(path.src.js)
.pipe(rigger())
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write())
.pipe(dest(path.build.js))

done()
})

task('style:build', function (done) {
src(path.src.style)
.pipe(sourcemaps.init())
.pipe(sass({
sourceMap: true,
errLogToConsole: true
}))
.pipe(prefixer())
.pipe(cssmin())
.pipe(sourcemaps.write())
.pipe(dest(path.build.css))

done()
})

task('image:build', function (done) {
src(path.src.img)
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()],
interlaced: true
}))
.pipe(dest(path.build.img))

done()
})

task('fonts:build', function (done) {
src(path.src.fonts)
.pipe(dest(path.build.fonts))

done()
})

task('build', series(
'html:build',
'js:build',
'style:build',
'fonts:build',
'image:build'
))

task('watch', parallel([
() => {
return watch(path.watch.html, series('html:build'))
},
() => {
return watch(path.watch.style, series('style:build'))
},
() => {
return watch(path.watch.js, series('js:build'))
},
() => {
return watch(path.watch.img, series('image:build'))
},
() => {
return watch(path.watch.fonts, series('fonts:build'))
}
]))

task('default', function (done) {
console.log('TODO')
done()
})`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment