Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript variables in Gruntfile.js?

I'm maintaining a web application that uses Grunt extensively. I have to minify, copy my html, css, js files to different locations in different times. So to make it easy I created a simple javascript variable in my GruntFile.js as follows:

var path="C:/dist";

uglify: {
    options: {
       mangle: false
     },
     my_target: {
       files: {
        path+'/js/jsFile.js': ['src/js/jquery-1.10.2.min.js']
        }
     }          
}

When I am building this i am getting the following error

>> SyntaxError: Unexpected token +

Can't I Use path variable in my GruntFile.js. Because I have 10 location paths.

like image 820
Rajeev Avatar asked Feb 07 '14 18:02

Rajeev


2 Answers

Another way, is to utilize Grunt templates:

grunt.initConfig({
  path: 'C:/dist/',
  uglify: {
    options: {
      mangle: false
    },
    '<%= path %>js/jsFile.js': ['src/js/jquery-1.10.2.min.js']
  }          
});
like image 81
Kyle Robinson Young Avatar answered Sep 29 '22 18:09

Kyle Robinson Young


The javascript object format doesn't allow a variable as the actual key:

path+'/js/jsFile.js'

This should work for you:

var path = "C:/dist";

var files = {};
files[path+"/js/jsFile.js"] = ['src/js/jquery-1.10.2.min.js'];

//...
options: {
   mangle: false
 },
 my_target: {
   files: files
 }          

You can see several example of using variables as the key here:

How To Set A JS object property name from a variable

like image 27
helion3 Avatar answered Sep 29 '22 16:09

helion3