我在 gruntfile 中的 module.exports
顶部设置了模板:
// Self calling so we get a value.
// Expects only a single schema in the schema folder.
grunt.config.set('schemaPath', function() {
console.log(grunt.file.expand('schema/*.json')[0]);
return grunt.file.expand('schema/*.json')[0];
}());
console.log(grunt.config.get('schemaPath'));
稍后,在配置命令时,我想使用模板字符串,如下所示:
stripJsonComments: {
currentSchema: {
files: {
'build/<%= schemaPath %>': '<%= schemaPath %>'
}
}
}
但是我收到错误:处理模板时发生错误(无法读取未定义的属性“src”)。
经过调查,模板字符串似乎没有扩展,而是被删除。
最佳答案
您不能使用模板作为属性名称,它们不会被处理。所以'build/<%= schemaPath %>': '<%= schemaPath %>'
行不通。
我建议研究 task configuration 的另一个变体,您可以在其中定义 src
和dest
明确属性,然后使用值模板。
如果您正在寻找多个 src-dest 文件映射,您可以使用 files array format 。它看起来像这样:
module.exports = function(grunt) {
grunt.initConfig({
concat: {
main: {
files: [
{src: ['<%= file1 %>', '<%= file2 %>'], dest: '<%= destPath %>'},
]
},
}
});
grunt.config.set('file1', 'foo.js');
grunt.config.set('file2', 'bar.js');
grunt.config.set('destPath', 'dest/baz.js');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.registerTask('default', ['concat']);
};
关于gruntjs - 如何使用 grunt 配置的模板字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471549/