Gulp task configuration pattern¶
The Gulp task configuration pattern is a software development practice that involves defining a centralized configuration object within a gulpfile.js to manage project-specific parameters.^[600-developer__frontend__fe-tools__glup-livereload.md]
Purpose¶
This pattern separates data from logic by extracting variables—such as source directories, ports, and hostnames—into a custom config object.^[600-developer__frontend__fe-tools__glup-livereload.md] This approach allows multiple tasks to reference a single source of truth for file paths and server settings, making the build system easier to maintain and update.^[600-developer__frontend__fe-tools__glup-livereload.md]
Implementation¶
In a typical Gulp setup, the configuration object is defined immediately after loading the required plugins.^[600-developer__frontend__fe-tools__glup-livereload.md]
var gulp = require('gulp');
var connect = require('[gulp-connect](<./gulp-connect.md>)');
var livereload = require('gulp-livereload');
// Configuration object
var config = {
src: 'src'
}
Once defined, the properties of this object are utilized across the task definitions to ensure consistency.^[600-developer__frontend__fe-tools__glup-livereload.md]
Usage Examples¶
The configuration pattern is commonly applied in server, watch, and reload tasks:
- Server Configuration: The
connecttask usesconfig.srcto define the server root andconfig.port(or a hardcoded value mapped to the config) to set the access port.^[600-developer__frontend__fe-tools__glup-livereload.md] - File Watching: The
watchtask references the configuration object to monitor specific file extensions within the source directory.^[600-developer__frontend__fe-tools__glup-livereload.md] - Source Handling: The
reloadtask uses the configuration to construct file paths for the livereload process, ensuring the correct files are pushed to the browser.^[600-developer__frontend__fe-tools__glup-livereload.md]
Related Concepts¶
- [[Task Runners]]
- [[Gulp]]
- [[Workflow Automation]]
Sources¶
600-developer__frontend__fe-tools__glup-livereload.md