# grunt-task-helper **Repository Path**: mirrors_anseki/grunt-task-helper ## Basic Information - **Project Name**: grunt-task-helper - **Description**: Help with handling the files (e.g. check changed files) before other tasks, or adding something (e.g. replace text). - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-08 - **Last Updated**: 2026-05-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # grunt-task-helper [![npm](https://img.shields.io/npm/v/grunt-task-helper.svg)](https://www.npmjs.com/package/grunt-task-helper) [![GitHub issues](https://img.shields.io/github/issues/anseki/grunt-task-helper.svg)](https://github.com/anseki/grunt-task-helper/issues) [![dependencies](https://img.shields.io/badge/dependencies-No%20dependency-brightgreen.svg)](package.json) [![license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) taskHelper helps selecting files for other tasks. For example, you want to minify only changed JS files. Then taskHelper selects files which are newer than `dest` from `src` (or newer than the time when this ran last time), and these files are passed to [grunt-contrib-uglify](https://github.com/gruntjs/grunt-contrib-uglify) task. And, taskHelper helps you do something small to files (or file's contents). For example, rename file, replace text, etc... You can create your custom task to do something easily via `grunt.registerTask()`. Or, writing new plugin is easy too. Using taskHelper is more easy. ## Getting Started This plugin requires Grunt `~0.4.1` If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command: ```shell npm install grunt-task-helper --save-dev ``` Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript: ```js grunt.loadNpmTasks('grunt-task-helper'); ``` ## The "taskHelper" task taskHelper accepts standard Grunt `files` components (see [Files](http://gruntjs.com/configuring-tasks#files)) and [*handlers*](#handlers). The handler is a *JavaScript Function* which you wrote, or a name of [builtin handler](#builtin-handlers). These handlers are called in some timings to select files or do something to files or file's contents. If you want, taskHelper creates standard Grunt `files` components which are new for other tasks. ### Overview In your project's Gruntfile, add a section named `taskHelper` to the data object passed into `grunt.initConfig()` (see [Configuring tasks](http://gruntjs.com/configuring-tasks)). You specify the `files` and some [*handlers*](#handlers) (e.g. `options.handlerByFile`). taskHelper accepts these all files, and some files are selected or done something via handler, and only selected files (filtered files) are passed to other tasks via `options.filesArray`. **Example:** Copy only CSS files which are needed. This handler works like expanded [Custom Filter Function](http://gruntjs.com/configuring-tasks#custom-filter-function). `Gruntfile.js` ```js grunt.initConfig({ taskHelper: { deploy: { options: { handlerByFileSrc: function(src, dest, options) { var pathElms = src.match(/^(.+)\.css$/), filepath; if (pathElms) { // CSS file from SCSS file is not needed. if (grunt.file.exists(pathElms[1] + '.scss')) { return false; } // Give priority to souces directory. filepath = src.replace(/^develop/, 'souces'); if (grunt.file.exists(filepath)) { return filepath; } } }, filesArray: [] }, src: 'develop/**/*.{css,scss}', dest: 'backup/' } }, copy: { deploy: { // Copy files which are selected via taskHelper. files: '<%= taskHelper.deploy.options.filesArray %>' } } }); ``` **Example:** Minify only changed JS files. (see [Builtin handler "newFile"](#builtin-handlers-newfile).) `Gruntfile.js` ```js grunt.initConfig({ taskHelper: { deploy: { options: { // Select files which are newer than `dest`. handlerByFile: 'newFile', filesArray: [] }, expand: true, cwd: 'develop/', src: '**/*.js', dest: 'public_html/' } }, uglify: { deploy: { // Minify files which are selected via taskHelper. files: '<%= taskHelper.deploy.options.filesArray %>' } } }); ``` **Example:** Insert menu navigation into HTML files. `Gruntfile.js` ```js var htmlMenu = grunt.file.read('../menu.html'); grunt.initConfig({ taskHelper: { deploy: { options: { handlerByContent: function(contentSrc, options) { // Insert HTML content to placeholder. return contentSrc.replace(/<%MENU%>/, htmlMenu); } }, expand: true, cwd: 'develop/', src: '**/*.html', dest: 'public_html/' } } }); ``` **Example:** Edit HTML files to refer to minified JS files (e.g. `file.js` to `file.min.js`). `Gruntfile.js` ```js var jsFiles = []; grunt.initConfig({ taskHelper: { // Get source JS files and minified JS files via globbing pattern. getJs: { options: { filesArray: jsFiles }, expand: true, cwd: 'develop/', src: '**/*.js', dest: 'public_html/', ext: '.min.js' }, // Edit HTML files.