Gulp Babel Issue Using Foundation - gulp

I'm using the latest version on Foundation 6.2 and have added Babel to the project. When I add new scripts to Concat and Uglify these don't work but in the Gulp file I comment out Babel the scripts work fine.
I've added my Gulp file below, babel is only needed for Foundations bower_compnents.
Any help would be very helpful.
Jemes
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var sassPaths = [
'bower_components/foundation-sites/scss',
'bower_components/motion-ui/src'
];
// Sass
gulp.task('sass', function() {
return gulp.src('src/scss/app.scss')
.pipe($.sass({
includePaths: sassPaths,
outputStyle: 'compressed'
})
.on('error', $.sass.logError))
.pipe($.autoprefixer({
browsers: ['last 2 versions', 'ie >= 9']
}))
.pipe(gulp.dest('css'));
});
// Scripts
gulp.task('scripts', function() {
return gulp.src(['src/js/assets/jquery.js', 'bower_components/foundation-sites/dist/foundation.js', 'src/js/assets/jquery-scrolltofixed.js', 'src/js/assets/app.js'])
.pipe($.babel())
.pipe($.concat('app.js'))
.pipe(gulp.dest('src/js/built/'))
.pipe($.rename({ suffix: '.min' }))
.pipe($.uglify({mangle:false}))
.pipe(gulp.dest('js/'))
.pipe($.notify({ message: 'Scripts task complete' }));
});
// Default task
gulp.task('default', ['sass','scripts'], function() {
// Watch .scss files
gulp.watch('src/scss/**/*.scss', ['sass']);
// Watch .js files
gulp.watch('src/js/**/*.js', ['scripts']);
});

Related

gulp-nunjucks-render is taking a lot of time to compile

I am using gulp-nunjucks-render for HTML templating.
The problem is, it is taking more than 3-4 minutes to compile. Even though I have only seven nunjucks templates.
'use strict';
var gulp = require('gulp'),
autoprefixer = require('gulp-autoprefixer'),
sass = require('gulp-sass'),
minifyCSS = require('gulp-minify-css'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
connect = require('gulp-connect'),
sourcemaps = require('gulp-sourcemaps'),
nunjucksRender = require('gulp-nunjucks-render'),
browserSync = require('browser-sync');
gulp.task('html', function(){
gulp.src('build/*.html')
.pipe(browserSync.reload({stream: true}))
});
gulp.task('nunjucksRender', function(){
gulp.src('sources/html/**/*.+(html|nunjucks)')
.pipe(nunjucksRender({
path: ['sources/html/']
}))
.pipe(gulp.dest('build/'))
.pipe(browserSync.reload({stream: true}))
});
gulp.task('css', function(){
gulp.src('sources/scss/main.scss')
.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'expanded'})
.on('error', sass.logError)
)
.pipe(autoprefixer({
browsers: ['last 50 versions']
}))
// .pipe(minifyCSS())
.pipe(sourcemaps.write(''))
.pipe(gulp.dest('build/css'))
.pipe(browserSync.reload({stream: true}))
});
gulp.task('js', function() {
gulp.src('sources/**/*.js',)
// concat pulls all our files together before minifying them
.pipe( concat('main.min.js') )
// .pipe(uglify())
.pipe(gulp.dest('build/js'))
.pipe(browserSync.reload({stream: true}))
});
gulp.task('watch', function(){
gulp.watch('build/*.html', ['html', 'nunjucksRender']);
gulp.watch('sources/**/*.nunjucks', ['nunjucksRender']);
gulp.watch('sources/scss/**/*.scss', ['css']);
gulp.watch('sources/js/**/*.js', ['js']);
});
gulp.task('browserSync', function(){
browserSync({
server: {
baseDir: 'build'
}
});
});
gulp.task('default', ['css', 'html']);
gulp.task('start', ['browserSync', 'watch']);
Here is how it looks like when compilation is finished
Is there any gulp plugin to reduce compile time or compile only those files which are changed?
here is the github repo in case you need to see full file structure.
Ah.. I was watching .html and .nunjucks files for changes.. so when .nunjucks file compiles and creates .html file then again .html related task runs.

Gulp not creating js sourcemaps file

for some reason gulp not creating source maps file for concated and uglified js.
I tried different options and setups but with no luck.
I was working at one moment but than it stop working. Not sure why.
Here is the gulp task:
'use strict';
var gulp = require('gulp'),
sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer'),
sourcemaps = require('gulp-sourcemaps'),
clean = require('gulp-clean'),
cssmin = require('gulp-cssmin'),
rename = require('gulp-rename'),
runSequence = require('run-sequence'),
concat = require('gulp-concat'),
uglify = require('gulp-uglifyjs');
// Configure sass
gulp.task('sass', () => {
return gulp.src('sass/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'expanded'
}).on('error', sass.logError))
.pipe(sourcemaps.write('sourceMap'))
.pipe(gulp.dest('web/css'))
});
// Configure autoprefixer
gulp.task('autoprefixer', () => {
gulp.src(['web/css/**/*.css'])
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(gulp.dest('web/css/'))
});
// Configure clean task
gulp.task('clean:css', () => {
gulp.src('web/css/*.css')
.pipe(clean())
});
// Configure css min
var cssMinifyLocation = ['web/css/*.css', '!web/css/*.min.css'];
gulp.task('css:min', () => {
return gulp.src(cssMinifyLocation)
.pipe(autoprefixer())
.pipe(cssmin())
.pipe(rename(
{suffix: '.min'}
))
.pipe(gulp.dest('web/css'))
});
// Configure jsconcat and jsmin
gulp.task('compilejs', function()
{
gulp.src(
[
'web/js/twigjs/**/*.js'
])
.pipe(sourcemaps.init())
.pipe(concat('scripts.js'))
.pipe(uglify())
.pipe(rename({
suffix: '.min'
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('web/js'));
});
// Configure watch
gulp.task('watch', () => {
gulp.watch('sass/**/*.scss', () => { runSequence('clean:css', 'sass',
'autoprefixer', ['css:min']) });
gulp.watch('web/js/twigjs/**/*.js', () => {runSequence('compilejs')});
});
// Run build task
gulp.task('build', (callback) => {
runSequence('clean:css', 'sass', 'autoprefixer',
['css:min'], callback);
});
Please advise, and provide feedback.
Or if you have suggestion what to do in some different way.
Try this code:
gulp.task('compilejs', function()
{
gulp.src(
[
'web/js/twigjs/**/*.js'
])
.pipe(sourcemaps.init())
.pipe(concat('scripts.js'))
.pipe(uglify())
.pipe(rename({
suffix: '.min'
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('web/js'));
});

Gulp Sass is not compiling CSS file

I just started a new project but can't get my gulp sass up and running as its not compiling. I've used the same code for other projects but its now started to fail on me.
Below is my gulp.js file
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
var bourbon = require("node-bourbon").includePaths;
var neat = require("node-neat").includePaths;
var cleanCSS = require('gulp-clean-css');
// Static Server + watching scss/html files
gulp.task('serve', ['sass'], function() {
browserSync.init({
server: "./app"
});
gulp.watch("app/scss/**/*.scss", ['sass']);
gulp.watch("app/*.html").on('change', browserSync.reload);
// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
return gulp.src("app/scss/*.scss")
.pipe(sass({
includePaths: bourbon,
includePaths: neat
}))
.pipe(gulp.dest("app/css"))
.pipe(browserSync.stream());
});
// Minify main.css file
gulp.task('minify-css', function() {
return gulp.src('app/css/main.css')
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(gulp.dest('minified-css'));
});
gulp.task('default', ['serve']);
Also what strategies should I use to debug this issue.
.pipe(sass(your options).on("error", sass.logError))
I note that you are watching and gulp.src'ing in two different ways:
gulp.watch("app/scss/**/*.scss", ['sass']);
return gulp.src("app/scss/*.scss")
What happens if you change the .src() to the same as the .watch??

Compile gulp sass into the serve task

I make this gulp tasks: sass, serve, watch, but I'm trying to run gulp sass when the gulp watch into the gulp serve is activate but it's not work.
I need when I do a change into the sass file, this compile and show in realtime in the browser.
Any help ?
var gulp = require('gulp'),
browserSync = require('browser-sync'),
postcss = require('gulp-postcss'),
autoprefixer = require('autoprefixer'),
sass = require('gulp-sass');
gulp.task('serve', function() {
browserSync.init({
notify: false,
port: 3005,
server: {
baseDir: ["app"],
routes: {
'/bower_components': 'bower_components'
}
}
})
gulp.watch(['app/**/*.*']).on('change', browserSync.reload);
});
gulp.task('sass', function() {
var processors = [
autoprefixer({ browsers: ['last 2 versions']})
];
return gulp.src('./app/sass/*.scss')
.pipe(sass().on('error',sass.logError))
.pipe(postcss(processors))
.pipe(gulp.dest('./app/css'));
});
gulp.task('watch', ['sass'], function() {
gulp.watch('./app/sass/*.scss', ['sass']);
});
gulp.task('default', ['sass']);
Adding .pipe(browserSync.reload({stream: true})) to your sass task pipe should make browsersync reload only the compiled css.
Here's an example of your gulp task
gulp.task('sass', function() {
var processors = [
autoprefixer({ browsers: ['last 2 versions']})
];
return gulp.src('./app/sass/*.scss')
.pipe(sass().on('error',sass.logError))
.pipe(postcss(processors))
.pipe(gulp.dest('./app/css'))
.pipe(browserSync.reload({stream: true}));
});

gulp-connect doesn't work on gulp watch

I've just started to learn gulp and I'm trying to use gulp-connect https://www.npmjs.com/package/gulp-connect.
This is my files structure
And it's my gulpfile.js file
var gulp = require('gulp'),
concatCss = require('gulp-concat-css'),
minifyCss = require('gulp-minify-css'),
rename = require('gulp-rename'),
notify = require('gulp-notify'),
autoprefixer = require('gulp-autoprefixer'),
livereload = require('gulp-livereload'),
connect = require('gulp-connect');
// server connect
gulp.task('connect', function() {
connect.server({
root: [__dirname],
livereload: true
});
});
// css
gulp.task('css', function () {
return gulp.src('css/*.css')
/*.pipe(notify('Done!'))*/
.pipe(autoprefixer({
browsers: ['> 1%', 'IE 7']
}))
.pipe(concatCss("style.css"))
.pipe(minifyCss())
.pipe(rename('style.min.css'))
.pipe(gulp.dest('dist/'))
.pipe(connect.reload());
});
//html
gulp.task('html', function(){
gulp.src('index.html')
.pipe(connect.reload());
});
// watch
gulp.task('watch', function () {
gulp.watch('css/*.css', ['css']);
gulp.watch('index.html', ['html']);
});
// default
gulp.task('default', ['connect', 'html', 'css', 'watch']);
I don't undestand why all works correctly when I use command gulp in node.js console, but if I try to use command gulp watch I get an error in browser console
http://localhost:8080/ net::ERR_CONNECTION_REFUSED
What did I do wrong?
You're not running 'connect' task when running 'watch'.
Reformat your tasks like this
gulp.task('watch:assets', function () {
gulp.watch('css/*.css', ['css']);
gulp.watch('index.html', ['html']);
});
gulp.task('default', ['connect', 'html', 'css', 'watch:assets']);
gulp.task('watch', ['connect', 'watch:assets']);
Go back to terminal and run
gulp watch