将Grunt Jade选项传递到File

Pass Grunt Jade Options to File

本文关键字:File 选项 Grunt Jade      更新时间:2023-09-26

我正在使用grunt-contrib-jade,我想使用pkg.name和pkg.version来构建我的css文件名。我不能让这个工作,希望有人能帮助我。以下是我的文件:

Gruntfile中jade任务的一部分

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

然后在我的jade文件中我有:

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

不知道如何在Gruntfile中从jade任务中添加编译选项中的数据。

提前感谢您的帮助

看起来您没有加载包。在你的Grunt文件中添加以下内容:

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

我个人更喜欢将它添加到元对象下,如下所示:

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

你也可以尝试以下操作:

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

这种方法不像前一种方法那样动态。