Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Gulp module and Plugins */
- const gulp = require('gulp'),
- imagemin = require('gulp-imagemin'),
- clean = require('gulp-clean'),
- uglify = require('gulp-uglify'),
- cssmin = require('gulp-cssmin'),
- usemin = require('gulp-usemin'),
- connect = require('gulp-connect-php'),
- browserSync = require('browser-sync'),
- runSequence = require('run-sequence'),
- uncss = require('gulp-uncss'),
- /* Source and Distribution path */
- workPath = 'src',
- savePath = 'dist';
- /* Default task */
- //clean dist -> copy src to dist ->
- //build image & minify js/css ->
- //remove unused css in html files ->
- //start a php server and browserSync and watch
- gulp.task('default', function(){
- runSequence(
- 'copy',
- ['build-img', 'usemin'],
- 'uncss',
- 'start-server'
- );
- });
- /* Clean de dist folder */
- gulp.task('clean', function(){
- return gulp.src(savePath)
- .pipe(clean());
- });
- /* Copy from src to dist */
- //need clean before
- gulp.task('copy', ['clean'], function(){
- return gulp.src(workPath + '/**/*')
- .pipe(gulp.dest(savePath));
- });
- /* Build image */
- //compact them
- //doesn't need to wait (synchronous)
- //need copy before
- gulp.task('build-img', function(){
- gulp.src(savePath + '/public/img/**/*')
- .pipe(imagemin())
- .pipe(gulp.dest(savePath + '/public/img/'));
- });
- /* Build JS & CSS */
- //js: uses uglify plugin
- //css: uses cssmin plugin
- //need copy before
- gulp.task('usemin', function(){
- return gulp.src(savePath + '/**/*.{html,phtml}')
- .pipe(usemin({
- 'path' : savePath + '/public',
- 'outputRelativePath' : 'public',
- 'js' : [uglify],
- 'css' : [cssmin]
- }))
- .pipe(gulp.dest(savePath));
- });
- /* Remove unused css in html files */
- //need usemin before
- gulp.task('uncss', function(){
- return gulp.src(savePath + '/public/css/**/*.css')
- .pipe(uncss({
- html : [savePath + '/app/views/**/*.{html,phtml}']
- }))
- .pipe(gulp.dest(savePath + '/public/css'));
- });
- /* Start a php server with dist & browserSync */
- gulp.task('start-server', function(){
- //create php server
- connect.server({
- port : 8000,
- hostname : '127.0.0.1',
- base : savePath + '/public'
- }, function (){
- //start browserSync
- browserSync.init({
- proxy: '127.0.0.1:8000'
- });
- });
- //look for changes in src
- //then start browser-sync
- gulp.watch(workPath + '/**/*').on('change', function () {
- gulp.start('browser-sync');
- });
- });
- /* Rebuild dist & reload browser */
- //called after any change on src
- gulp.task('browser-sync', function(){
- return runSequence(
- 'copy',
- ['build-img', 'usemin'],
- 'uncss',
- 'browser-reload'
- );
- });
- /* Reload browser */
- gulp.task('browser-reload', function(){
- browserSync.reload();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement