Karthik Ar
Karthik Ar

Reputation: 125

Grunt task run one by one

Please help me.. I am new to grunt. I have to run grunt task one by one. When i execute the Grunt file i am trying to execute one by one ['clean', 'writefile','concat','requirejs'] since write file helps to create a dynamic json for requier.

When ever i execute first time grunt gives me error and at the second time it runs without error since the json file is created in the path. I tried grunt.task.run() but i couldn't get it

    module.exports = function (grunt) {
        'use strict';
        grunt.initConfig({

            // Before generating any new files, remove any previously-created files.
            clean: {
                tests: ['rjs/build.json','frontend-built']
            },
            writefile: {
                json_value: {
                    options: {
                        data: 'frontend/config.json'
                    },
                    src: 'rjs/value.hbs',
                    dest: 'rjs/build.json'
                }
            },
            requirejs: {
                compile: {
                    options:grunt.file.readJSON('rjs/build.json')
                }
            },
            concat: {
                dist: {
                    files: {
                        'frontend/theme/css/theameA.css': ['frontend/theme/css/common/**/*.css','frontend/theme/css/lib/**/*.css','frontend/theme/css/theme_a/**/*.css'],
                        'frontend/theme/css/theameB.css': ['frontend/theme/css/common/**/*.css','frontend/theme/css/lib/**/*.css','frontend/theme/css/theme_b/**/*.css']

                    }
                }
            }

        });
        grunt.loadNpmTasks('grunt-contrib-clean');
        grunt.loadNpmTasks('grunt-writefile');
        grunt.loadNpmTasks('grunt-contrib-concat');
        grunt.loadNpmTasks('grunt-contrib-requirejs');
        grunt.registerTask('default', ['clean', 'writefile','concat','requirejs']);

    };

Upvotes: 0

Views: 257

Answers (1)

Robert Levy
Robert Levy

Reputation: 29083

Ok the problem is that the config code is processed before the tasks run so even if it didn't error out, it wouldn't be the correct behavior.

Try this to set the requirejs config dynamically via another custom task:

   module.exports = function (grunt) {

    'use strict';
    grunt.initConfig({

        // Before generating any new files, remove any previously-created files.
        clean: {
            tests: ['rjs/build.json','frontend-built']
        },
        writefile: {
            json_value: {
                options: {
                    data: 'frontend/config.json'
                },
                src: 'rjs/value.hbs',
                dest: 'rjs/build.json'
            }
        },
        concat: {
            dist: {
                files: {
                    'frontend/theme/css/theameA.css': ['frontend/theme/css/common/**/*.css','frontend/theme/css/lib/**/*.css','frontend/theme/css/theme_a/**/*.css'],
                    'frontend/theme/css/theameB.css': ['frontend/theme/css/common/**/*.css','frontend/theme/css/lib/**/*.css','frontend/theme/css/theme_b/**/*.css']

                }
            }
        }

    });
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-writefile');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-requirejs');
    grunt.registerTask('setRjsConfig', function() {
         grunt.config('requirejs.options.compile', grunt.file.readJSON('rjs/build.json'));
    });
    grunt.registerTask('default', ['clean', 'writefile','concat', 'setRjsConfig', 'requirejs']);

};

Upvotes: 1

Related Questions