我有一个咕噜构建。里面有grunt-bump
和grunt-replace
。每次运行时,我都会使用 grunt-bump
在 pkg.json
中升级一个版本。而且我还想在每次运行 grunt-replace
时将 index.js
版本替换为 pkg.json
版本号。但是,我不能重复执行此操作,因为一旦变量`@@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']);
};
index.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
文件夹(或任何实际包含您的产品静态 Assets 的文件夹)。这样,基本文件就不会被更改。
也许您可以更改dest
并使您的网络服务器指向index.html的产品版本?
files : [
{
expand : true,
flatten : true,
src : [ 'index.html' ],
dest : 'build/index.html'
},
]
关于javascript - 是否有办法重复抓取 `pkg.json` 版本作为变量来替换 Grunt 中 `index.js` 文件的版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40553712/