Javascript variables in Gruntfile.js?

狂风中的少年 提交于 2019-12-06 17:24:53

问题


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.


回答1:


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']
  }          
});



回答2:


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



来源:https://stackoverflow.com/questions/21635005/javascript-variables-in-gruntfile-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!