无论如何,是否可以反复抓取“pkg.json”版本作为变量来替换Grunt中“index.js”文件的版本



我有一个咕噜咕噜的构建。里面有grunt-bumpgrunt-replace。每次运行它时,我都会使用 grunt-bumppkg.json中提升一个版本。而且我还想在每次运行时用pkg.json版本号替换index.js版本grunt-replace.但是,我不能重复执行此操作,因为一旦变量被替换@@package变量,变量就不再存在。

Gruntfile.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']);
};

索引.html:

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

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

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

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

最新更新