简体   繁体   English

Gruntfile.js中的Javascript变量?

[英]Javascript variables in Gruntfile.js?

I'm maintaining a web application that uses Grunt extensively. 我正在维护一个广泛使用Grunt的Web应用程序。 I have to minify, copy my html, css, js files to different locations in different times. 我必须缩小,在不同的时间将我的html,css,js文件复制到不同的位置。 So to make it easy I created a simple javascript variable in my GruntFile.js as follows: 为了方便起见,我在GruntFile.js中创建了一个简单的javascript变量,如下所示:

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. 我不能在我的GruntFile.js中使用路径变量。 Because I have 10 location paths. 因为我有10个位置路径。

Another way, is to utilize Grunt templates: 另一种方法是使用Grunt模板:

grunt.initConfig({
  path: 'C:/dist/',
  uglify: {
    options: {
      mangle: false
    },
    '<%= path %>js/jsFile.js': ['src/js/jquery-1.10.2.min.js']
  }          
});

The javascript object format doesn't allow a variable as the actual key: javascript对象格式不允许变量作为实际键:

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 如何从变量设置JS对象属性名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM