Created
December 4, 2016 18:27
-
-
Save Levantado/525415434f2e67124ee54278b81e9716 to your computer and use it in GitHub Desktop.
Gulp 4 setup gulpFile, gulpPath, package.json(dev-depend), structure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const gulp = require('gulp'), | |
$ = require('gulp-load-plugins')({lazy: true}), | |
browserSync = require('browser-sync'), | |
autoprefixer = require('autoprefixer'), | |
del = require('del'), | |
wiredep = require('wiredep').stream, | |
reload = browserSync.reload, | |
paths = require('./gulppath.json'); | |
gulp.task('nodemonWith', (done)=> { | |
log('Launch NodeMon'); | |
const options = { | |
scripts: 'bin/www', | |
delay: 1, | |
debug: true, | |
verbose: true, | |
env: { | |
'PORT': 3000, | |
'DEBUG': '*' | |
}, | |
watch: ['*.js', 'gulpfile.js'] | |
}; | |
$.nodemon(options) | |
.on('start', function(){ console.log('Starting node....')}) | |
.on('restart', function(ev){console.log('Restarting...' + ev);}) | |
.on('exit', function(){console.log('Exit...')}) | |
.on('crash', function(){console.log('Some broke...')}); | |
done(); | |
}); | |
gulp.task('nodemon', function(cb) { | |
let started = false; | |
$.nodemon({ | |
script: 'bin/www', | |
debug: true, | |
verbose: true, | |
watch: [paths.dist.scripts, paths.watch.vendorjs, paths.watch.vendorcs] | |
}).on('start', function () { | |
if (!started) { | |
cb(); | |
started = true; | |
} | |
}); | |
}); | |
gulp.task('browserSync',function (done) { | |
browserSync({ | |
proxy: "http://localhost:3000", | |
files: ["public/**/*.*"], | |
debug: true, | |
verbose: true, | |
port:4000, | |
browser: "google chrome" | |
}); | |
gulp.watch([ | |
paths.watch.json, paths.dist.distall | |
]).on('change', reload); | |
gulp.watch(paths.watch.html, gulp.series('htmlED')); | |
gulp.watch(paths.watch.sass, gulp.series('stylesED')); | |
gulp.watch(paths.watch.coffee, gulp.series('coffeeEd')); | |
gulp.watch(paths.watch.apfonts, gulp.series('fonts')); | |
gulp.watch(paths.supply.bowjson, gulp.parallel('wiredep', 'fonts')); | |
gulp.watch(paths.watch.template, gulp.series('templateEd')); | |
gulp.watch(paths.watch.image, gulp.series('images')); | |
gulp.watch([paths.src.all, paths.src.exhtml],gulp.series('extras')); | |
done() | |
}); | |
gulp.task('editWithNode', (done) => { | |
log('Watch process for running with clean node or nodemon'); | |
gulp.watch(paths.watch.html, gulp.series('htmlED')); | |
gulp.watch(paths.watch.sass, gulp.series('stylesED')); | |
gulp.watch(paths.watch.coffee, gulp.series('coffeeEd')); | |
gulp.watch(paths.watch.apfonts, gulp.series('fonts')); | |
gulp.watch(paths.supply.bowjson, gulp.parallel('wiredep', 'fonts')); | |
gulp.watch(paths.watch.template, gulp.series('templateEd')); | |
gulp.watch(paths.watch.image, gulp.series('images')); | |
gulp.watch([paths.src.all, paths.src.exhtml],gulp.series('extras')); | |
done() | |
}); | |
gulp.task('coffeeEd',function(done) { | |
log('Coffee to JS'); | |
gulp.src(paths.src.coffee) | |
.pipe($.changed(paths.dist.scripts, {extension: '.js'})) | |
.pipe($.plumber()) | |
.pipe($.sourcemaps.init()) | |
.pipe($.coffee({bare:true})) | |
.pipe($.babel({ | |
presets: ['es2015'] | |
})) | |
.pipe($.uglify({ | |
mangle: false, | |
compress: true | |
})) | |
.pipe($.sourcemaps.write()) | |
.pipe(gulp.dest(paths.dist.scripts)) | |
.pipe($.notify({ message: 'COFFEE compile',onLast:true})); | |
done(); | |
}); | |
gulp.task('stylesED', function(done) { | |
log('SCSS to CSS'); | |
gulp.src(paths.src.sass) | |
.pipe($.plumber()) | |
.pipe($.sourcemaps.init()) | |
.pipe($.sass.sync({ | |
outputStyle: 'expanded', | |
precision: 10, | |
includePaths: ['.'] | |
}).on('error', $.sass.logError)) | |
.pipe($.postcss([ autoprefixer({ browsers: ['last 2 versions'] }) ])) | |
.pipe($.groupCssMediaQueries()) | |
.pipe($.cleanCss()) | |
.pipe($.cssnano({safe: true, autoprefixer: false})) | |
.pipe($.sourcemaps.write()) | |
.pipe(gulp.dest(paths.dist.styles)) | |
.pipe($.notify({ message: 'SASS compile',onLast:true})); | |
done(); | |
}); | |
gulp.task('htmlED', function(done) { | |
log('Compile HTML'); | |
gulp.src(paths.src.html) | |
.pipe($.plumber()) | |
.pipe($.useref({searchPath: [paths.dist.dist, '/.']})) | |
.pipe($.if('*.html', $.htmlmin({collapseWhitespace: false}))) | |
.pipe(gulp.dest(paths.dist.dist)) | |
.pipe($.notify({ message: 'HTML compile',onLast:true})); | |
done(); | |
}); | |
gulp.task('templateEd', function(done){ | |
log('Template copy'); | |
gulp.src(paths.src.template) | |
.pipe($.newer(paths.dist.template)) | |
.pipe($.plumber()) | |
.pipe($.htmlmin({collapseWhitespace: true})) | |
.pipe(gulp.dest(paths.dist.template)) | |
.pipe($.notify({ message: 'HTML compile',onLast:true})); | |
done(); | |
}); | |
gulp.task('images', function (done) { | |
log('Imgae min and copy'); | |
gulp.src(paths.src.images) | |
.pipe($.plumber()) | |
.pipe($.cache($.imagemin({ | |
progressive: true, | |
interlaced: true, | |
// don't remove IDs from SVGs, they are often used | |
// as hooks for embedding and styling | |
svgoPlugins: [{cleanupIDs: false}] | |
}))) | |
.pipe(gulp.dest(paths.dist.images)); | |
done(); | |
}); | |
gulp.task('fonts', function (done) { | |
gulp.src(require('main-bower-files')('**/*.{eot,svg,ttf,woff,woff2}', function (error) { | |
if (error) { | |
return console.log(error); | |
} | |
}) | |
.concat(paths.src.fonts)) | |
.pipe(gulp.dest(paths.dist.fonts)) | |
.pipe($.notify({ message: 'Fonts end',onLast:true})); | |
done() | |
}); | |
gulp.task('extras', function (done) { | |
// Copy extra files like json in main directory src{ | |
gulp.src([ | |
paths.src.all, | |
paths.src.exhtml | |
], | |
{dot: true}).pipe(gulp.dest(paths.dist.dist)) | |
.pipe($.notify({ message: 'EXTRAS copy end',onLast:true})); | |
done(); | |
}); | |
gulp.task('clean', del.bind(null, [paths.dist.dist])); | |
gulp.task('wiredep', function (done) { | |
gulp.src(paths.src.html) | |
.pipe(wiredep({ | |
ignorePath: /^(\.\.\/)*\.\./ | |
})) | |
.pipe(gulp.dest(paths.src.src)) | |
.pipe($.notify({ message: 'Wiredep html END',onLast:true})); | |
done() | |
}); | |
gulp.task('builder', gulp.series('clean',gulp.parallel('htmlED', 'stylesED', 'coffeeEd', 'templateEd', 'images', 'fonts', 'extras')), function (done) { | |
done(); | |
return gulp.src(paths.dist.distall).pipe($.size({title: 'build', gzip: true})); | |
}); | |
gulp.task('server:watch', gulp.parallel('nodemon','browserSync')); | |
gulp.task('NodeandEdit',gulp.parallel('nodemonWith','editWithNode')); | |
function log (msg) { | |
if (typeof(msg) === 'object') { | |
for (let item in msg) { | |
if (msg.hasOwnProperty(item)) { | |
$.util.log($.util.colors.blue(msg[item])); | |
} | |
} | |
} else { | |
$.util.log($.util.colors.blue(msg)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"src":{ | |
"src":"app", | |
"all":"app/*.*", | |
"exhtml":"!app/*.html", | |
"coffee":"app/_coffee/**/*.coffee", | |
"sass":"app/_sass/*.scss", | |
"html":"app/*.html", | |
"images":"app/images/**/*", | |
"style":"app/styles", | |
"fonts":"app/fonts/**/*", | |
"scripts":"app/scripts", | |
"template":"app/template/**/*.*" | |
}, | |
"temp":{ | |
"temp":".tmp", | |
"style":".tmp/styles", | |
"scripts":".tmp/scripts", | |
"fonts":".tmp/fonts" | |
}, | |
"dist":{ | |
"distall":"public/**/*", | |
"dist":"public", | |
"fonts":"public/fonts", | |
"images":"public/images", | |
"template":"public/template", | |
"scripts":"public/scripts", | |
"styles":"public/styles" | |
}, | |
"name":{ | |
"script":"main.js" | |
}, | |
"supply":{ | |
"bower":"/bower_components", | |
"sctipts":"/scripts", | |
"bowjson":"bower.json" | |
}, | |
"watch":{ | |
"json":"!public/*json", | |
"html":"app/*.html", | |
"vendorjs":"!public/scripts/vendor", | |
"vendorcs":"!public/styles/vendor.css", | |
"coffee":"app/_coffee/**/*.coffee", | |
"sass":"app/_sass/**/*.scss", | |
"apfonts":"app/fonts/**/*", | |
"tmfonts":".tmp/fonts/**/*", | |
"test":"test/spec/**/*.js", | |
"image":"app/images/**/*", | |
"tmjs":".tmp/scripts/**/*.js", | |
"template":"app/template/**/*.*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "", | |
"version": "0.0.0", | |
"private": true, | |
"scripts": { | |
"start": "node ./bin/www" | |
}, | |
"dependencies": { | |
}, | |
"devDependencies": { | |
"autoprefixer": "^6.4.0", | |
"babel-preset-es2015": "^6.14.0", | |
"browser-sync": "^2.18.1", | |
"chai": "^3.5.0", | |
"coffeeify": "^2.0.1", | |
"del": "^2.2.2", | |
"eslint": "^3.11.1", | |
"eslint-config-google": "^0.7.1", | |
"eslint-config-standard": "^6.2.1", | |
"eslint-plugin-promise": "^3.4.0", | |
"eslint-plugin-standard": "^2.0.1", | |
"graceful-fs": "^4.1.6", | |
"gulp": "github:gulpjs/gulp#4.0", | |
"gulp-babel": "^6.1.2", | |
"gulp-browserify": "^0.5.1", | |
"gulp-cache": "^0.4.5", | |
"gulp-changed": "^1.3.2", | |
"gulp-clean-css": "^2.0.13", | |
"gulp-cli": "^1.2.2", | |
"gulp-coffee": "^2.3.2", | |
"gulp-concat": "^2.6.1", | |
"gulp-cssnano": "^2.1.2", | |
"gulp-eslint": "^3.0.1", | |
"gulp-group-css-media-queries": "^1.1.0", | |
"gulp-htmlmin": "^2.0.0", | |
"gulp-if": "^2.0.2", | |
"gulp-imagemin": "^3.0.3", | |
"gulp-jasmine": "^2.4.2", | |
"gulp-load-plugins": "^1.4.0", | |
"gulp-newer": "^1.3.0", | |
"gulp-ng-annotate": "^2.0.0", | |
"gulp-nodemon": "^2.2.1", | |
"gulp-notify": "^2.2.0", | |
"gulp-plumber": "^1.1.0", | |
"gulp-postcss": "^6.1.1", | |
"gulp-rename": "^1.2.2", | |
"gulp-sass": "^2.3.2", | |
"gulp-size": "^2.1.0", | |
"gulp-sourcemaps": "^2.2.0", | |
"gulp-uglify": "^2.0.0", | |
"gulp-useref": "^3.1.2", | |
"gulp-util": "^3.0.7", | |
"gulp-watch": "^4.3.11", | |
"gulpt": "^0.1.2", | |
"jasmine-reporters": "^2.2.0", | |
"karma": "^1.3.0", | |
"karma-chrome-launcher": "^2.0.0", | |
"karma-jasmine": "^1.0.2", | |
"karma-phantomjs-launcher": "^1.0.2", | |
"karma-safari-launcher": "^1.0.0", | |
"karma-spec-reporter": "0.0.26", | |
"main-bower-files": "^2.13.1", | |
"mocha": "^3.1.2", | |
"node-notifier": "^4.6.1", | |
"wiredep": "^4.0.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File structure | |
app.js | |
bin | |
www | |
app | |
_coffee | |
_sass | |
template | |
images | |
index.html | |
public | |
scripts | |
-app.js | |
vendor | |
-vendor.js | |
modules | |
-view.js | |
-model.js | |
-controller.js | |
styles | |
vendors | |
vendors.css | |
app.css | |
template | |
template*.html | |
index.html | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment