简体   繁体   中英

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. 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

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.

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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