1

I cannot figure out why uglify does not want concat string as input or output ...

This works :

uglify: {
    dev_uglify_js: {
        files: {
            'my_file.min.js': ['my_file.js']
        }
    }
}

For example, this does not works :

uglify: {
    dev_uglify_js: {
        files: {
            'my'+'_file.min.js': ['my_file.js']
        }
    }
}

Do you have any idea why ? The output error is "SyntaxError: Unexpected token".

The real insterest here is to concatenate a timestamp to the file name. But just with 2 strings it does not work so ...

Thanks for your help !

maxime1992
  • 22,502
  • 10
  • 80
  • 121

1 Answers1

1

In JavaScript, an object key cannot be declared dynamically. This is not a problem with grunt or uglify - it's a language constraint.

myObject = { 'a' + 'b' : 'b' } // NOPE!

However, any object property can be accessed via square brackets. For example:

myObject = { 'banana': 'boat' }
myObject.banana // boat
myObject['banana'] // boat!

Therefore, you can add properties after the object is already created, using the square brackets syntax.

myObject = {} 
myObject[ 'a' + 'b' ] = 'b' // Yes
myObject.ab // b

The Gruntfile example

In your Gruntfile, you're bound to, at some point, call something like grunt.config.init or grunt.initConfig. This is usually done inline:

grunt.initConfig({
  uglify: {} // properties ...
});

However, initConfig simply receives an object. You can define it and manipulate it as much as you need before calling this function. So, for example:

var config = { uglify: {} };
config.uglify['such'+'dynamic'+'very'+'smarts'] = {};
grunt.initConfig(config);

Similar questions:

How do I create a dynamic key to be added to a JavaScript object variable

How do I add a property to a JavaScript object using a variable as the name?

Community
  • 1
  • 1
Guilherme Rodrigues
  • 2,818
  • 1
  • 17
  • 22
  • Thx for your answer gadr90 :). I read both subjects and indeed they are related to my question. But still ... I don't understand. How can i have a dynamic name file for uglify ? – maxime1992 Feb 20 '15 at 16:53
  • I feel quite dumb on this one, i spent 3hours looking for ideas but nothing work. – maxime1992 Feb 20 '15 at 16:59
  • 1
    Hey, @Maxime, there are no dumb questions! I think you don't realize that the grunt configuration object is just that - an object. You don't need to declare it inline. See if my expanded answer is more fitting, and congrats on your determination! – Guilherme Rodrigues Feb 20 '15 at 17:09
  • Ok then ! In my grunt i have many tasks. Not only the uglify one. Do i have to declare them before the grunt init ? Could you edit and add some code to your last code example ? Because i don't understand – maxime1992 Feb 20 '15 at 17:14
  • 1
    You can declare your entire config before and call `initConfig` on the last line, if you want :) – Guilherme Rodrigues Feb 20 '15 at 18:05
  • 1
    I declared the config before the initConfig and now it works !! :D !! Thank you so much gadr90, and i checked your blog you write interesting articles ! Keep going ! (you saved my night haha). Pb marked as solved ;) – maxime1992 Feb 20 '15 at 18:16