Change in Gulp 4 Task Exposure: Preventing Internal Tasks from Appearing in CLI
0 reputation · 12 Apr 2020, 19:18 UTC
0 reputation · 12 Apr 2020, 19:18 UTC
Developers want to keep helper tasks that are only used internally in a gulpfile—such as cleanup steps composed with gulp.series or gulp.parallel—from appearing in the output of gulp --tasks or being invocable directly, to prevent accidental public exposure.
In Gulp 4, every function registered with gulp.task or exported as a task is listed by the CLI, and naming conventions like a leading underscore do not provide real privacy; there is no built‑in mechanism to mark a task as private, and the series/parallel composition APIs do not expose a visibility option. This leaves an open question about how to opt‑out of public task registration without breaking existing workflows.
What options exist to hide a task defined with gulp.series from the CLI listing? Is there a configuration or API that can mark a task as private? How can authors ensure internal composition tasks remain hidden while still using the recommended series/parallel patterns?
35275 reputation · 12 Apr 2020, 20:06 UTC
To hide a task from the Gulp CLI listing, you must avoid registering it with gulp.task() and ensure it is not exported from your gulpfile.js. In Gulp 4, any function registered via the registry or exported as a module member is automatically treated as a public task.
The most effective way to maintain internal composition tasks while keeping them hidden is to define them as standard JavaScript functions. Because gulp.series() and gulp.parallel() accept function references, they do not require those functions to be registered as Gulp tasks first.
Correct Implementation:
const { series, parallel, src, dest } = require('gulp');
// INTERNAL: Defined as a standard function, NOT registered with gulp.task()
function clean() {
return src('dist', { read: false, allowEmpty: true }).pipe(someCleanPlugin());
}
// INTERNAL: Another helper function
function compileCss() {
return src('src/*.css').pipe(someSassPlugin()).pipe(dest('dist'));
}
// PUBLIC: Only this task is registered/exported, making it visible to the CLI
exports.build = series(clean, parallel(compileCss, otherTask));
Gulp 4's CLI scans for tasks in two places: the internal task registry (populated by gulp.task()) and the exports object of your gulpfile. By defining clean and compileCss as local variables/functions, they remain private to the module scope. They are still executable when called by series(), but the CLI has no reference to them as standalone entry points.
To ensure your internal tasks are successfully hidden, run the following commands in your terminal:
gulp --tasks: Verify that only your public tasks (e.g., build) appear in the list.gulp clean: Attempt to run a hidden task directly. Gulp should return a "task not found" error.This approach assumes you are using Gulp 4.x. If you are using a legacy Gulp 3.x workflow, the registry behaves differently. Note that naming a task with a leading underscore (e.g., _clean) is a convention for developers but provides no actual technical privacy in Gulp 4.
Diagnostic Detail: Are you using a custom task loader or a plugin that automatically scans the directory for files and registers every function it finds as a task? If so, the local function approach may be overridden by the loader.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.