Exporting a task file to gulpfile.js and run on command line - gulp

(Pertains to Gulp 4.0/ES6)
I would like to create a task file that can be imported by main gulpfile.js and can also be run directly from the command line using gulp (in this case without even referencing the gulpfile.js file on command line). Is this possible?
I can create a gulp task file that can be run directly:
task-a.js:
const log = require('fancy-log');
function taskA(done){
// do stuff
log('Executing task A');
done();
}
exports.default = taskA;
Run from command line:
gulp -f task-a.js
[17:53:21] Using gulpfile C:\app\task-a.js
[17:53:21] Starting 'default'...
[17:53:21] Executing task A
[17:53:21] Finished 'default' after 2.73 ms
OK, looks good.
In order to import into gulpfile.js I need to modify the export in task-a.js to:
task-a.js:
// modified the exports for require()
module.exports taskA;
gulpfile.js:
const taskA = require('./task-a');
gulp.task('default', taskA);
gulp -f gulpfile.js
OK, this also works.
But is it possible to change the exports in task-a.js to allow for both import and to be run from command line using gulp?

One way to export the function in task-a.js as a task:
gulp.task('default', taskA);
module.exports = gulp.series('default');
This ensures that the gulpfile.js can import the function as a composed operation.

Related

Why is Gulp running the same task twice?

I am having a similar issue as the question posted here but none of the answers given there apply to my situation.
When I run the one and only task defined in my Gulpfile.js file it is getting executed twice.
I am using Gulp version 4.0.2
This is the contents of my Gulpfile.js file:
const { src, dest, watch, series, parallel } = require('gulp');
const sass = require('gulp-sass');
const rename = require('gulp-rename');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const files = {
bootstrapSrcPath: 'bootstrap-sass/bootstrap.scss',
bootstrapDstPath: 'Test'
};
exports.scssTask = series(
scssTaskFunc
);
function scssTaskFunc() {
return src(files.bootstrapSrcPath)
.pipe(sass({ style: 'expanded' }))
.pipe(dest(files.bootstrapDstPath))
.pipe(postcss([autoprefixer(), cssnano()]))
.pipe(rename({ suffix: '.min' }))
.pipe(dest(files.bootstrapDstPath));
}
This is the command I am running in the CMD prompt and the results:
C:\Users\myUser\source\repos\myProject> cmd.exe / c gulp - b "C:\Users\myUser\source\repos\myProject" --color--gulpfile "C:\Users\myUser\source\repos\myProject\Gulpfile.js" scssTask
[16: 10: 15]Using gulpfile ~\source\repos\myProject\Gulpfile.js
[16: 10: 15]Starting 'scssTask'...
[16: 10: 15] Starting 'scssTaskFunc'...
[16: 10: 17] Finished 'scssTaskFunc' after 1.7 s
[16: 10: 17]Finished 'scssTask' after 1.71 s
Process terminated with code 0.
It works and the output file is what I expect but it seems like a waste to do it twice.
This is the what gulp shows for tasks:
C:\Users\myUser\source\repos\myProject> gulp--tasks
[15: 57: 29]Tasks for ~\source\repos\myProject\Gulpfile.js
[15: 57: 29]└─┬ scssTask
[15: 57: 29]└─┬ <series>
[15:57:29] └── scssTaskFunc
Why is it running the task twice, once as the 'scssTask' and the second as the 'scssTaskFunc'?
Btw, this is my first attempt at gulp so I apologize if this is a derp question.
Your task isn't running twice, it just seems like it does because you're using gulp.series.
gulp.series and gulp.parallel are normally used to combine and compose tasks into larger operations. If, for example, you'd have a jsTaskFunc as well, you could create a task build like so:
exports.build = parallel(scssTaskFunc, jsTaskFunc);
and running gulp build would log something like this in your terminal:
[09:42:12] Starting 'build'...
[09:42:12] Starting 'scssTaskFunc'...
[09:42:12] Starting 'jsTaskFunc'...
[09:42:12] Finished 'scssTaskFunc' after 93 ms
[09:42:12] Finished 'jsTaskFunc' after 94 ms
[09:42:12] Finished 'build' after 111 ms
Something similar is happening now due to your use of gulp.series, because scssTask runs scssTaskFunc as a dependent task, but scssTask and scssTaskFunc are strictly speaking not the same task. Nothing gets run twice.
To avoid confusion, and because gulp.series isn't necessary, simply do:
exports.scssTask = scssTaskFunc;

Cannot find module 'babel-preset-env'

I'm trying to set up es6 transpilation from gulp.
I'm setting up a completely minimal test just to get babel working with gulp.
I installed the modules like this:
npm install --save-dev gulp gulp-cli gulp-babel#next #babel/core #babel/preset-env
I have this gulpfile:
var gulp = require('gulp');
var babel = require('gulp-babel');
gulp.task("default", function () {
return gulp.src("src/app.js")
.pipe(babel())
.pipe(gulp.dest("dist"));
});
This setup is slightly adapted from the babel documentation:
https://babeljs.io/setup#installation
When I run 'gulp', I get this output:
[20:16:25] Using gulpfile ~/play/react/babeltest/gulpfile.js
[20:16:25] Starting 'default'...
[20:16:25] 'default' errored after 23 ms
[20:16:25] Error in plugin "gulp-babel"
Message:
Cannot find module 'babel-preset-env' from '/home/bobdobbs/play/babeltest'
How do I get this working?

How to Run Gulp Task on Netlify

Hi i'm trying to run some gulp task on netlify for building Hugo web.
I wonder how to run serial gulp task on netlify,
by the way this is my gulpfile.js
var gulp = require('gulp');
var removeEmptyLines = require('gulp-remove-empty-lines');
var prettify = require('gulp-html-prettify');
var rm = require( 'gulp-rm' );
var minifyInline = require('gulp-minify-inline');
gulp.task('tojson', function () {
gulp.src('public/**/*.html')
.pipe(removeEmptyLines())
.pipe(gulp.dest('public/./'));
});
gulp.task('htmlClean', function () {
gulp.src('public/**/*.html')
.pipe(removeEmptyLines({
removeComments: true
}))
.pipe(gulp.dest('public/./'));
});
gulp.task('templates', function() {
gulp.src('public/**/*.html')
.pipe(prettify({indent_char: ' ', indent_size: 2}))
.pipe(gulp.dest('public/./'))
});
gulp.task('minify-inline', function() {
gulp.src('public/**/*.html')
.pipe(minifyInline())
.pipe(gulp.dest('public/./'))
});
where should i put the command to run all my gulps task in Netlify?
There are two places to setup your build commands in Netlify.
Admin Option
Put your commands in the online admin under the Settings section of your site and go to Build & Deploy (Deploy settings) and change the Build command:
Netlify Config file (netlify.toml) Option
Edit/add a netlify.toml file to the root of your repository and put your build commands into the context you want to target.
netlify.toml
# global context
[build]
publish = "public"
command = "gulp build"
# build a preview (optional)
[context.deploy-preview]
command = "gulp build-preview"
# build a branch with debug (optional)
[context.branch-deploy]
command = "gulp build-debug"
NOTE:
The commands can be any valid command string. Serializing gulp commands would work fine if you do not want to create a gulp sequence to run them. In example, gulp htmlClean && hugo && gulp tojson would be a valid command.
Commands in the netlify.toml will overwrite the site admin command.
You can string your tasks together like this:
add another plugin with NPM:
https://www.npmjs.com/package/run-sequence
var runSequence = require('run-sequence');
gulp.task('default', function (callback) {
runSequence(['tojson', 'htmlClean', 'templates', 'minify-inline'],
callback
)
})
Then run $ gulp
There's a section on run-sequence on this page that will help:
https://css-tricks.com/gulp-for-beginners/

gulp command not return to shell

I have a gulp script, and when I run it from command line, it does not return to shell.
this is a simple gulp command to delete files from dist directory
'use strict';
var del = require('del');
var gulp = require('gulp');
gulp.task('clean', function (cb) {
return del(['dist/**/*'], cb);
});
run command:
gulp clean
[13:39:10] Using gulpfile gulpfile.js
[13:39:10] Starting 'clean'...
[13:39:11] Finished 'clean' after 6.35 ms
npm WARN package.json abc#0.0.0 No description
npm WARN package.json abc#0.0.0 No repository field.
npm WARN package.json abc#0.0.0 No README data
npm WARN package.json karma-coverage#0.2.7 No README data
npm WARN package.json karma-phantomjs-launcher#0.1.4 No README data
it does not return to shell, I have to do CTRL+C to return back to shell.
I am new to gulp, please let me know what I did wrong, thanks.
Try removing the callback argument.
gulp.task('clean', function () {
return del(['dist/**/*']);
});

gulp-watch works only on initial run

What I do?
Run gulp (SCSS files are being processed, I get a CSS file)
I change any SCSS file again
Expected:
CSS file from 1. is updated with the changes from 2.
What happens?
CSS file from 1. isn't changed
Command line output:
$ gulp
[09:24:28] Using gulpfile c:\Users\User\_dev\github\project\gulpfile.js
[09:24:28] Starting 'sass'...
[09:24:28] Finished 'sass' after 98 ms
[09:24:28] Starting 'default'...
[09:24:28] Finished 'default' after 7.31 μs
[09:24:35] sass-watch saw _base.scss was changed
[09:25:39] sass-watch saw _base.scss was changed
gulpfile.js:
gulp.task('sass', function() {
watch({ glob: 'css/**/*.{scss,sass}', name: 'sass-watch'})
.pipe(plumber())
.pipe(sass())
.pipe(gulp.dest('css'))
});
gulp.task('default', ['sass']);
Notes:
Issue on GitHub (gulp)
Issue on GitHub (gulp-watch)
gulpfile.js on GitHub Gist)
OS: Win7
node: 0.10.29
npm: 1.4.14
The way the source files are piped in is not important. The result stays the same when using gulp.src()
I dont think your sass task is correctly written.
Try something like this:
var gulp = require('gulp');
var sass = require('gulp-sass')
gulp.task('sass', function () {
gulp.src('PATH-TO-SASS-FILES/*.scss')
.pipe(sass())
.pipe(gulp.dest('./css'));
});