Gulp Imagemin plugin - EROFS: read-olny file system, mkdir '/dist' - gulp

I'm using Gulp (CLI: 2.3.0; Local: 4.0.2) for my workflow. I have added to gulpfile imagemin function for images reduction, but when I want to run my task it throws error:
-> gulp
[10:56:11] Using gulpfile ~/dev/adm-servis-landing-page/gulpfile.js
[10:56:11] Starting 'default'...
[10:56:11] Starting 'compileSCSS'...
[10:56:11] Finished 'compileSCSS' after 396 ms
[10:56:11] Starting 'minifyJS'...
[10:56:11] Finished 'minifyJS' after 51 ms
[10:56:11] Starting 'imageOptimze'...
[10:56:12] 'imageOptimze' errored after 603 ms
[10:56:12] Error: EROFS: read-only file system, mkdir '/dist'
[10:56:12] 'default' errored after 1.07 s
Interesting is, that other gulp tasks can create /dist folder and can write into it, imagemin function can not. I have already tried everything, what I found on stackoverflow, but none of these things work for me.
Anyone have some solution to this?
OS: Mac OS Big Sur 11.4
npm: 7.20.2
Node: 14.7.1
package.json:
"devDependencies": {
"browser-sync": "^2.27.5",
"gulp": "^4.0.2",
"gulp-autoprefixer": "^8.0.0",
"gulp-clean-css": "^4.3.0",
"gulp-imagemin": "^7.1.0",
"gulp-sass": "^5.0.0",
"gulp-terser": "^2.0.1",
"gulp-webp": "^4.0.1",
"sass": "^1.36.0"
}
gulpfile:
'use strict';
const { src, series, dest, watch } = require('gulp');
const browsersync = require('browser-sync').create();
const sass = require('gulp-sass')(require('sass'));
const prefix = require('gulp-autoprefixer');
const minify = require('gulp-clean-css');
const terser = require('gulp-terser');
const imagemin = require('gulp-imagemin');
const imagewebp = require('gulp-webp');
const browserSync = require('browser-sync');
function compileSCSS() {
return src('src/scss/main.scss')
.pipe(sass())
.pipe(prefix())
.pipe(minify())
.pipe(dest('dist/css'));
}
function minifyJS() {
return src('src/js/*.js').pipe(terser()).pipe(dest('dist/js'));
}
function imageOptimze() {
return src('src/img/*.{jpg, png}')
.pipe(
imagemin([
imagemin.mozjpeg({ quality: 80, progressive: true }),
imagemin.optipng({ optimizationLevel: 2 }),
])
)
.pipe(dest('/dist/images'));
}
function webpImage() {
return src('dist/images/*.{jpg, png}')
.pipe(imagewebp())
.pipe(dest('/dist/images'));
}
function browsersyncServe(cb) {
browsersync.init({
server: {
baseDir: '.',
},
});
cb();
}
function browsersyncReload(cb) {
browsersync.reload();
cb();
}
function watchTask() {
watch('*.html', browsersyncReload);
watch(
['src/scss/**/*.scss', 'src/js/**/*.js'],
series(compileSCSS, minifyJS, browsersyncReload)
);
watch('src/img/*.{jpg,png}', series(imageOptimze, browsersyncReload));
watch('dist/images/*.{jpg,png}', series(webpImage, browsersyncReload));
}
// function taskWatch() {
// watch('/src/*.html', browsersyncReload);
// }
exports.default = series(
compileSCSS,
minifyJS,
imageOptimze,
webpImage,
browsersyncServe,
watchTask
);

After hour I have found the problem.
In my imagemin function I had:
.pipe(dest('/dist/images'));
And solution is, that I have removed / before dist/images, so with this it works:
.pipe(dest('dist/images'));

Related

Migration from gulp v3 to v4 [duplicate]

I'm trying to run the command below but unfortunately I run into errors.
$ gulp build
In my terminal and I get this assertion error. I've uninstalled node and NPM and reinstalled again using brew - How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X) with these steps. My node version is v10.5.0 and npm version is 6.1.0.
My system is MacOS High Sierra 10.13.2
assert.js:269
throw err;
^
AssertionError [ERR_ASSERTION]: Task function must be specified
at Gulp.set [as _setTask] (bulkit/startup-kit/node_modules/undertaker/lib/set-task.js:10:3)
at Gulp.task (startup-kit/node_modules/undertaker/lib/task.js:13:8)
at Object.<anonymous>
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Module.require (internal/modules/cjs/loader.js:650:17)
at require (internal/modules/cjs/helpers.js:20:18)
package.json
{
"name": "bulkit-startup",
"version": "0.0.1",
"description": "Bulkit Startup Kit",
"main": "Gruntfile.js",
"devDependencies": {
"autoprefixer": "^6.3.6",
"browser-sync": "^2.24.5",
"gulp": "^4.0.0",
"gulp-clean": "^0.3.2",
"gulp-concat": "^2.6.0",
"gulp-postcss": "^6.1.0",
"gulp-sass": "^2.2.0",
"gulp-sourcemaps": "^1.6.0",
"jquery": "^3.3.1",
"mq4-hover-shim": "^0.3.0",
"panini": "^1.3.0",
"rimraf": "^2.5.2"
},
"engines": {
"node": ">=0.10.1"
},
"scripts": {
"start": "gulp",
"build": "gulp build"
},
"repository": {
"type": "git",
"url": "https://github.com/cssninjaStudio/bulkit.git"
},
"bugs": {
"url": "https://github.com/cssninjaStudio/bulkit/issues",
"email": "support#cssninja.io"
},
"author": "Css Ninja <hello#cssninja.io> (https://cssninja.io/themes/bulkit)",
"license": "Commercial",
"private": true,
"dependencies": {
"bulma": "^0.7.0",
"del": "^3.0.0",
"jquery-waypoints": "^2.0.5",
"jquery.counterup": "^2.1.0",
"scrollreveal": "^3.4.0",
"slick-carousel": "^1.8.1",
"wallop": "^2.4.1"
}
}
gulpfile.js
var gulp = require('gulp');
var clean = require('gulp-clean');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var mq4HoverShim = require('mq4-hover-shim');
var rimraf = require('rimraf').sync;
var browser = require('browser-sync');
var panini = require('panini');
var concat = require('gulp-concat');
var port = process.env.SERVER_PORT || 8080;
var nodepath = 'node_modules/';
var assetspath = 'assets/';
// Starts a BrowerSync instance
gulp.task('server', ['build'], function(){
browser.init({server: './_site', port: port});
});
// Watch files for changes
gulp.task('watch', function() {
gulp.watch('scss/**/*', ['compile-scss', browser.reload]);
gulp.watch('sass/**/*', ['compile-sass', browser.reload]);
gulp.watch('js/**/*', ['copy-js', browser.reload]);
gulp.watch('images/**/*', ['copy-images', browser.reload]);
gulp.watch('html/pages/**/*', ['compile-html']);
gulp.watch(['html/{layouts,includes,helpers,data}/**/*'], ['compile-html:reset','compile-html']);
gulp.watch(['./src/{layouts,partials,helpers,data}/**/*'], [panini.refresh]);
});
// Erases the dist folder
gulp.task('reset', function() {
rimraf('bulma/*');
rimraf('scss/*');
rimraf('assets/css/*');
rimraf('assets/fonts/*');
rimraf('images/*');
});
// Erases the dist folder
gulp.task('clean', function() {
rimraf('_site');
});
// Copy Bulma filed into Bulma development folder
gulp.task('setupBulma', function() {
//Get Bulma from node modules
gulp.src([nodepath + 'bulma/*.sass']).pipe(gulp.dest('bulma/'));
gulp.src([nodepath + 'bulma/**/*.sass']).pipe(gulp.dest('bulma/'));
});
// Copy static assets
gulp.task('copy', function() {
//Copy other external font and data assets
gulp.src(['assets/fonts/**/*']).pipe(gulp.dest('_site/assets/fonts/'));
gulp.src([nodepath + 'slick-carousel/slick/fonts/**/*']).pipe(gulp.dest('_site/assets/css/fonts/'));
gulp.src([nodepath + 'slick-carousel/slick/ajax-loader.gif']).pipe(gulp.dest('_site/assets/css/'));
});
//Theme Sass variables
var sassOptions = {
errLogToConsole: true,
outputStyle: 'compressed',
includePaths: [nodepath + 'bulma/sass']
};
//Theme Scss variables
var scssOptions = {
errLogToConsole: true,
outputStyle: 'compressed',
includePaths: ['./scss/partials']
};
// Compile Bulma Sass
gulp.task('compile-sass', function () {
var processors = [
mq4HoverShim.postprocessorFor({ hoverSelectorPrefix: '.is-true-hover ' }),
autoprefixer({
browsers: [
"Chrome >= 45",
"Firefox ESR",
"Edge >= 12",
"Explorer >= 10",
"iOS >= 9",
"Safari >= 9",
"Android >= 4.4",
"Opera >= 30"
]
})//,
//cssnano(),
];
//Watch me get Sassy
return gulp.src('./bulma/bulma.sass')
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(postcss(processors))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./_site/assets/css/'));
});
// Compile Theme Scss
gulp.task('compile-scss', function () {
var processors = [
mq4HoverShim.postprocessorFor({ hoverSelectorPrefix: '.is-true-hover ' }),
autoprefixer({
browsers: [
"Chrome >= 45",
"Firefox ESR",
"Edge >= 12",
"Explorer >= 10",
"iOS >= 9",
"Safari >= 9",
"Android >= 4.4",
"Opera >= 30"
]
})//,
//cssnano(),
];
//Watch me get Sassy
return gulp.src('./scss/core.scss')
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(postcss(processors))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./_site/assets/css/'));
});
// Compile Html
gulp.task('compile-html', function() {
gulp.src('html/pages/**/*.html')
.pipe(panini({
root: 'html/pages/',
layouts: 'html/layouts/',
partials: 'html/includes/',
helpers: 'html/helpers/',
data: 'html/data/'
}))
.pipe(gulp.dest('_site'))
.on('finish', browser.reload);
});
gulp.task('compile-html:reset', function(done) {
panini.refresh();
done();
});
// Compile css from node modules
gulp.task('compile-css', function() {
return gulp.src([
nodepath + 'slick-carousel/slick/slick.css',
nodepath + 'slick-carousel/slick/slick-theme.css',
nodepath + 'wallop/css/wallop.css',
//Additional static css assets
assetspath + 'css/icons.min.css',
])
.pipe(concat('app.css'))
.pipe(gulp.dest('./_site/assets/css/'));
});
// Compile js from node modules
gulp.task('compile-js', function() {
return gulp.src([
nodepath + 'jquery/dist/jquery.min.js',
nodepath + 'slick-carousel/slick/slick.min.js',
nodepath + 'scrollreveal/dist/scrollreveal.min.js',
nodepath + 'waypoints/lib/jquery.waypoints.min.js',
nodepath + 'waypoints/lib/shortcuts/sticky.min.js',
nodepath + 'jquery.counterup/jquery.counterup.min.js',
nodepath + 'wallop/js/Wallop.min.js',
//Additional static js assets
assetspath + 'js/ggpopover/ggpopover.min.js',
assetspath + 'js/ggpopover/ggtooltip.js',
assetspath + 'js/embed/embed.js',
assetspath + 'js/gmap/gmap.min.js',
])
.pipe(concat('app.js'))
.pipe(gulp.dest('./_site/assets/js/'));
});
//Copy Theme js to production site
gulp.task('copy-js', function() {
gulp.src('js/**/*.js')
.pipe(gulp.dest('./_site/assets/js/'));
});
//Copy images to production site
gulp.task('copy-images', function() {
gulp.src('images/**/*')
.pipe(gulp.dest('./_site/assets/images/'));
});
gulp.task('init', ['setupBulma']);
gulp.task('build', ['clean','copy', 'compile-js', 'compile-css', 'copy-js', 'compile-sass', 'compile-scss', 'compile-html', 'copy-images']);
gulp.task('default', ['server', 'watch']);
Gulp 4.0 has changed the way that tasks should be defined if the task depends on another task to execute. The list parameter has been deprecated.
An example from your gulpfile.js would be:
// Starts a BrowerSync instance
gulp.task('server', ['build'], function(){
browser.init({server: './_site', port: port});
});
Instead of the list parameter they have introduced gulp.series() and gulp.parallel().
This task should be changed to something like this:
// Starts a BrowerSync instance
gulp.task('server', gulp.series('build', function(){
browser.init({server: './_site', port: port});
}));
I'm not an expert in this. You can see a more robust example in the gulp documentation for running tasks in series or these following excellent blog posts by Jhey Thompkins and Stefan Baumgartner
https://codeburst.io/switching-to-gulp-4-0-271ae63530c0
https://fettblog.eu/gulp-4-parallel-and-series/
Try replacing your last line of gulpfile.js
gulp.task('default', ['server', 'watch']);
with
gulp.task('default', gulp.series('server', 'watch'));
Lower your gulp version in package.json file to 3.9.1-
"gulp": "^3.9.1",
You don't need to downgrade your gulp from gulp 4. Use gulp.series() to combine multiple tasks. At first install gulp globally with
npm install --global gulp-cli
and then install locally on your working directory with
npm install --save-dev gulp
see details here
Example:
package.json
{
"name": "gulp-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"browser-sync": "^2.26.3",
"gulp": "^4.0.0",
"gulp-sass": "^4.0.2"
},
"dependencies": {
"bootstrap": "^4.3.1",
"jquery": "^3.3.1",
"popper.js": "^1.14.7"
}
}
gulpfile.js
var gulp = require("gulp");
var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();
// Specific Task
function js() {
return gulp
.src(['node_modules/bootstrap/dist/js/bootstrap.min.js', 'node_modules/jquery/dist/jquery.min.js', 'node_modules/popper.js/dist/umd/popper.min.js'])
.pipe(gulp.dest('src/js'))
.pipe(browserSync.stream());
}
gulp.task(js);
// Specific Task
function gulpSass() {
return gulp
.src(['src/scss/*.scss'])
.pipe(sass())
.pipe(gulp.dest('src/css'))
.pipe(browserSync.stream());
}
gulp.task(gulpSass);
// Run multiple tasks
gulp.task('start', gulp.series(js, gulpSass));
Run gulp start to fire multiple tasks & run gulp js or gulp gulpSass for specific task.
https://fettblog.eu/gulp-4-parallel-and-series/
Because
gulp.task(name, deps, func) was replaced by gulp.task(name, gulp.{series|parallel}(deps, func)).
You are using the latest version of gulp but older code. Modify the code or downgrade.
I get the same error when using Gulp. The solution is to switch to Gulp version 3.9.1, both for the local version and the CLI version.
sudo npm install -g gulp#3.9.1
Run in the project's folder
npm install gulp#3.9.1
The problem is that you are using gulp 4 and the syntax in gulfile.js is of gulp 3. So either downgrade your gulp to 3.x.x or make use of gulp 4 syntaxes.
Syntax Gulp 3:
gulp.task('default', ['sass'], function() {....} );
Syntax Gulp 4:
gulp.task('default', gulp.series(sass), function() {....} );
You can read more about gulp and gulp tasks on:
https://medium.com/#sudoanushil/how-to-write-gulp-tasks-ce1b1b7a7e81
Replace the array in:
gulp.task('default', ['task1', 'task2','task3']);
with gulp.series()
gulp.task('default', gulp.series('task1', 'task2','task3'));
It's not good to keep changing the gulp & npm versions in-order to fix the errors. I was getting several exceptions last days after reinstall my working machine. And wasted tons of minutes to re-install & fixing those.
So, I decided to upgrade all to latest versions:
npm -v : v12.13.0
node -v : 6.13.0
gulp -v : CLI version: 2.2.0 Local version: 4.0.2
This error is getting because of the how it has coded in you gulpfile but not the version mismatch.
So, Here you have to change 2 things in the gulpfile to aligned with Gulp version 4.
Gulp 4 has changed how initiate the task than Version 3.
In version 4, you have to defined the task as a function, before call it as a gulp task by it's string name. In V3:
gulp.task('serve', ['sass'], function() {..});
But in V4 it should be like:
function serve() {
...
}
gulp.task('serve', gulp.series(sass));
As #Arthur has mentioned, you need to change the way of passing arguments to the task function. It was like this in V3:
gulp.task('serve', ['sass'], function() {
...
});
But in V4, it should be:
gulp.task('serve', gulp.series(sass));
While migrating from Gulp 3.x to Gulp 4.x, these are the common errors we are getting:
Errors:
AssertionError [ERR_ASSERTION]: Task function must be specified
AssertionError [ERR_ASSERTION]: Task never defined:
The following tasks did not complete: Did you forget to signal async completion?
Here are few points to take care regarding task definition and execution:
First define task
Use return
Then execute with gulp.series()
Here, is the quick Gulp code which is working with Gulp v4:
// First define task with Return
gulp.task('css-minify', function () {
return gulp.src('assets/images/*.*')
.pipe(smushit({verbose: true}))
.pipe(gulp.dest('assets/images'));
});
// Then execute task with bracket '()' instead of '[]'
gulp.task('default', gulp.series('css-minify'));
Replacing [] with gulp.series() definitely worked to me, and another change I've needed to do was to move my main task 'Ex. gulp main-task' to the end of the Gulp file using Gulp v4.02
worked after updating gulp.task() with gulp.task( gulp.series() );
Steps:
"gulp": "^3.9.1",
npm install
gulp styles

Gulp 3 => Gulp 4 problems

I have converted a Gulpfile over to Gulp 4 but i'm having issues with it.
When I run gulp it runs fine and compiles the styles and uploads them but then says "Connection Closed" and doesn't do anything when I modify a file.
Anyone have any ideas to how I can get it to continue to watch the files and rerun the compilation?
(() => {
'use strict';
const gulp = require('gulp');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const concat = require('gulp-concat');
const autoprefixer = require('gulp-autoprefixer');
const plumber = require('gulp-plumber');
const gutil = require('gulp-util');
const sftp = require('gulp-sftp');
const path = require('path');
const notify = require('gulp-notify');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
gulp.task('sass2css', () => {
return (
gulp
.src('src/sass/**/*.{scss,sass}')
.pipe(plumber({
errorHandler: function (err) {
console.log(err);
this.emit('end');
}
}))
.pipe(sourcemaps.init())
.pipe(sass({
indentedSyntax: false,
outputStyle: 'nested'
}))
// .pipe(concat('application.css'))
.pipe(autoprefixer({
browsers: ['last 3 versions']
}))
.pipe(gulp.dest('dist/styles'))
)
})
gulp.task('deployCSS', () => {
return gulp.src('dist/styles/*.css')
.pipe(sftp({
host: 'xxxxxxxxx',
auth: 'xxxxxxx',
remotePath: 'xxxxx/xxxxx/xxxxx/assets/css'
}));
})
// gulp.task('watch', function() {
// gulp.watch('src/sass/**/*.{scss,sass}', gulp.series('sass'));
// gulp.watch('dist/styles/**/*.css', gulp.series('deployCSS'));
// })
// gulp.task('default', gulp.parallel('sass', 'watch'));
gulp.task('watch', (done) => {
gulp.watch('src/sass/**/*.{scss,sass}', gulp.series('sass2css'));
gulp.watch('dist/styles/**/*.css', gulp.series('deployCSS'));
done();
})
exports.default = gulp.series('sass2css', 'deployCSS', 'watch');
function errorHandler (error) {
console.log(error.toString());
this.emit('end');
}
})();
This is what my terminal looks like when run once:
[vagrant#precise64] ➜ myproject git:(master) ✗ gulp
[09:07:59] Using gulpfile /vagrant/myproject/gulpfile.js
[09:07:59] Starting 'default'...
[09:07:59] Starting 'sass2css'...
[09:08:02] Finished 'sass2css' after 2.89 s
[09:08:02] Starting 'deployCSS'...
[09:08:02] Authenticating with password.
[09:08:06] gulp-sftp: Uploaded: application.css => xxxxx/xxxxx/xxxxx/assets/css/application.css
[09:08:06] gulp-sftp: 1 file uploaded successfully
[09:08:06] Finished 'deployCSS' after 3.28 s
[09:08:06] Starting 'watch'...
[09:08:06] Finished 'watch' after 34 ms
[09:08:06] Finished 'default' after 6.22 s
[09:08:06] SFTP :: SFTP session closed
[09:08:06] Connection :: end
[09:08:06] Connection :: closed
gulp.series() returns a function so you need to add () after your calls. Give this a try:
gulp.task('watch', (done) => {
gulp.watch('src/sass/**/*.{scss,sass}').on('change', function (){
gulp.series('sass2css')();
});
gulp.watch('dist/styles/**/*.css').on('change', function (){
gulp.series('deployCSS')();
});
done();
});

Everytime I run gulp anything, I get a assertion error. - Task function must be specified

I'm trying to run the command below but unfortunately I run into errors.
$ gulp build
In my terminal and I get this assertion error. I've uninstalled node and NPM and reinstalled again using brew - How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X) with these steps. My node version is v10.5.0 and npm version is 6.1.0.
My system is MacOS High Sierra 10.13.2
assert.js:269
throw err;
^
AssertionError [ERR_ASSERTION]: Task function must be specified
at Gulp.set [as _setTask] (bulkit/startup-kit/node_modules/undertaker/lib/set-task.js:10:3)
at Gulp.task (startup-kit/node_modules/undertaker/lib/task.js:13:8)
at Object.<anonymous>
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Module.require (internal/modules/cjs/loader.js:650:17)
at require (internal/modules/cjs/helpers.js:20:18)
package.json
{
"name": "bulkit-startup",
"version": "0.0.1",
"description": "Bulkit Startup Kit",
"main": "Gruntfile.js",
"devDependencies": {
"autoprefixer": "^6.3.6",
"browser-sync": "^2.24.5",
"gulp": "^4.0.0",
"gulp-clean": "^0.3.2",
"gulp-concat": "^2.6.0",
"gulp-postcss": "^6.1.0",
"gulp-sass": "^2.2.0",
"gulp-sourcemaps": "^1.6.0",
"jquery": "^3.3.1",
"mq4-hover-shim": "^0.3.0",
"panini": "^1.3.0",
"rimraf": "^2.5.2"
},
"engines": {
"node": ">=0.10.1"
},
"scripts": {
"start": "gulp",
"build": "gulp build"
},
"repository": {
"type": "git",
"url": "https://github.com/cssninjaStudio/bulkit.git"
},
"bugs": {
"url": "https://github.com/cssninjaStudio/bulkit/issues",
"email": "support#cssninja.io"
},
"author": "Css Ninja <hello#cssninja.io> (https://cssninja.io/themes/bulkit)",
"license": "Commercial",
"private": true,
"dependencies": {
"bulma": "^0.7.0",
"del": "^3.0.0",
"jquery-waypoints": "^2.0.5",
"jquery.counterup": "^2.1.0",
"scrollreveal": "^3.4.0",
"slick-carousel": "^1.8.1",
"wallop": "^2.4.1"
}
}
gulpfile.js
var gulp = require('gulp');
var clean = require('gulp-clean');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var mq4HoverShim = require('mq4-hover-shim');
var rimraf = require('rimraf').sync;
var browser = require('browser-sync');
var panini = require('panini');
var concat = require('gulp-concat');
var port = process.env.SERVER_PORT || 8080;
var nodepath = 'node_modules/';
var assetspath = 'assets/';
// Starts a BrowerSync instance
gulp.task('server', ['build'], function(){
browser.init({server: './_site', port: port});
});
// Watch files for changes
gulp.task('watch', function() {
gulp.watch('scss/**/*', ['compile-scss', browser.reload]);
gulp.watch('sass/**/*', ['compile-sass', browser.reload]);
gulp.watch('js/**/*', ['copy-js', browser.reload]);
gulp.watch('images/**/*', ['copy-images', browser.reload]);
gulp.watch('html/pages/**/*', ['compile-html']);
gulp.watch(['html/{layouts,includes,helpers,data}/**/*'], ['compile-html:reset','compile-html']);
gulp.watch(['./src/{layouts,partials,helpers,data}/**/*'], [panini.refresh]);
});
// Erases the dist folder
gulp.task('reset', function() {
rimraf('bulma/*');
rimraf('scss/*');
rimraf('assets/css/*');
rimraf('assets/fonts/*');
rimraf('images/*');
});
// Erases the dist folder
gulp.task('clean', function() {
rimraf('_site');
});
// Copy Bulma filed into Bulma development folder
gulp.task('setupBulma', function() {
//Get Bulma from node modules
gulp.src([nodepath + 'bulma/*.sass']).pipe(gulp.dest('bulma/'));
gulp.src([nodepath + 'bulma/**/*.sass']).pipe(gulp.dest('bulma/'));
});
// Copy static assets
gulp.task('copy', function() {
//Copy other external font and data assets
gulp.src(['assets/fonts/**/*']).pipe(gulp.dest('_site/assets/fonts/'));
gulp.src([nodepath + 'slick-carousel/slick/fonts/**/*']).pipe(gulp.dest('_site/assets/css/fonts/'));
gulp.src([nodepath + 'slick-carousel/slick/ajax-loader.gif']).pipe(gulp.dest('_site/assets/css/'));
});
//Theme Sass variables
var sassOptions = {
errLogToConsole: true,
outputStyle: 'compressed',
includePaths: [nodepath + 'bulma/sass']
};
//Theme Scss variables
var scssOptions = {
errLogToConsole: true,
outputStyle: 'compressed',
includePaths: ['./scss/partials']
};
// Compile Bulma Sass
gulp.task('compile-sass', function () {
var processors = [
mq4HoverShim.postprocessorFor({ hoverSelectorPrefix: '.is-true-hover ' }),
autoprefixer({
browsers: [
"Chrome >= 45",
"Firefox ESR",
"Edge >= 12",
"Explorer >= 10",
"iOS >= 9",
"Safari >= 9",
"Android >= 4.4",
"Opera >= 30"
]
})//,
//cssnano(),
];
//Watch me get Sassy
return gulp.src('./bulma/bulma.sass')
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(postcss(processors))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./_site/assets/css/'));
});
// Compile Theme Scss
gulp.task('compile-scss', function () {
var processors = [
mq4HoverShim.postprocessorFor({ hoverSelectorPrefix: '.is-true-hover ' }),
autoprefixer({
browsers: [
"Chrome >= 45",
"Firefox ESR",
"Edge >= 12",
"Explorer >= 10",
"iOS >= 9",
"Safari >= 9",
"Android >= 4.4",
"Opera >= 30"
]
})//,
//cssnano(),
];
//Watch me get Sassy
return gulp.src('./scss/core.scss')
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(postcss(processors))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./_site/assets/css/'));
});
// Compile Html
gulp.task('compile-html', function() {
gulp.src('html/pages/**/*.html')
.pipe(panini({
root: 'html/pages/',
layouts: 'html/layouts/',
partials: 'html/includes/',
helpers: 'html/helpers/',
data: 'html/data/'
}))
.pipe(gulp.dest('_site'))
.on('finish', browser.reload);
});
gulp.task('compile-html:reset', function(done) {
panini.refresh();
done();
});
// Compile css from node modules
gulp.task('compile-css', function() {
return gulp.src([
nodepath + 'slick-carousel/slick/slick.css',
nodepath + 'slick-carousel/slick/slick-theme.css',
nodepath + 'wallop/css/wallop.css',
//Additional static css assets
assetspath + 'css/icons.min.css',
])
.pipe(concat('app.css'))
.pipe(gulp.dest('./_site/assets/css/'));
});
// Compile js from node modules
gulp.task('compile-js', function() {
return gulp.src([
nodepath + 'jquery/dist/jquery.min.js',
nodepath + 'slick-carousel/slick/slick.min.js',
nodepath + 'scrollreveal/dist/scrollreveal.min.js',
nodepath + 'waypoints/lib/jquery.waypoints.min.js',
nodepath + 'waypoints/lib/shortcuts/sticky.min.js',
nodepath + 'jquery.counterup/jquery.counterup.min.js',
nodepath + 'wallop/js/Wallop.min.js',
//Additional static js assets
assetspath + 'js/ggpopover/ggpopover.min.js',
assetspath + 'js/ggpopover/ggtooltip.js',
assetspath + 'js/embed/embed.js',
assetspath + 'js/gmap/gmap.min.js',
])
.pipe(concat('app.js'))
.pipe(gulp.dest('./_site/assets/js/'));
});
//Copy Theme js to production site
gulp.task('copy-js', function() {
gulp.src('js/**/*.js')
.pipe(gulp.dest('./_site/assets/js/'));
});
//Copy images to production site
gulp.task('copy-images', function() {
gulp.src('images/**/*')
.pipe(gulp.dest('./_site/assets/images/'));
});
gulp.task('init', ['setupBulma']);
gulp.task('build', ['clean','copy', 'compile-js', 'compile-css', 'copy-js', 'compile-sass', 'compile-scss', 'compile-html', 'copy-images']);
gulp.task('default', ['server', 'watch']);
Gulp 4.0 has changed the way that tasks should be defined if the task depends on another task to execute. The list parameter has been deprecated.
An example from your gulpfile.js would be:
// Starts a BrowerSync instance
gulp.task('server', ['build'], function(){
browser.init({server: './_site', port: port});
});
Instead of the list parameter they have introduced gulp.series() and gulp.parallel().
This task should be changed to something like this:
// Starts a BrowerSync instance
gulp.task('server', gulp.series('build', function(){
browser.init({server: './_site', port: port});
}));
I'm not an expert in this. You can see a more robust example in the gulp documentation for running tasks in series or these following excellent blog posts by Jhey Thompkins and Stefan Baumgartner
https://codeburst.io/switching-to-gulp-4-0-271ae63530c0
https://fettblog.eu/gulp-4-parallel-and-series/
Try replacing your last line of gulpfile.js
gulp.task('default', ['server', 'watch']);
with
gulp.task('default', gulp.series('server', 'watch'));
Lower your gulp version in package.json file to 3.9.1-
"gulp": "^3.9.1",
You don't need to downgrade your gulp from gulp 4. Use gulp.series() to combine multiple tasks. At first install gulp globally with
npm install --global gulp-cli
and then install locally on your working directory with
npm install --save-dev gulp
see details here
Example:
package.json
{
"name": "gulp-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"browser-sync": "^2.26.3",
"gulp": "^4.0.0",
"gulp-sass": "^4.0.2"
},
"dependencies": {
"bootstrap": "^4.3.1",
"jquery": "^3.3.1",
"popper.js": "^1.14.7"
}
}
gulpfile.js
var gulp = require("gulp");
var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();
// Specific Task
function js() {
return gulp
.src(['node_modules/bootstrap/dist/js/bootstrap.min.js', 'node_modules/jquery/dist/jquery.min.js', 'node_modules/popper.js/dist/umd/popper.min.js'])
.pipe(gulp.dest('src/js'))
.pipe(browserSync.stream());
}
gulp.task(js);
// Specific Task
function gulpSass() {
return gulp
.src(['src/scss/*.scss'])
.pipe(sass())
.pipe(gulp.dest('src/css'))
.pipe(browserSync.stream());
}
gulp.task(gulpSass);
// Run multiple tasks
gulp.task('start', gulp.series(js, gulpSass));
Run gulp start to fire multiple tasks & run gulp js or gulp gulpSass for specific task.
https://fettblog.eu/gulp-4-parallel-and-series/
Because
gulp.task(name, deps, func) was replaced by gulp.task(name, gulp.{series|parallel}(deps, func)).
You are using the latest version of gulp but older code. Modify the code or downgrade.
I get the same error when using Gulp. The solution is to switch to Gulp version 3.9.1, both for the local version and the CLI version.
sudo npm install -g gulp#3.9.1
Run in the project's folder
npm install gulp#3.9.1
The problem is that you are using gulp 4 and the syntax in gulfile.js is of gulp 3. So either downgrade your gulp to 3.x.x or make use of gulp 4 syntaxes.
Syntax Gulp 3:
gulp.task('default', ['sass'], function() {....} );
Syntax Gulp 4:
gulp.task('default', gulp.series(sass), function() {....} );
You can read more about gulp and gulp tasks on:
https://medium.com/#sudoanushil/how-to-write-gulp-tasks-ce1b1b7a7e81
Replace the array in:
gulp.task('default', ['task1', 'task2','task3']);
with gulp.series()
gulp.task('default', gulp.series('task1', 'task2','task3'));
It's not good to keep changing the gulp & npm versions in-order to fix the errors. I was getting several exceptions last days after reinstall my working machine. And wasted tons of minutes to re-install & fixing those.
So, I decided to upgrade all to latest versions:
npm -v : v12.13.0
node -v : 6.13.0
gulp -v : CLI version: 2.2.0 Local version: 4.0.2
This error is getting because of the how it has coded in you gulpfile but not the version mismatch.
So, Here you have to change 2 things in the gulpfile to aligned with Gulp version 4.
Gulp 4 has changed how initiate the task than Version 3.
In version 4, you have to defined the task as a function, before call it as a gulp task by it's string name. In V3:
gulp.task('serve', ['sass'], function() {..});
But in V4 it should be like:
function serve() {
...
}
gulp.task('serve', gulp.series(sass));
As #Arthur has mentioned, you need to change the way of passing arguments to the task function. It was like this in V3:
gulp.task('serve', ['sass'], function() {
...
});
But in V4, it should be:
gulp.task('serve', gulp.series(sass));
While migrating from Gulp 3.x to Gulp 4.x, these are the common errors we are getting:
Errors:
AssertionError [ERR_ASSERTION]: Task function must be specified
AssertionError [ERR_ASSERTION]: Task never defined:
The following tasks did not complete: Did you forget to signal async completion?
Here are few points to take care regarding task definition and execution:
First define task
Use return
Then execute with gulp.series()
Here, is the quick Gulp code which is working with Gulp v4:
// First define task with Return
gulp.task('css-minify', function () {
return gulp.src('assets/images/*.*')
.pipe(smushit({verbose: true}))
.pipe(gulp.dest('assets/images'));
});
// Then execute task with bracket '()' instead of '[]'
gulp.task('default', gulp.series('css-minify'));
Replacing [] with gulp.series() definitely worked to me, and another change I've needed to do was to move my main task 'Ex. gulp main-task' to the end of the Gulp file using Gulp v4.02
worked after updating gulp.task() with gulp.task( gulp.series() );
Steps:
"gulp": "^3.9.1",
npm install
gulp styles

Gulp build command gives me this error message

$ gulp build
[02:36:37] Using gulpfile C:\xampp\htdocs\melodic\gulpfile.js
[02:36:37] Starting 'build'...
[02:36:37] Starting 'clean:dist'...
[02:36:37] Finished 'clean:dist' after 4.63 ms
[02:36:37] Starting 'sass'...
[02:36:37] Starting 'useref'...
[02:36:37] Starting 'img'...
[02:36:37] Starting 'cleancss'...
[02:36:37] Finished 'cleancss' after 37 ms
C:\xampp\htdocs\melodic\node_modules\gulp-useref\node_modules\vinyl-
fs\lib\src\index.js:20
throw new Error('Invalid glob argument: ' + glob);
^
Error: Invalid glob argument:
at Object.src (C:\xampp\htdocs\melodic\node_modules\gulp-
useref\node_modules\vinyl-fs\lib\src\index.js:20:11)
at DestroyableTransform.addAssetsToStream
(C:\xampp\htdocs\melodic\node_modules\gulp-useref\index.js:62:15)
at C:\xampp\htdocs\melodic\node_modules\gulp-useref\index.js:124:31
at Array.forEach (native)
at DestroyableTransform.processAssets
(C:\xampp\htdocs\melodic\node_modules\gulp-useref\index.js:115:11)
at C:\xampp\htdocs\melodic\node_modules\gulp-useref\index.js:178:31
at Stream.<anonymous> (C:\xampp\htdocs\melodic\node_modules\gulp-
useref\node_modules\event-stream\index.js:318:20)
at _end (C:\xampp\htdocs\melodic\node_modules\through\index.js:65:9)
at Stream.stream.end
(C:\xampp\htdocs\melodic\node_modules\through\index.js:74:5)
at DestroyableTransform.onend
(C:\xampp\htdocs\melodic\node_modules\through2\node_modules\readable-
stream\lib\_stream_readable.js:577:10)
I have tried to remove the running tasks one by one but to no avail. What throws me off also is the fact that it seems to run through all the tasks and even finishing them and then crashing as the last task is finished. Thanks in advance.
EDIT: Here is my gulpfile as I forgot to include it.
var gulp = require('gulp');
// Requires the gulp-sass plugin
var sass = require('gulp-sass');
//Requires browser synch
var browserSync = require('browser-sync').create();
//Requires userref
var useref = require('gulp-useref');
//Requires uglify
var uglify = require('gulp-uglify');
var gulpIf = require('gulp-if');
//Requires cssnano
var cssnano = require('gulp-cssnano');
//Requires imagemin
var imagemin = require('gulp-imagemin');
//Requires cache
var cache = require('gulp-cache');
//Requires del
var del = require('del');
//Requires run-sequence
var runSequence = require('run-sequence');
//requires postcss
var postcss = require('gulp-postcss');
//ACTIVE COMMANDS
/*Run 'gulp browserSync' in commandline.
This task automatically reloads the broswer upon each save of document*/
gulp.task('browserSync', function() {
browserSync.init({
injectChanges: true,
server: {
baseDir: "./app"
},
})
});
/*Run 'gulp sass' in command line.
THIS TASK GETS ALL SCSS IMPORTED INTO THE app.scss FILE AND CONVERTS IT INTO app.css.
THIS TASK IS A ONE TIME CONVERSTION*/
gulp.task('sass', function() {
return gulp.src('app/scss/**/*.scss') // Gets all files ending with .scss in app/scss
.pipe(sass())
.pipe(gulp.dest('app/css'))
.pipe(browserSync.stream({match: '**/*.css'}));
});
/*Run 'gulp watch' in command line.
THIS TASK GETS ALL SCSS AND OTHER FILE TYPES AND CONVERTS THEM ACITVELY.
THIS TASK IS ONGOING*/
gulp.task('watch', ['browserSync', 'sass'], function (){
gulp.watch('app/scss/**/*.scss', ['sass']);
});
//PRODUCTION/FINAL COMMANDS
/*Run 'gulp useref' in command line.
THIS TASK GETS ALL CSS AND OTHER FILE TYPES AND JOINS DIFFERENT FILES AND MINIMIZES THEM IN THE DIST FOLDER.
*/
gulp.task('useref', function(){
return gulp.src('app/*.html')
.pipe(useref())
// Minifies only if it's a JavaScript file
.pipe(gulpIf('*.js', uglify()))
// Minifies only if it's a CSS file
.pipe(gulpIf('*.css', cssnano()))
.pipe(gulp.dest('dist'))
});
gulp.task('cleancss', function () {
return gulp.src('./src/*.css')
.pipe(postcss())
.pipe(gulp.dest('./dest'));
});
/*Run 'gulp img' in command line.
MINIMIZSES PHOTOS TO THE DIST FOLDER*/
gulp.task('img', function(){
return gulp.src('app/img/**/*.+(png|jpg|gif|svg)')
.pipe(imagemin({
// Setting interlaced to true
interlaced: true
}))
// Caching images that ran through imagemin
.pipe(cache(imagemin({
interlaced: true
})))
.pipe(gulp.dest('dist/img'))
});
/*Run 'gulp fonts' in command line.
CLEANS THE DIST DIRECTORY FOR UNUSED FILES*/
gulp.task('clean:dist', function() {
return del.sync('dist');
});
/*Run 'gulp fonts' in command line.
CLEANS THE IMAGE CACHE CREATED EARLIER*/
gulp.task('cache:clear', function (callback) {
return cache.clearAll(callback)
});
gulp.task('build', function (callback) {
runSequence('clean:dist',
['sass', 'useref', 'img', 'cleancss'],
callback
)
});
gulp.task('default', function (callback) {
runSequence(['sass','browserSync', 'watch'],
callback
)
});
EDIT: Included my gulpfile for further details.

Browsersync serve local files instead of remote files

So I want to connect browsersync to a site I don't have any control over. Basically I want it to replace a css file on the live site with a css file on my computer (which gets compiled from a bunch of less files). When that local css file is changed then browsersync should inject the css like it normally would. I'm using browsersync with gulp since I also want to have other tasks run.
The code below will open gulpjs.com and correctly match main.css from gulpjs.com and attempt to serve something in it's place. But all I'm getting is a 404 for the main.min.css. It's looking for it here
http://localhost:3000/css/main.min.css
I zipped up the test project (node modules not included) you can download it here - https://www.dropbox.com/s/5x5l6bbxlwthhoo/browsersyncTest.zip?dl=0
Here's what I have so far...
Project Structure
css/ main.min.css (compiled from app.less)
less/ app.less
gulpfile.js
package.json
package.json
{
"name": "browsersyncTest",
"devDependencies": {
"autoprefixer": "^6.7.0",
"browser-sync": "^2.18.6",
"cssnano": "^3.10.0",
"gulp": "^3.9.1",
"gulp-clean-css": "^2.3.2",
"gulp-less": "^3.3.0",
"gulp-postcss": "^6.3.0",
"gulp-rename": "^1.2.2",
"gulp-uglify": "^2.0.0",
"gulp-watch": "^4.3.11"
}
}
gulpfile.js
var gulp = require('gulp');
var less = require('gulp-less');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var cssnano = require('cssnano');
var cleancss = require('gulp-clean-css');
var rename = require('gulp-rename');
var watch = require('gulp-watch');
var browserSync = require('browser-sync').create();
gulp.task('default', function() {
});
gulp.task('less', function () {
var plugins = [autoprefixer({browsers: ['last 2 versions', 'ie >= 10']})];
return gulp.src('less/app.less')
.pipe(less())
.pipe(rename('main.min.css'))
//.pipe(postcss(plugins))
//.pipe(cleancss())
.pipe(gulp.dest('css'))
.pipe(browserSync.stream());
});
gulp.task('watch', ['less'], function () {
browserSync.init({
proxy: "http://gulpjs.com/",
files: ['css/**'],
serveStatic: ['css'],
rewriteRules: [{
match: new RegExp('css/main.css'),
fn: function() {
return 'css/main.min.css';
}
}]
});
gulp.watch('less/**/*.less', ['less']);
});
Thanks a bunch!
Got it all figured out.
Here's the updated code.
browserSync.init({
proxy: "http://gulpjs.com/",
rewriteRules: [
{
match: new RegExp("css/main.css"),
fn: function() {
return "main.min.css"
}
}
],
files: "css/*.css",
serveStatic: ['./css']
});