我正在尝试在复制文件时替换不同文件中的占位符。我的gruntfile运行良好,但添加处理选项来进行替换时,它就无法正常工作。下面是我的gruntfile的相关部分:
grunt.initConfig({
copy: {
js: {
files: [{
expand: true,
cwd: 'src/wp-content/themes/pilau-starter/',
src: ['**/*.js'],
dest: 'public/wp-content/themes/pilau-starter/'
}],
options: {
process: function ( content ) {
console.log( content );
content = content.replace( /pilauBreakpointLarge/g, breakpoints.large );
content = content.replace( /pilauBreakpointMedium/g, breakpoints.medium );
return content;
}
}
},
}
});
这些路径可以在GitHub上的代码上下文中理解:https://github.com/pilau/starter(公共目录没有提交到repo,因为它是一个起始主题)。这些路径是我原始Gruntfile中的变量,并且在所有其他任务中都正常工作。
所有变量都设置好了。我包括了
console.log(content)
来检查进程函数是否实际运行-似乎不是,所以我猜这是基本语法错误。有一个答案(https://stackoverflow.com/a/28600474/1087660),似乎解决了这个问题,但据我所知,那种做法只是不好的JS语法-不确定它是如何被标记为正确的。
运行复制任务的
--verbose
输出:Running "copy:js" (copy) task
Verifying property copy.js exists in config...OK
Files: src/wp-content/themes/pilau-starter/js/admin.js -> public/wp-content/themes/pilau-starter/js/admin.js
Files: src/wp-content/themes/pilau-starter/js/flickity.js -> public/wp-content/themes/pilau-starter/js/flickity.js
Files: src/wp-content/themes/pilau-starter/js/global.js -> public/wp-content/themes/pilau-starter/js/global.js
Files: src/wp-content/themes/pilau-starter/js/modernizr.js -> public/wp-content/themes/pilau-starter/js/modernizr.js
Files: src/wp-content/themes/pilau-starter/js/picturefill.js -> public/wp-content/themes/pilau-starter/js/picturefill.js
Files: src/wp-content/themes/pilau-starter/js/respond.js -> public/wp-content/themes/pilau-starter/js/respond.js
Options: processContent=false, processContentExclude=[], process=undefined
Options: processContent=false, processContentExclude=[], process=undefined
Copying src/wp-content/themes/pilau-starter/js/admin.js -> public/wp-content/themes/pilau-starter/js/admin.js
Reading src/wp-content/themes/pilau-starter/js/admin.js...OK
Writing public/wp-content/themes/pilau-starter/js/admin.js...OK
--verbose
标志运行任务,输出是什么?此外,在调试时,nonull
选项可能会有所帮助。 - steveaxgrunt-contrib-copy
?你可以尝试使用processContent
而不是process
,因为它在 v0.4.1 及更早版本中使用。你也可以尝试在控制台记录你的breakpoints.large
和breakpoints.medium
,也许它们在你的配置中没有正确设置... - nemesv--verbose
参数来增加详细输出。 - JamessrcThemeDir
,因为如果复制未找到文件,则不执行进程,并且您将得不到日志,就像您所描述的那样。您在哪里定义这些变量? - Xavier Priour--verbose
输出 - 我猜process=undefined
是问题所在?其他所有东西都能正常工作 - 我的编辑已经成功从 src/ 复制到 /public。breakpoints
也能正常记录。 - Steve Taylor