GruntJS, several tasks and grunt.option

I am starting to use GruntJS for one of my projects. I easily recorded a simple build script using simple aliases.

However, my script contains many tasks that are basically the same, the only difference is some options, such as the source folder and destination folder.

For instance:

sass: { options:{ trace: true, debugInfo: true, style: 'compressed' }, html: { files: { 'build/html/css/main.css': 'sass/html.sass' } }, html2: { files: { 'build/html2/css/main.css': 'sass/html2.sass' } }, html3: { files: { 'build/html3/css/main.css': 'sass/html3.sass' } } } 

What I would like to achieve is to have only one task, and then pass the parameters (dest, src) to this task.

I tried to implement this with MultiTasks:

 grunt.registerTask('sass2', 'Run all Sass compilation tasks.', function() { var projects = ['html','html2','html3']; projects.forEach(function(proj){ grunt.config.set('sass.files.dest', 'build/' + proj + '/css/main.css'); grunt.config.set('sass.files.src', 'sass/' + proj + '.sass'); grunt.log.writeln(grunt.config.get('sass.files.dest')); grunt.log.writeln(grunt.config.get('sass.files.src')); grunt.task.run('sass'); }); }); 

Grunt log displays the correct values ​​for the parameters, however only html3 sass is compiled.

I don’t understand why only one of the projects compiles and how I can fix it.

Perhaps there is another way to solve this problem. The best way. Perhaps the use of templates?

Any help or advice would be helpful.

Thanks!

+4
source share
1 answer

Only the last configuration is used, because grunt.task.run puts them in the queue to run after the current task. From the API :

Complete one or more tasks. Each task in taskList will be launched immediately after completion of the current task in the specified order

What do you have:

  • Set configuration 1
  • Set configuration 2
  • Set Configuration 3
  • Run three tasks using the currently active configuration, which is # 3.

Instead, you can do something similar to dynamically create many configuration sections and perform the following tasks:

 grunt.config.set('sass.' + proj + '.files', [{ src: 'sass/' + proj + '.sass', dest: 'build/' + proj + '/css/main.css' }]); 

and then call runtask with the newly created partition:

 grunt.task.run('sass:' + proj); 
+6
source

Source: https://habr.com/ru/post/1481621/


All Articles