How to deploy project local to production server using gulp? - gulp

I want to deploy my project local to production server.
At my local machine I am using gulp build gulp serve command.
This command is redirected to me at
http://localhost:3000.
When I added code on production server and run these command.
it is redirect to me at http://142.93.165.118:3000
I want to display original host url like:https://example.com
Currently it is displaying http://142.93.165.118:3000 on production server.
Here is my gulp.js code
'use strict';
/**
The custom gulp file to build and serve the web site.
*/
//The main gulp task creator
var gulp = require('gulp'),
connect = require('gulp-connect');
//SASS transpilation to CSS
var sass = require('gulp-sass');
//Refresh the browser when the files are updated.
var browserSync = require('browser-sync').create();
//Minify JS codes
var uglify = require('gulp-uglify');
//Filter files by extension and pass them to a callback
var gulpIf = require('gulp-if');
//Minify CSS codes
var cssnano = require('gulp-cssnano');
//CSS sourcemaps to find the class definistions easily
var sourcemaps = require('gulp-sourcemaps');
//Optimize images
var imagemin = require('gulp-imagemin');
//Caches the optimized images not to optimize them with every build/run
var cache = require('gulp-cache');
//Delete the given folder contents for clean up
var del = require('del');
//Run multiple tasks concurrently
var runSequence = require('run-sequence');
//Handles AngularJS dependency injection
var ngAnnotate = require('gulp-ng-annotate');
//Renames files
var rename = require('gulp-rename');
//Injects the JS and CSS file paths into the index.html file
var inject = require("gulp-inject");
//Concat(merge) files
var concat = require('gulp-concat');
//Reads bower files from bower.json to process them
var bowerMain = require('main-bower-files');
//Filter files by extension and return the paths.
var gulpFilter = require('gulp-filter');
//Minify HTML files
var htmlmin = require('gulp-htmlmin');
//Concatenates AngugularJS templates
var angularTemplatecache = require("gulp-angular-templatecache");
//This will redirect all requests to index.html file to allow seo friendly urls like tld/sports intead of tld/#/sports
var historyApiFallback = require('connect-history-api-fallback');
var minify = "yes";
var isServe = "false";
var conf = {
srcDir: 'src/',
appStylesDir: 'src/app/',
destDir: 'dist/',
cssDestDir: 'dist/css/',
imagesDestDir: 'dist/images/',
imagesSrcDir: 'src/images/',
fontsSrcDir: ['src/fonts/*', 'bower_components/bootstrap-sass/assets/fonts/bootstrap/*', 'bower_components/bootstrap/dist/fonts/*'],
fontsDestDir: 'dist/fonts/',
jsSrcDir: 'src/app/',
jsDestDir: 'dist/scripts/'
};
var bowerConf = {
"overrides": {
"admin-lte": {
"main": [
"**/AdminLTE.css",
"**/adminlte.js",
"**/_all-skins.min.css",
"**/jquery-jvectormap-1.2.2.min.js",
"**/jquery-jvectormap-world-mill-en.js",
"**/bootstrap3-wysihtml5.all.min.js",
"**/bootstrap3-wysihtml5.min.css",
"**/slider.css",
"**/bootstrap-timepicker.min.css",
"**/iCheck/**/_all.css",
"**/iCheck/**/futurico.css",
"**/iCheck/**/polaris.css",
"**/icheck.min.js",
"**/jquery.inputmask.js",
"**/jquery.inputmask.date.extensions.js",
"**/jquery.inputmask.extensions.js"
],
"dependencies": {
"jquery": "^3.2.1",
"jquery-ui": "1.11.4",
"jquery-slimscroll": "slimscroll#^1.3.8",
"jquery-sparkline": "^2.1.3",
"bootstrap": "^3.3.7",
"font-awesome": "^4.7.0",
"moment": "^2.18.1",
"chart.js": "1.0.*",
"datatables.net": "^1.10.15",
"datatables.net-bs": "^2.1.1",
"fastclick": "^1.0.6",
"Flot": "flot#^0.8.3",
"fullcalendar": "^3.4.0",
"ion.rangeSlider": "ionrangeslider#^2.2.0",
"jvectormap": "^1.2.2",
"jquery-knob": "^1.2.13",
"morris.js": "^0.5.1",
"PACE": "pace#^1.0.2",
"select2": "^4.0.3",
"Ionicons": "ionicons#^2.0.1",
"inputmask": "jquery.inputmask#^3.3.7",
"bootstrap-colorpicker": "^2.5.1",
"bootstrap-datepicker": "^1.7.0",
"bootstrap-daterangepicker": "^2.1.25",
"bootstrap-timepicker": "^0.5.2",
"bootstrap-slider": "*"
}
},
"bootstrap": {
"main": [
"**/bootstrap.min.css",
"**/bootstrap.min.js"
]
},
"mocha": {
"ignore": true
},
"jquery-slimscroll": {
"main": "**/jquery.slimscroll.min.js"
},
"inputmask": {
"ignore": true
},
"font-awesome": {
"main": [
"**/font-awesome.min.css",
"**/fontawesome-webfont.eot",
"**/fontawesome-webfont.woff2",
"**/fontawesome-webfont.woff",
"**/fontawesome-webfont.ttf",
"**/fontawesome-webfont.svg"
]
},
"Flot": {
"main": [
"**/jquery.flot.js",
"**/jquery.flot.resize.js",
"**/jquery.flot.pie.js",
"**/jquery.flot.categories.js"
]
},
"select2": {
"main": [
"**/select2.min.css",
"**/select2.full.min.js"
]
},
"bootstrap-colorpicker": {
"main": [
"**/bootstrap-colorpicker.min.css",
"**/bootstrap-colorpicker.min.js"
]
},
"ckeditor": {
"ignore": true
}
}
};
gulp.task('bower', function () {
var jsFilter = gulpFilter('**/*.js', {restore: true});
var cssFilter = gulpFilter(['**/*.css'], {restore: true});
var fontFilter = gulpFilter(['**/*.eot', '**/*.woff', '**/*.svg', '**/*.ttf'], {restore: true});
var imageFilter = gulpFilter(['**/*.gif', '**/*.png', '**/*.svg', '**/*.jpg', '**/*.jpeg'], {restore: true});
return gulp.src(bowerMain(bowerConf))
// JS
.pipe(jsFilter)
.pipe(concat('vendor.js'))
.pipe(gulp.dest(conf.jsDestDir))
.pipe(uglify())
.pipe(rename("vendor.min.js"))
.pipe(gulp.dest(conf.jsDestDir))
.pipe(jsFilter.restore)
// CSS
.pipe(cssFilter)
.pipe(concat('vendor.css'))
.pipe(cssnano())
.pipe(sourcemaps.write('.'))
.pipe(rename("vendor.min.css"))
.pipe(gulp.dest(conf.cssDestDir))
.pipe(cssFilter.restore)
// FONTS
.pipe(fontFilter)
.pipe(gulp.dest(conf.fontsDestDir))
.pipe(fontFilter.restore)
// IMAGES
.pipe(imageFilter)
.pipe(gulp.dest(conf.imagesDestDir))
.pipe(imageFilter.restore)
});
//JS operations
gulp.task('js', function () {
return gulp.src([
conf.jsSrcDir + '/app.js',
conf.jsSrcDir + '/core/**/*.js',
conf.jsSrcDir + '/pages/**/*.js',
conf.jsSrcDir + '/modules/**/*.js',
conf.jsSrcDir + '**/*.html'
])
.pipe(gulpIf('*.html', htmlmin({
removeEmptyAttributes: true,
removeAttributeQuotes: true,
collapseBooleanAttributes: true,
collapseWhitespace: true
})))
.pipe(gulpIf('*.html', angularTemplatecache('templateCacheHtml.js', {
module: 'adminlte',
root: 'app'
})))
.pipe(concat("app.js"))
.pipe(ngAnnotate())
//.pipe(gulpIf(minify === 'yes', uglify()))
.pipe(rename("app.min.js"))
.pipe(gulp.dest(conf.jsDestDir))
});
//SASS operations
gulp.task('sass', function () {
return gulp.src(
[
conf.srcDir + "index.scss",
conf.appStylesDir + '**/**/*.scss'
])
.pipe(concat("app.css"))
.pipe(sass().on('error', sass.logError)) // log errors to console
.pipe(gulpIf(minify === 'yes', cssnano())) //Minify
.pipe(rename('app.min.css'))
.pipe(gulp.dest(conf.cssDestDir)) // Save in the given folder
});
//INJECT operations
gulp.task('inject', function () {
return gulp.src('src/index.html')
//Remove dist from the link of the css and js files.
.pipe(inject(gulp.src(conf.cssDestDir + 'vendor.min.css', {read: false}), {
ignorePath: 'dist/',
starttag: '<!-- bower:css -->',
endtag: '<!-- endbower:css -->'
}))
.pipe(inject(gulp.src(conf.cssDestDir + 'app.min.css', {read: false}), {
ignorePath: 'dist/',
starttag: '<!-- app:css -->',
endtag: '<!-- endapp:css -->'
}))
.pipe(inject(gulp.src(conf.jsDestDir + (isServe === "yes" ? 'vendor.js' : 'vendor.min.js'), {read: false}), {
ignorePath: 'dist/',
starttag: '<!-- bower:js -->',
endtag: '<!-- endbower:js -->'
}))
.pipe(inject(gulp.src(conf.jsDestDir + 'app.min.js', {read: false}), {
ignorePath: 'dist/',
starttag: '<!-- app:js -->',
endtag: '<!-- endapp:js -->'
}))
.pipe(gulp.dest(conf.destDir));
});
// Watchers
gulp.task('watch', function () {
gulp.watch('src/app/**/*.scss', ['sass', browserSync.reload]);
gulp.watch('src/app/**/*.html', ['js', browserSync.reload]);
gulp.watch('src/app/**/*.js', ['js', browserSync.reload]);
});
// Start browserSync server
gulp.task('browserSync', function () {
browserSync.init({
server: {
baseDir: 'dist',
livereload: true,
index: 'index.html',
middleware: [historyApiFallback()]
}
});
});
gulp.task('images', function () {
return gulp.src(conf.imagesSrcDir + '**/*.+(png|jpg|jpeg|gif|svg|ico)')
// Caching images
.pipe(cache(imagemin({
interlaced: true
})))
.pipe(gulp.dest(conf.imagesDestDir))
});
//Copy fonts
gulp.task('fonts', function () {
return gulp.src(conf.fontsSrcDir)
.pipe(gulp.dest(conf.fontsDestDir))
});
//Clean up
gulp.task('clean', function () {
return del.sync('dist').then(function (cb) {
return cache.clearAll(cb);
});
});
gulp.task('clear', () =>
cache.clearAll()
);
gulp.task('clean:dist', function () {
console.log("clean dist should backup the current dist folder when build called");
return del.sync(['dist/**/*', '!dist/images', '!dist/images/**/*']);
});
gulp.task('serve', function (callback) {
minify = "no";
isServe = "yes";
runSequence(
['sass', 'js'],
['inject'],
['watch', 'browserSync'],
callback
)
});
gulp.task('connect', function() {
connect.server({
root: 'app',
livereload: true
});
});
gulp.task('default', ['connect']);
var onError = function (err) {
console.log(err);
};
var gulp = require('gulp');
var webserver = require('gulp-webserver');
gulp.task('build', function (callback) {
runSequence(
'clean:dist',
'sass',
'js',
'bower',
['images', 'fonts'],
['inject'],
callback
)
});
Could you provide me proper way.

step 1) I have moved all files of dist folder to root directory.
step 2) Deleted dist folder
step 3) run index.html
its working for me

Related

Why Gulp runs without errors but is not compiling the SCSS scripts?

I setted up my Gulpfile and so far it looked good. I had no errors anymore. Just looked a bit too fast as he compiled it. But if he would really do the job this wouldn't be any problem. But as i run my gulp css task it didn't compiled my SCSS files into CSS and also not created the minified, concatinated build file afterwards. I checked paths and searched for solutions in internet but this doesn't helped me further.
I had no errors. So why he don't do the job? Did i made a mistake in the gulpfile? I used for running the task async functions to avoid an error. But it seems it just let disappear errors but it doesn't work. Has anybody an idea what i need to change?
Thats what my console does after running gulp css:
[08:58:02] Using gulpfile C:\xampp\htdocs\united-in-faith\gulpfile.js
[08:58:02] Starting 'css'...
[08:58:02] Finished 'css' after 3.71 ms
Thats my gulpfile:
const { src, dest, watch, task, series, parallel } = require('gulp');
var sass = require('gulp-sass');
var minifyCss = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();
var minifyPhp = require('gulp-php-minify');
var sourcemaps = require('gulp-sourcemaps');
var scssOrigin = './scripts/scss/**/_*.scss';
var jsOrigin = './scripts/js/*.js';
var cssFilePath = './scripts/css/**/_*.css';
var jsDestination = './build/scripts/js';
var cssDestination = './scripts/scripts/css';
var cssDestinationMin = './build/scripts/css';
var jsDestinationMin = './build/scripts/js';
var phpDestination = './build';
var htmlDestination = './build';
var appCssMin = 'app.min.css';
var appJsMin = 'app.min.js';
var appJs = 'app.js';
var appCss = 'app.css';
var watchJs = './scripts/js/**/_*.js';
var watchCss = './scripts/scss/**/_*.scss';
function js(done) {
src(jsOrigin)
.pipe(concat({ path: appJsMin}))
.pipe(uglify())
.pipe(dest(jsDestinationMin));
done();
}
function css(done) {
var processors = [
autoprefixer,
];
src(scssOrigin, { sourcemaps: true})
.pipe(sourcemaps.init())
.pipe(sass({ outputStyle: "expanded" }).on('error', sass.logError))
.pipe(sourcemaps.write('./maps'))
.pipe(postcss(processors))
.pipe(dest(cssDestination));
src('scripts/scss/main.scss', { sourcemaps: true})
.pipe(sass({ outputStyle: "expanded" }).on('error', sass.logError))
.pipe(dest('scripts/css'));
done();
}
function minifyCss (done) {
return src(cssFilePath, { sourcemaps: true})
.pipe(sourcemaps.init())
.pipe(minifyCss({compatibility: 'ie8'}))
.pipe(concat(appCssMin))
.pipe(sourcemaps.write('./maps'))
.pipe(dest(cssDestinationMin));
}
/*function cssNoMin (done) {
var processors = [
autoprefixer,
];
src(scssOrigin, { sourcemaps: true})
.pipe(sass({ outputStyle: "expanded" }).on('error', sass.logError))
.pipe(postcss(processors))
.pipe(dest(cssFilePath));
src(cssFilePath, { sourcemaps: true})
.pipe(concat(appCss))
.pipe(dest(scssDestinationMin))
.pipe(browserSync.stream());
done();
}
*/
function jsNoMin (done) {
src(jsOrigin)
.pipe(concat({ path: appJs }))
.pipe(dest(jsDestinationMin))
.pipe(browserSync.stream());
done();
}
function browserSyncServer (done) {
browserSync.init({
open: false,
injectChanges: true,
server: { //server variable set elsewhere
baseDir: './',
proxy: "https://localhost/united-in-faith",
port: 8080,
online: true
},
ui: {
port: 8082
}
});
done();
}
function browserSyncReload (done) {
browserSync.reload();
done();
}
function publishPhp () {
return src('sites/**/*php')
.pipe(dest(phpDestination));
src('sites/**/*html')
.pipe(dest(phpDestination));
}
function watch_files () {
watch('*.php', browserSyncReload);
watch([watchJs,watchCss], series(css,minifyCss, js, browserSyncReload))
.on('error', function (err) {
console.log(err.toString());
this.emit('end');
});
}
function watchNoMin () {
watch('*.php', browserSyncReload);
watch([watchJs,watchCss], series(css, jsNoMin , browserSyncReload))
.on('error', function (err) {
console.log(err.toString());
this.emit('end');
});
}
task("default", async function() {
series(css,minifyCss,js,browserSyncServer);
});
task("css", async function() {
series(css,minifyCss,browserSyncServer);
});
task("js", async function() {
series(js,browserSyncServer);
});
task("publish", async function() {
series(css, minifyCss,js,publishPhp,browserSyncServer);
});
task("watch", series(watch_files, browserSyncServer));
task("watch-no-min", series(watchNoMin, browserSyncServer));
task("all", parallel(css,js));
task("css-no-min", css);
task("js-no-min", js);
Thats my dependencies of Package.json
"dependencies": {
"lodash": "^4.17.21",
"#fortawesome/fontawesome-pro": "^6.0.0-beta1",
"bootstrap": "^5.0.1",
"bootstrap-icons": "^1.5.0",
"ckeditor4": "^4.16.1",
"cookieconsent": "^3.1.1",
"emojione": "^4.5.0",
"gulp-php-minify": "^0.1.3",
"jquery": "^3.6.0",
"jquery-ui": "^1.12.1",
"jquery-ui-touch-punch": "^0.2.3"
},
"devDependencies": {
"q": "^1.5.1",
"autoprefixer": "^10.2.6",
"browser-sync": "^2.26.14",
"chokidar": "^3.5.2",
"debug": "^4.3.1",
"gulp": "^4.0.2",
"gulp-clean-css": "^4.3.0",
"gulp-cli": "^2.3.0",
"gulp-concat": "^2.6.1",
"gulp-postcss": "^9.0.0",
"gulp-sass": "^4.1.0",
"gulp-sourcemaps": "^3.0.0",
"gulp-uglify": "^3.0.2",
"gulp-watch": "^5.0.1",
"webpack": "^5.40.0",
"webpack-cli": "^4.7.2"
}

gulp-sequence not working

I have implementing gulp task in the project.
I have installed gulp-assests-version-replace to make version control file of css file.
Now I want to run above task after creating the app.css. It runs before creating the app.css, so version control file cannot be created.
Below is the gulp-file for running the gulp tasks.
var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
var changed = require('gulp-changed');
var concat = require('gulp-concat');
var del = require('del');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var imagemin = require('gulp-imagemin');
var jshint = require('gulp-jshint');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var assetsVersionReplace = require('gulp-assets-version-replace');
var uglify = require('gulp-uglify');
var path = {
assets: 'skin/frontend/custom_theme/default/'
};
var config = {
tasks: {
css: {
autoprefixerOptions: {
// For "browserslist" see "package.json"
cascade: false,
supports: false // See: https://github.com/filamentgroup/select-css/issues/17
},
dest: path.assets + 'css/build',
sassOptions: {
outputStyle: 'compressed'
},
src: path.assets + 'css/src/**/*.scss'
},
version: {
autoprefixerOptions: {
// For "browserslist" see "package.json"
cascade: false,
supports: false // See: https://github.com/filamentgroup/select-css/issues/17
},
dest: path.assets + 'css/build',
sassOptions: {
outputStyle: 'compressed'
},
src: path.assets + 'css/build/app.css'
}
}
}
gulp.task('default', ['clean'], function () {
gulp.start('css');
gulp.start('assetsVersionReplace');
});
gulp.task('clean', function () {
return del([
path.assets + 'css/build',
path.assets + 'js/build',
path.assets + 'js/min',
path.assets + 'img/build'
]);
});
gulp.task('css', function () {
return gulp.src(config.tasks.css.src)
.pipe(plumber({
errorHandler: reportError
}))
.pipe(sass(config.tasks.css.sassOptions))
.pipe(autoprefixer(config.tasks.css.autoprefixerOptions))
.pipe(gulp.dest(config.tasks.css.dest));
});
gulp.task('assetsVersionReplace', function () {
return gulp.src(config.tasks.version.src)
.pipe(assetsVersionReplace({
replaceTemplateList: [
'app/design/frotend/custom_theme/default/template/page/head.phtml'
]
}))
.pipe(gulp.dest(config.tasks.version.dest))
});
gulp.task('watch', function () {
// Run "default" immediately
gulp.start('default');
// CSS
gulp.watch(config.tasks.css.src, ['css']);
gulp.watch(config.tasks.version.src, ['assetsVersionReplace']);
});
How can I run assetsVersionReplace task after creating the css (or after runnung 'CSS' task)?
Either change
gulp.task('assetsVersionReplace', function () {
to
gulp.task('assetsVersionReplace', ['css'], function () {
or, if that isn't an option, add a helper task
gulp.task('assetsVersionReplaceAfterCss', ['css'], function () {
gulp.start('assetsVersionReplace');
}
and use that instead
Why don't you use gulp series.
gulp.task('default',
gulp.series(
'css',
'assetsVersionReplace'
)
);

the broswer cannot reload when files updates using gulp and browser-sync

I have total followed the instructions of https://browsersync.io/docs/gulp
my gulpfile.js code:
let gulp = require('gulp');
let sass = require('gulp-sass');
let browserSync = require('browser-sync').create();
let reload = browserSync.reload;
gulp.task('server', ['css'], function() {
browserSync.init({
server: {
baseDir: './dist'
}
});
gulp.watch('src/sass/*.scss', ['css']);
gulp.watch("dist/*.html").on('change', reload);
gulp.watch("dist/sass/*.css").on('change', reload);
});
// 编译Sass
gulp.task('css', function() { // 任务名
return gulp.src('src/sass/a.scss') // 目标 sass 文件
.pipe(sass()) // sass -> css
.pipe(gulp.dest('dist/sass/')) // 目标目录
// .pipe(reload({stream: true}))
.pipe(browserSync.stream());
});
gulp.task('default', ['server']);
when I update the sass file, it seems that the css file will be updated immediately, but the broswer cannot reload.
and the command line show:
It seem that the cli cannot connect to the broswer?
===
The problem is solved,my html file does not have a body tag,see https://github.com/BrowserSync/browser-sync/issues/392#issuecomment-245380582
I post you my GULP FILE ... as you can see i use the
gulp-webserver
it only need to put reload:true to work as you aspect:
"use strict";
var gulp = require("gulp"),
concat = require("gulp-concat"),
cssmin = require("gulp-cssmin"),
htmlmin = require("gulp-htmlmin"),
uglify = require("gulp-uglify"),
merge = require("merge-stream"),
del = require("del"),
bundleconfig = require("./bundleconfig.json"); // make sure bundleconfig.json doesn't contain any comments
var livereload = require('gulp-livereload');
var changed = require('gulp-changed');
var stripDebug = require('gulp-strip-debug');
var minifyHTML = require('gulp-minify-html');
var autoprefix = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');
var rename = require('gulp-rename');
var jshint = require('gulp-jshint');
var shell = require('gulp-shell');
var sass = require('gulp-sass');
var preprocess = require('gulp-preprocess');
var ngAnnotate = require('gulp-ng-annotate');
var templateCache = require('gulp-angular-templatecache');
var webserver = require('gulp-webserver'); //<-- YOU HAVE TO INSTALL IT MAYBE WITH NPM (npm install --save -dev gulp-webserver)
// THESE IS THE TASK FOR WEB SERVER AND RELOAD
gulp.task('webserver', function () {
gulp.src('')
.pipe(webserver({
host: 'localhost', // <-- IF YOU PUT YOUR IP .. YOU CAN WATCH IT FROM OTHER devices
port: 80,
livereload: true,
directoryListing: true,
open: true,
fallback: 'index-dev.html'
}));
});
// Task to process Index page with different include ile based on enviroment
gulp.task('html-dev', function () {
return gulp.src('Index.html')
.pipe(preprocess({ context: { NODE_ENV: 'dev', DEBUG: true } })) //To set environment variables in-line
.pipe(rename({ suffix: '-dev' }))
.pipe(gulp.dest(''));
});
// Task to process Index page with different include ile based on enviroment
gulp.task('html-prod', function () {
return gulp.src('Index.html')
.pipe(preprocess({ context: { NODE_ENV: 'prod', DEBUG: false } })) //To set environment variables in-line
.pipe(rename({ suffix: '-prod' }))
.pipe(gulp.dest(''));
});
// CSS concat, auto-prefix, minify, then rename output file
gulp.task('minify-css', function () {
var cssPath = { cssSrc: ['./app/view/assets/content/css/styles-default.css', './app/view/assets/content/css/styles-theme-1.css', './app/view/assets/content/css/styles-theme-2.css', '!*.min.css', '!/**/*.min.css'], cssDest: './app_build/content_build/css' };
return gulp.src(cssPath.cssSrc)
// .pipe(shell(['attrib C:/_goocity/Goocity/Goocity.Web/app_build/content_build/css/*.css -r']))
// .pipe(concat('styles.css'))
.pipe(autoprefix('last 2 versions'))
.pipe(minifyCSS())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest(cssPath.cssDest));
});
// Lint Task
gulp.task('lint', function () {
var jsPath = { jsSrc: ['./app/app.js', './app/controller/*.js', './app/view/utils/directives/*.js', './app/model/services/*.js'] };
return gulp.src(jsPath.jsSrc)
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
//SASS task
gulp.task('sass', function () {
var sassPath = {
cssSrc: ['./app/view/assets/content/css/*.scss'], cssDest: './app/view/assets/content/css'
};
gulp.src(sassPath.cssSrc)
.pipe(shell(['attrib C:/_goocity/Goocity/Goocity.Web/app/view/assets/content/css/*.css -r']))
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(sassPath.cssDest))
.pipe(livereload());
});
gulp.task('cache', function () {
return gulp.src('./app/view/template/**/*.html')
.pipe(templateCache('templates.js', { module: 'Goocity', root: '/app/view/template' }))
.pipe(gulp.dest('./app/view/template'))
.pipe(livereload());
});
gulp.task('cache-directives', function () {
return gulp.src('./app/view/utils/directives/template/**/*.html')
.pipe(templateCache('templates.js', { module: 'Goocity', root: '/app/view/utils/directives/template' }))
.pipe(gulp.dest('./app/view/utils/directives/template'))
.pipe(livereload());
});
gulp.task("min:js", function () {
var tasks = getBundles(".js").map(function (bundle) {
return gulp.src(bundle.inputFiles, { base: "." })
.pipe(ngAnnotate({
remove: true,
add: true,
single_quotes: false
}))
.pipe(concat(bundle.outputFileName))
.pipe(uglify())
.pipe(gulp.dest("."));
});
return merge(tasks);
});
//gulp.task("min:css", function () {
// var tasks = getBundles(".css").map(function (bundle) {
// return gulp.src(bundle.inputFiles, { base: "." })
// .pipe(concat(bundle.outputFileName))
// .pipe(cssmin())
// .pipe(gulp.dest("."));
// });
// return merge(tasks);
//});
gulp.task("min:html", function () {
var tasks = getBundles(".html").map(function (bundle) {
return gulp.src(bundle.inputFiles, { base: "." })
.pipe(concat(bundle.outputFileName))
.pipe(htmlmin({ collapseWhitespace: true, minifyCSS: true, minifyJS: true }))
.pipe(gulp.dest("."));
//.pipe(livereload());
});
return merge(tasks);
});
gulp.task("clean", function () {
var files = bundleconfig.map(function (bundle) {
return bundle.outputFileName;
});
return del(files);
});
gulp.task("min", ["clean", "min:js", "minify-css", 'html-dev', "cache", "cache-directives"]);
gulp.task("default", ["watch", "webserver"]);
gulp.task("watch", function () {
livereload.listen();
// watch for JS error
gulp.watch(['./app/app.js', './app/controllers/*.js', './app/directives/*.js', './app/services/*.js'], ['lint']);
// min js
getBundles(".js").forEach(function (bundle) {
gulp.watch(bundle.inputFiles, ["min:js"]);
});
//watch for SASS changes
gulp.watch('./app/view/assets/content/css/scss/*.scss', ['sass']);
gulp.watch('./app/view/assets/content/css/scss/settings/*.scss', ['sass']);
gulp.watch('./app/view/assets/lib/bower/lumx/dist/scss/base/*.scss', ['sass']);
gulp.watch('./app/view/assets/lib/bower/lumx/dist/scss/modules/*.scss', ['sass']);
gulp.watch('./app/view/assets/content/css/*.scss', ['sass']);
gulp.watch('./app/view/assets/content/css/Hover-master/scss/*.scss', ['sass']);
//gulp.watch('./app/view/assets/content/css/*.css', ['minify-css']);
//watch for PREPROCCESS x PROD
gulp.watch('Index.html', ['html-prod']);
//watch for PREPROCCESS x DEV
gulp.watch('Index.html', ['html-dev']);
//watch for TEMPLATE CACHE
gulp.watch('./app/view/template/**/*.html', ['cache']);
gulp.watch('./app/view/utils/directives/template/**/*.html', ['cache-directives']);
});
function getBundles(extension) {
return bundleconfig.filter(function (bundle) {
return new RegExp(extension).test(bundle.outputFileName);
});
}
The problem is solved,my html file does not have a body tag,see https://github.com/BrowserSync/browser-sync/issues/392#issuecomment-245380582

Gulp - how to delete build directory before running other tasks

I got this Gulp file where I'm running various tasks.
I would like to delete the contents of my build directory before running any other tasks but can't work out how to do this.
I've tried lots of various ways I found when searching for a solution to this but can't get it to work.
Here is my Gulp file:
// List required plugins
var gulp = require('gulp'),
del = require('del'),
uglify = require('gulp-uglify'),
postcss = require('gulp-postcss'),
lost = require('lost'),
nano = require('gulp-cssnano'),
sourcemaps = require('gulp-sourcemaps'),
autoprefixer = require('autoprefixer'),
mqpacker = require('css-mqpacker'),
mixins = require('postcss-mixins'), // Must go before other plugins in the PostCSS task
nested = require('postcss-nested'),
vars = require('postcss-simple-vars'),
partials = require('postcss-import'),
plumber = require('gulp-plumber'),
gutil = require('gulp-util'),
imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant'),
browserSync = require('browser-sync').create();
// Config - Paths and other config
var src = {
cssDir: 'src/css/**/*.css',
jsDir: 'src/js/**/*.js',
imgDir: 'src/img/*',
svgDir: 'src/img/svg/**/*.svg',
htmlDir: 'src/**/*.html',
srcDir: 'src/',
},
build = {
buildDir: 'build/',
cssDir: 'build/assets/css/',
htmlDir: 'build/**/*.html',
jsDir: 'build/assets/js/',
imgDir: 'build/assets/img/',
},
options = {
autoprefix: { browsers: ['last 2 versions'] },
imagemin: { optimizationLevel: 7, progressive: true, interlaced: true, svgoPlugins: [ {removeViewBox: false}, {cleanupIDs: false} ], use: [pngquant()] },
port: '80',
};
// Error handling function
// Thanks to https://cameronspear.com/blog/how-to-handle-gulp-watch-errors-with-plumber/
var onError = function(err) {
gutil.beep();
console.log(err);
this.emit('end');
}
// Clean task
gulp.task('clean:build', function () {
return del([
build.buildDir,
]);
});
// Copy HTML to Build
gulp.task('html', function () {
return gulp.src(src.htmlDir)
.pipe(plumber({
errorHandler: onError
}))
.pipe(gulp.dest(build.buildDir))
.pipe(browserSync.stream());
});
// BrowserSync Server + watching files for changes
gulp.task('server', function() {
browserSync.init({
server: {
baseDir: build.buildDir
},
port: options.port,
});
});
// PostCSS Task
gulp.task('css', function () {
var processors = [
partials(),
lost(),
autoprefixer({ browsers: ['last 2 version'] }),
mixins(),
nested(),
vars(),
mqpacker(), // Make sure mqpacker is last!
];
return gulp.src(src.cssDir)
.pipe(plumber({
errorHandler: onError
}))
.pipe(sourcemaps.init())
.pipe(postcss(processors))
//.pipe(nano()) - Disable minification during development
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(build.cssDir))
.pipe(browserSync.stream());
});
// JS Task
gulp.task('compressJS', function() {
return gulp.src(src.jsDir)
.pipe(plumber({
errorHandler: onError
}))
.pipe(uglify())
.pipe(gulp.dest(build.jsDir))
.pipe(browserSync.stream());
});
// Image Task
gulp.task('imagemin', function() {
return gulp.src(src.imgDir)
.pipe(plumber({
errorHandler: onError
}))
.pipe(imagemin(
options.imagemin
))
.pipe(gulp.dest(build.imgDir))
.pipe(browserSync.stream());
});
gulp.task('watch', function () {
gulp.watch(src.cssDir, ['css']);
gulp.watch(src.jsDir, ['compressJS']);
gulp.watch(src.htmlDir, ['html']);
gulp.watch(src.imgDir, ['imagemin']);
});
gulp.task('default', ['html', 'imagemin', 'compressJS', 'server', 'watch']);
How should I do to run the task clean:build before any other tasks but only run it once. Or is there a better way of cleaning out files before running a build?
Thanks!!
Have your tasks depend on your clean task:
gulp.task('html', ['clean:build'], function() { ... })
this is how i do it.
var del = require('del') ;
gulp.task('delete-build-folder', function(cb) {
return del([config.buildDir], cb);
});
gulp.task('default', ['delete-build-folder'], function() {
console.log('building.....');
gulp.start('copy-html', 'copy-html-views');
});

JHipster - Some CSS files did not appear in vendor file made by Gulp

I have this in main CSS:
And My vendor is this:
Why when I run Production mode material-icons.css and md-data-table.css did not appear?
Gulpfile.js
// Generated on 2016-04-23 using generator-jhipster 3.0.0
'use strict';
var gulp = require('gulp'),
rev = require('gulp-rev'),
templateCache = require('gulp-angular-templatecache'),
htmlmin = require('gulp-htmlmin'),
imagemin = require('gulp-imagemin'),
ngConstant = require('gulp-ng-constant-fork'),
eslint = require('gulp-eslint'),
es = require('event-stream'),
flatten = require('gulp-flatten'),
del = require('del'),
wiredep = require('wiredep').stream,
runSequence = require('run-sequence'),
browserSync = require('browser-sync'),
KarmaServer = require('karma').Server,
plumber = require('gulp-plumber'),
changed = require('gulp-changed'),
gulpIf = require('gulp-if'),
inject = require('gulp-inject'),
angularFilesort = require('gulp-angular-filesort');
var handleErrors = require('./gulp/handleErrors'),
serve = require('./gulp/serve'),
util = require('./gulp/utils'),
build = require('./gulp/build');
var config = require('./gulp/config');
gulp.task('clean', function () {
return del([config.dist], { dot: true });
});
gulp.task('copy', function () {
return es.merge(
gulp.src(config.bower + 'bootstrap/fonts/*.*')
.pipe(plumber({errorHandler: handleErrors}))
.pipe(changed(config.dist + 'content/fonts/'))
.pipe(rev())
.pipe(gulp.dest(config.dist + 'content/fonts/'))
.pipe(rev.manifest(config.revManifest, {
base: config.dist,
merge: true
}))
.pipe(gulp.dest(config.dist)),
gulp.src(config.app + 'content/**/*.{woff,woff2,svg,ttf,eot,otf}')
.pipe(plumber({errorHandler: handleErrors}))
.pipe(changed(config.dist + 'content/fonts/'))
.pipe(flatten())
.pipe(rev())
.pipe(gulp.dest(config.dist + 'content/fonts/'))
.pipe(rev.manifest(config.revManifest, {
base: config.dist,
merge: true
}))
.pipe(gulp.dest(config.dist)),
gulp.src([config.app + 'robots.txt', config.app + 'favicon.ico', config.app + '.htaccess'], { dot: true })
.pipe(plumber({errorHandler: handleErrors}))
.pipe(changed(config.dist))
.pipe(gulp.dest(config.dist))
);
});
gulp.task('images', function () {
return gulp.src(config.app + 'content/images/**')
.pipe(plumber({errorHandler: handleErrors}))
.pipe(changed(config.dist + 'content/images'))
.pipe(imagemin({optimizationLevel: 5, progressive: true, interlaced: true}))
.pipe(rev())
.pipe(gulp.dest(config.dist + 'content/images'))
.pipe(rev.manifest(config.revManifest, {
base: config.dist,
merge: true
}))
.pipe(gulp.dest(config.dist))
.pipe(browserSync.reload({stream: true}));
});
gulp.task('styles', [], function () {
return gulp.src(config.app + 'content/css')
.pipe(browserSync.reload({stream: true}));
});
gulp.task('inject', function () {
return gulp.src(config.app + 'index.html')
.pipe(inject(gulp.src(config.app + 'app/**/*.js').pipe(angularFilesort()), {relative: true}))
.pipe(gulp.dest(config.app));
});
gulp.task('wiredep', ['wiredep:test', 'wiredep:app']);
gulp.task('wiredep:app', function () {
var stream = gulp.src(config.app + 'index.html')
.pipe(plumber({errorHandler: handleErrors}))
.pipe(wiredep({
exclude: [
/angular-i18n/, // localizations are loaded dynamically
'bower_components/bootstrap/dist/js/' // exclude bootstrap js files as we use ui-bootstrap
]
}))
.pipe(gulp.dest(config.app));
return stream;
});
gulp.task('wiredep:test', function () {
return gulp.src(config.test + 'karma.conf.js')
.pipe(plumber({errorHandler: handleErrors}))
.pipe(wiredep({
exclude: [
/angular-i18n/, // localizations are loaded dynamically
/angular-scenario/,
'bower_components/bootstrap/dist/js/' // exclude Bootstrap js files as we use ui-bootstrap
],
ignorePath: /\.\.\/\.\.\//, // remove ../../ from paths of injected JavaScript files
devDependencies: true,
fileTypes: {
js: {
block: /(([\s\t]*)\/\/\s*bower:*(\S*))(\n|\r|.)*?(\/\/\s*endbower)/gi,
detect: {
js: /'(.*\.js)'/gi
},
replace: {
js: '\'src/{{filePath}}\','
}
}
}
}))
.pipe(gulp.dest(config.test));
});
gulp.task('assets:prod', ['images', 'styles', 'html'], build);
gulp.task('html', function () {
return gulp.src(config.app + 'app/**/*.html')
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(templateCache({
module: 'registroApp',
root: 'app/',
moduleSystem: 'IIFE'
}))
.pipe(gulp.dest(config.tmp));
});
gulp.task('ngconstant:dev', function () {
return ngConstant({
dest: 'app.constants.js',
name: 'registroApp',
deps: false,
noFile: true,
interpolate: /\{%=(.+?)%\}/g,
wrap:
'(function () {\n' +
' "use strict";\n' +
' // DO NOT EDIT THIS FILE, EDIT THE GULP TASK NGCONSTANT SETTINGS INSTEAD WHICH GENERATES THIS FILE\n' +
' {%= __ngModule %}\n' +
'})();\n',
constants: {
ENV: 'dev',
VERSION: util.parseVersion()
}
})
.pipe(gulp.dest(config.app + 'app/'));
});
gulp.task('ngconstant:prod', function () {
return ngConstant({
dest: 'app.constants.js',
name: 'registroApp',
deps: false,
noFile: true,
interpolate: /\{%=(.+?)%\}/g,
wrap:
'(function () {\n' +
' "use strict";\n' +
' // DO NOT EDIT THIS FILE, EDIT THE GULP TASK NGCONSTANT SETTINGS INSTEAD WHICH GENERATES THIS FILE\n' +
' {%= __ngModule %}\n' +
'})();\n',
constants: {
ENV: 'prod',
VERSION: util.parseVersion()
}
})
.pipe(gulp.dest(config.app + 'app/'));
});
// check app for eslint errors
gulp.task('eslint', function () {
return gulp.src(['gulpfile.js', config.app + 'app/**/*.js'])
.pipe(plumber({errorHandler: handleErrors}))
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failOnError());
});
// check app for eslint errors anf fix some of them
gulp.task('eslint:fix', function () {
return gulp.src(config.app + 'app/**/*.js')
.pipe(plumber({errorHandler: handleErrors}))
.pipe(eslint({
fix: true
}))
.pipe(eslint.format())
.pipe(gulpIf(util.isLintFixed, gulp.dest(config.app + 'app')));
});
gulp.task('test', ['wiredep:test', 'ngconstant:dev'], function (done) {
new KarmaServer({
configFile: __dirname + '/' + config.test + 'karma.conf.js',
singleRun: true
}, done).start();
});
gulp.task('watch', function () {
gulp.watch('bower.json', ['install']);
gulp.watch(['gulpfile.js', 'pom.xml'], ['ngconstant:dev']);
gulp.watch(config.app + 'content/css/**/*.css', ['styles']);
gulp.watch(config.app + 'content/images/**', ['images']);
gulp.watch(config.app + 'app/**/*.js', ['inject']);
gulp.watch([config.app + '*.html', config.app + 'app/**', config.app + 'i18n/**']).on('change', browserSync.reload);
});
gulp.task('install', function () {
runSequence(['wiredep', 'ngconstant:dev'], 'inject');
});
gulp.task('serve', function () {
runSequence('install', serve);
});
gulp.task('build', ['clean'], function (cb) {
runSequence(['copy', 'wiredep:app', 'ngconstant:prod'], 'inject', 'assets:prod', cb);
});
gulp.task('default', ['serve']);
Build.js (For gulp task)
var fs = require('fs'),
gulp = require('gulp'),
lazypipe = require('lazypipe'),
footer = require('gulp-footer'),
sourcemaps = require('gulp-sourcemaps'),
rev = require('gulp-rev'),
htmlmin = require('gulp-htmlmin'),
ngAnnotate = require('gulp-ng-annotate'),
prefix = require('gulp-autoprefixer'),
cssnano = require('gulp-cssnano'),
uglify = require('gulp-uglify'),
useref = require("gulp-useref"),
revReplace = require("gulp-rev-replace")
plumber = require('gulp-plumber'),
gulpIf = require('gulp-if'),
handleErrors = require('./handleErrors');
var config = require('./config');
var initTask = lazypipe()
.pipe(sourcemaps.init)
.pipe(footer, ';');
var jsTask = lazypipe()
.pipe(ngAnnotate)
.pipe(uglify);
var cssTask = lazypipe()
.pipe(prefix)
.pipe(cssnano);
module.exports = function() {
var templates = fs.readFileSync(config.tmp + '/templates.js');
var manifest = gulp.src(config.revManifest);
return gulp.src([config.app + '**/*.html',
'!' + config.app + 'app/**/*.html',
'!' + config.app + 'swagger-ui/**/*',
'!' + config.bower + '**/*.html'])
.pipe(plumber({errorHandler: handleErrors}))
//init sourcemaps and prepend semicolon
.pipe(useref({}, initTask))
//append html templates
.pipe(gulpIf('**/app.js', footer(templates)))
.pipe(gulpIf('*.js', jsTask()))
.pipe(gulpIf('*.css', cssTask()))
.pipe(gulpIf('*.html', htmlmin({collapseWhitespace: true})))
.pipe(gulpIf('**/*.!(html)', rev()))
.pipe(revReplace({manifest}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.dist));
}
Bower.json
{
"version": "0.0.0",
"name": "registro",
"appPath": "src/main/webapp/",
"testPath": "src/test/javascript/spec",
"dependencies": {
"angular": "1.5.2",
"angular-aria": "1.5.2",
"angular-loading-bar": "0.9.0",
"angular-resource": "1.5.2",
"angular-sanitize": "1.5.2",
"angular-translate": "2.11.0",
"angular-translate-interpolation-messageformat": "2.11.0",
"angular-translate-loader-partial": "2.11.0",
"angular-translate-storage-cookie": "2.11.0",
"angular-ui-router": "0.2.18",
"angular-cache-buster": "0.4.3",
"angular-cookies": "1.5.2",
"angular-dynamic-locale": "0.1.30",
"angular-i18n": "1.5.2",
"angular-bootstrap": "1.2.5",
"bootstrap-ui-datetime-picker": "2.2.3",
"angular-material": "1.0.7",
"material-design-icons": "^2.2.3",
"ngstorage": "0.3.10",
"jquery": "2.2.2",
"json3": "3.3.2",
"modernizr": "3.3.1",
"ng-file-upload": "12.0.4",
"ngInfiniteScroll": "1.2.2",
"swagger-ui": "2.1.4",
"angular-material-data-table": "^0.10.8"
},
"devDependencies": {
"angular-mocks": "1.5.2",
"angular-scenario": "1.5.2"
},
"overrides": {
"angular": {
"dependencies": {
"jquery": "2.2.2"
}
},
"material-design-icons": {
"main": [
"iconfont/material-icons.css"
]
},
"messageformat": {
"main": [
"messageformat.js",
"locale/es.js",
"locale/en.js"
]
}
},
"resolutions": {
"angular": "1.5.2",
"angular-cookies": "1.5.2",
"angular-material": "1.0.7",
"jquery": "2.2.2"
}
}
Notes:
I used JHipster (generator-jhipster 3.0.0).
In Dev mode everything works good.
These *.css dependents are added automatically by Gulp.
The issue here was that some files do not have an initial comment, so cssnano did not separate it in lines.