Warrior
Warrior

Reputation: 3304

Grunt run task in a look with dynamic parameters

Using Grunt Js i am trying to minify js file from a folder in to another folder, this is working find when i am hardcoding the paths or if i pass the version folder name through CLI

Like: grunt minify --vrn=11.3.0 11.3.0 is the folder name in the path ../../ecommerce/${version}/js/sitejs/ my "ecommerce" folder has multiple directories with version like -> 10.0.0 -> 11.3.0 . .

SRC Path of Js: ../../ecommerce/11.3.0/js/sitejs/

DEST Path for Minified Js: ../../ecommerce/11.3.0/js/sitejs/u-min/

what i am trying is to read all the directory names under "ecommemerce" and dynamically pass it to the task, so that on one command all the version folder will be checked for the file change and generate minfied file for the same

Is it possible? if i call grunt minify from CLI it will work for changed:uglify:site:11.3.0 & changed:uglify:site:10.0.0 in one shot

Note: I cannot use the watch in my scenario

Below is my Gruntfile.js

module.exports = function (grunt) {
  let version = grunt.option('vrn');

  let inputSiteFilePath = `../../ecommerce/${version}/js/sitejs/`;
  let targetSiteFilePath = `../../ecommerce/${version}/js/sitejs/u-min/`;
  
  let versionDirList = [];
  grunt.file.expand({ filter: 'isDirectory' }, '../../ecommerce/*').forEach(element => {
    versionDirList.push(element.split("/").pop());
  });
  console.log('versionDirList:',versionDirList);
   
  grunt.loadNpmTasks("grunt-contrib-uglify");
  grunt.loadNpmTasks('grunt-changed');

  grunt.initConfig({
    pkg: grunt.file.readJSON("package.json"),
    uglify: {
      site: {
        options: {
          sourceMap: true,
          ie8: true,
          mangle: false,
          compress: {
            sequences: true,
            properties: true,
            dead_code: true,
            drop_debugger: true,
            unsafe: false,
            conditionals: true,
            comparisons: true,
            evaluate: true,
            booleans: true,
            loops: true,
            unused: true,
            hoist_funs: true,
            hoist_vars: false,
            if_return: true,
            join_vars: true,
            side_effects: true,
            drop_console: true,
            global_defs: {},
          },
        },
        files: [
          {
            cwd: inputSiteFilePath,
            src: ["*.js"],
            dest:targetSiteFilePath,
            expand: true,
            flatten: false,
          },
        ],
      },
    },
  });
  
  grunt.registerTask('minify', ['changed:uglify:site']);

};

OUTPUT

PS D:\GitHubApplications\minify-app> grunt minify --vrn=11.3.0
versionDirList: [ '10.12.0', '11.3.0' ]
Running "changed:uglify:site" (changed) task

Running "uglify:site" (uglify) task
>> 1 sourcemap created.
>> 1 file created 16.8 kB → 11.3 kB

Running "changed-postrun:uglify:site:1:D:\GitHubApplications\minify-app\node_modules\grunt-changed\.cache" (changed-postrun) task

Done.
PS D:\GitHubApplications\minify-app> 

Upvotes: 0

Views: 39

Answers (1)

Warrior
Warrior

Reputation: 3304

With below i was able to loop task for all the versions and minify in one shot

Note: changed was dropped off, as it didnt work in my scenario and was not identifying the file changes in all version folders

module.exports = function (grunt) {
  grunt.loadNpmTasks("grunt-contrib-uglify");
  grunt.initConfig({
    pkg: grunt.file.readJSON("package.json"),
    uglify: {
      site: {
        options: {
          vrn:"11.3.0",
          sourceMap: true,
          ie8: true,
          mangle: false,
          compress: {
            sequences: true,
            properties: true,
            dead_code: true,
            drop_debugger: true,
            unsafe: false,
            conditionals: true,
            comparisons: true,
            evaluate: true,
            booleans: true,
            loops: true,
            unused: true,
            hoist_funs: true,
            hoist_vars: false,
            if_return: true,
            join_vars: true,
            side_effects: true,
            drop_console: true,
            global_defs: {},
            
          },
        },
        files: [
          {
            cwd: `../../ecommerce/<%= vrn %>/js/sitejs/`,
            src: ["*.js"],
            dest: `../../ecommerce/<%= vrn %>/js/sitejs/u-min/`,
            expand: true,
            flatten: false,
          },
        ],
      },
    },
  });

  grunt.registerTask('minifyalljs', function(vrn){
    grunt.config.set('vrn', vrn);
    grunt.task.run('uglify:site');
  });
   
  grunt.registerTask('minifyalljsversion', function(){
    grunt.file.expand({ filter: 'isDirectory' }, '../../ecommerce/*').forEach(element => {
      var currentVrn = element.split("/").pop();
      grunt.task.run('minifyalljs:' + currentVrn);
    });
  });
};

For CLI: grunt minifyalljsversion

Upvotes: 0

Related Questions