【发布时间】:2015-06-07 21:33:46
【问题描述】:
我正在尝试在复制时替换不同文件中的一些占位符。我的 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选项在调试时也很有帮助。 -
您使用的是哪个版本的
grunt-contrib-copy?您可以尝试使用processContent而不是process,因为它在 v0.4.1 及更早版本中使用过。您也可以尝试控制台记录您的breakpoints.large和breakpoints.medium可能它们在您的配置中设置不正确... -
运行时文件是否被复制到输出? +1
--verbose建议。 -
我测试了您的 Gruntfile 提取(使用硬编码变量),它按预期完美运行(替换时复制,记录内容)。所以我会怀疑一个不正确的变量,很可能
srcThemeDir好像复制没有找到文件,进程没有执行并且你没有得到日志,正如你所描述的。你在哪里定义变量? -
很抱歉在其中包含未声明的变量,我已经编辑并用实际路径替换了它们。这些路径适用于其他一切。我还添加了
--verbose输出 - 我猜process=undefined是问题所在?其他一切正常 - 我的编辑从 src/ 复制到 /public 很好。breakpoints记录正常。
标签: gruntjs grunt-contrib-copy