Is it possible to pass a flag to Gulp to have it run tasks in different ways?
Yes, utilize Gulp with yargs to pass flags. Running gulp task --flag prompts conditional logic in your tasks. An example of a styles task:
Issue gulp styles --production to compress your CSS. Alternatively, only gulp styles if you wish to include sourcemaps.
Flexing with enhanced tasks
We can place yargs into overdrive for parsing command line arguments and brace it with gulp-if to enhance your workflow. Here's how to pair them like wine and cheese:
Giving gulp scripts --uglify a whirl will minify your JavaScript files. If you run it barebones with gulp scripts, the files remain unaltered.
Trigger actions based on conditions
Optimize your gulpfile by centrally managing your condition checks from a utility function:
Using gulp images --production results in optimized images only when required.
Creating environment-specific tasks
Occasionally, tasks tend to be environment-specific. Utilize Node's process.env as an alternative:
Command NODE_ENV=production gulp deploy sets the environmental variable leading the task.
Taking charge of minification and sourcemaps
Safeguard minification in the production environment:
Stay in control of sourcemaps:
Command gulp js --production forgoes sourcemaps for nimble builds.
Manipulating file names dynamically
Adjust output filenames or paths using flags, great for versioning:
Take gulp rename --suffix for a spin to append .min to output filenames.
Was this article helpful?