动态地将版本号添加到目标输出文件w / grunt [英] Dynamically add version number to dest output files w/ grunt

查看:74
本文介绍了动态地将版本号添加到目标输出文件w / grunt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的版本号有一个 package.json 文件,例如:

I have a package.json file with our version number, such as:

{
    name: "myproject"
    version: "2.0"
}

我的目标是将package.json文件中的版本号动态添加到输出文件中。例如,在javascript中,我不想手动更新版本号,但是想要在每个grunt构建之后生成类似于此的内容:

My goal is to dynamically add the version number from the package.json file into the output files. For example, in the javascript I don't want to manually update the version number, but would like something similar to this to be generated after each grunt build:

/* My Project, v2.0 */
window.myProject = {
    version: "2.0"
};

有没有简单的方法可以在我的Gruntfile.js配置中执行此操作?

Is there an easy way to do this in my Gruntfile.js configuration?

推荐答案

我实施了: https:// github。 com / erickrdch / grunt-string-replace

在我的源代码css / js文件中,我使用 {{VERSION} } ,它被设置在 package.json 文件中的版本号取代。下面是我添加到 Gruntfile.js 的配置。

In my source css/js files, I use the text {{ VERSION }} which gets replaced with the version number set in the package.json file. Below is the config I added to Gruntfile.js.

'string-replace': {
  version: {
    files: {
      // the files I did string replacement on
    },
    options: {
      replacements: [{
        pattern: /{{ VERSION }}/g,
        replacement: '<%= pkg.version %>'
      }]
    }
  }
},
pkg: grunt.file.readJSON('package.json'),

这篇关于动态地将版本号添加到目标输出文件w / grunt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆