简体   繁体   English

将Grunt Jade选项传递给文件

[英]Pass Grunt Jade Options to File

I am using grunt-contrib-jade and I am wanting to use the pkg.name and pkg.version to build out my css file name. 我正在使用grunt-contrib-jade,并且想使用pkg.name和pkg.version建立我的css文件名。 I can't get this to work and hope someone can help me on this. 我无法使它正常工作,希望有人可以帮助我。 Here is what I have: 这是我所拥有的:

Part of jade task in Gruntfile Gruntfile中的玉器任务的一部分

compile: {
    options: {
        data  : {
            app    : '<%= pkg.name %>',
            version: '<%= pkg.version %>',
        },
        pretty: true
    }

Then in my jade file I have: 然后在我的玉文件中,我有:

link(href='_assets/css/<%= app %>-<%= version %>.css', rel='stylesheet', media='screen')

Don't know how to add the data from the compile options from the jade task in Gruntfile. 不知道如何从Gruntfile中的jade任务的编译选项中添加数据。

Thank you in advance for your help 预先感谢您的帮助

it looks like you have not loaded package.json into your Grunt config add the folloing to your grunt file. 看来您尚未将package.json加载到您的Grunt配置中,将以下内容添加到您的grunt文件中。

grunt.initConfig({
   pkg: require("./package.json"),// <---- add this line
   compile: {
       options: {
          data  : {
              app    : '<%= pkg.name %>',
              version: '<%= pkg.version %>',
          },
          pretty: true
       }
  }
});

I personally prefer to add it under a meta object like as follows: 我个人更喜欢将其添加到如下的meta对象下:

grunt.initConfig({
   meta{
      pkg: require("./package.json"),// <---- add this line
   },
   compile: {
       options: {
          data  : {
              app    : '<%= meta.pkg.name %>', // <-- notice i added meta
              version: '<%= meta.pkg.version %>',// <-- notice added meta
          },
          pretty: true
       }
  }
});

you could also try the following: 您还可以尝试以下操作:

var pkg = require("./package.json");

grunt.initConfig({
   compile: {
       options: {
          data  : {
              app    : pkg.name, // <-- notice no quotes and no micro templating
              version: pkg.version ,// <-- notice no quotes and no micro templating
          },
          pretty: true
       }
  }
});

this aproach is not as dynamic as the previous one. 这种方式并不像上一个那样动态。

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

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