Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt task to modify javascript file

I have following node.js javascript file.

module.exports = function(){
    ...
    var config1 = "";
    var config2 = "";
    var config3 = "";
    ...
}

Suppose I have following grunt task.

grunt.registerTask('task1', ['...',copy]);
grunt.registerTask('task2', ['...',copy]);
grunt.registerTask('task3', ['...',copy]);

Depending on which task I am running, config1, config2, config3 varible value should change.

For example:

grunt task1

should produce

module.exports = function(){
    ...
    var config1 = "A";
    var config2 = "B";
    var config3 = "C";
    ...
}

while grunt task2

should produce

module.exports = function(){
    ...
    var config1 = "X";
    var config2 = "Y";
    var config3 = "Z";
    ...
}

Is there any grunt plugin which can do this or similar?

like image 567
Pavan Kumar Avatar asked Feb 17 '26 15:02

Pavan Kumar


1 Answers

For simple stuff like this, I just use grunt-replace. Give it a regex to find each line and a string to replace that line with what you want the output to be. This doesn't actually parse your code though so if you later do some refactoring, the regex's will need to change.

like image 57
Robert Levy Avatar answered Feb 20 '26 03:02

Robert Levy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!