Javascript 是否需要反复获取`pkg.json`版本作为变量来替换Grunt中`index.js`文件的版本?

Javascript 是否需要反复获取`pkg.json`版本作为变量来替换Grunt中`index.js`文件的版本?,javascript,gruntjs,Javascript,Gruntjs,我有一个咕噜的身材。其中有grunt bump和grunt replace。每次运行pkg.json时,我都使用grunt bump在pkg.json中升级一个版本。每次运行grunt replace时,我还希望将index.js版本替换为pkg.json版本号。但是,我不能重复这样做,因为一旦替换了变量包,变量就不再存在了 Grunfile.js: module.exports = function(grunt) { grunt.initconfig({ pkg: grunt.file.re

我有一个咕噜的身材。其中有
grunt bump
grunt replace
。每次运行
pkg.json
时,我都使用
grunt bump
pkg.json中升级一个版本。每次运行
grunt replace
时,我还希望将
index.js
版本替换为
pkg.json
版本号。但是,我不能重复这样做,因为一旦替换了变量包,变量就不再存在了

Grunfile.js:

module.exports = function(grunt) {

grunt.initconfig({
pkg: grunt.file.readjson('package.json'),

bump: {
  options: {
    files: ['package.json'],
    updateconfigs: [],
    commit: false,
    commitmessage: 'release v%version%',
    commitfiles: ['package.json'],
    createtag: false,
    tagname: 'v%version%',
    tagmessage: 'version %version%',
    push: false,
    pushto: 'upstream',
    gitdescribeoptions: '--tags --always --abbrev=1 --dirty=-d',
    globalreplace: false,
    prereleasename: false,
    metadata: '',
    regexp: false
  }
},

replace : {
  dist : {
    options : {
      patterns : [
        {
          match: 'packageJsonVersion',
          replacement: '<%= pkg.version %>';
        }
      ]
    },

    files : [
      {
        expand : true,
        flatten : true,
        src : [ 'index.html' ],
        dest : './'
      },
    ]
  }
 },
});

grunt.loadnpmtasks('grunt-bump');
grunt.loadnpmtasks('grunt-replace');

grunt.registertask('default', ['uglify']);
};
module.exports=函数(grunt){
grunt.initconfig({
pkg:grunt.file.readjson('package.json'),
碰撞:{
选项:{
文件:['package.json'],
UpdateConfigures:[],
犯:错,
commitmessage:'release v%version%',
提交文件:['package.json'],
createtag:false,
标记名:“v%version%”,
标记消息:“版本%version%”,
推送:错,
普什托:“上游”,
gitdescribeoptions:'--tags--always--abbrev=1--dirty=-d',
全局替换:false,
prereleasename:false,
元数据:“”,
regexp:false
}
},
替换:{
地区:{
选项:{
模式:[
{
匹配:“packageJsonVersion”,
替换:'';
}
]
},
档案:[
{
是的,
扁平化:是的,
src:['index.html'],
目的地:'./'
},
]
}
},
});
grunt.loadnpmtasks(“grunt-bump”);
grunt.loadnpmtasks(“grunt-replace”);
registertask('default',['uglify']);
};
index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>


  <script src="index.@@packageJsonVersion.js"></script>
</body>
</html>

通常,我在
src
文件夹中有一个包含要替换变量的base
index.html
文件,但我实际上复制了该文件,并在
build
文件夹(或任何实际包含产品静态资产的文件夹)中执行替换。这样,基本文件就不会被更改。
也许您可以更改
dest
,让web服务器指向index.html的prod版本

files : [
      {
        expand : true,
        flatten : true,
        src : [ 'index.html' ],
        dest : 'build/index.html'
      },
    ]

通常,我的基本
index.html
文件包含要在
src
文件夹中替换的变量,但我实际上复制了该文件,并在
build
文件夹(或实际包含产品静态资产的任何文件夹)中执行替换。这样,基本文件就不会被更改。
也许您可以更改
dest
,让web服务器指向index.html的prod版本

files : [
      {
        expand : true,
        flatten : true,
        src : [ 'index.html' ],
        dest : 'build/index.html'
      },
    ]

是的,这就是我上周最后做的。是的,这就是我上周最后做的。