实现此目的的一种潜在解决方案是利用grunts --options功能。
通过命令行运行 grunt 任务时,可以指定附加选项值。
在您的场景中,您可以传入将要更改的文件夹名称的版本号。(即在您的情况下,您尝试使用星号字符(*
)指定的部分,例如“5.1.1.5830”
警告:要使此解决方案发挥作用,它确实需要在通过命令行运行任务之前预先知道目标文件夹的值(版本号)是什么。
示例 Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
themesFolder: {
namePart: '0.0.0.0' // <-- If no option is passed via the CLI this name will be used.
},
less: {
production: {
options: {
// ...
},
files: {
// The destination path below utilizes a grunt template for the part
// of the folder name that will change. E.g. '5.1.1.0'
'../../application/user/themes/some-theme-<%= themesFolder.name %>/css/main.css': 'less/base.less'
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-less');
grunt.registerTask('saveFolderNameFromOption', 'Uses the option provided to configure name part.', function(n) {
var themesFolder = grunt.option('themesFolder');
if (themesFolder) {
// Store the option value so it can be referenced in the less task.
grunt.config('themesFolder.namePart', themesFolder);
}
});
grunt.registerTask('processLess', ['saveFolderNameFromOption', 'less:production']);
};
运行ProcessLess任务
通过命令行运行任务,如下所示:
$ grunt processLess --themesFolder=5.1.1.5830
注意:指定的附加选项。即:--themesFolder=5.1.1.5830
使用上述命令时,.css
输出将定向到以下路径:
'../../application/user/themes/some-theme-5.1.1.5830/css/main.css': 'less/base.less'
现在,每次运行任务时,您都会相应地修改选项。
好处:通过 CLI 提供版本号作为选项,将避免Gruntfile.js
每次运行时都必须重新配置。