我正在尝试使用grunt-contrib-copy(版本0.4.1)在构建过程中重写文件中的一些字符串。作为测试,我可以复制文件,因此它不是权限或位置问题。
这些是fileProcessor任务(用Coffeescript语法)复制文件的选项:
fileProcessor:
files: [
expand: true
cwd: "target/js/"
dest: "target/js/"
src: ["**/test-*.js"]
rename: (dest, src) ->
grunt.verbose.writeln src, " => ", dest + src.substring(0, src.lastIndexOf("/") + 1) + "foo.js"
dest + src.substring(0, src.lastIndexOf("/") + 1) + "foo.js"
]
我得到以下输出,一切按预期工作:
Running "copy:fileProcessor" (copy) task
Verifying property copy.fileProcessor exists in config...OK
test-25fd6a1c3a890933.js => target/js/foo.js
Files: target/js/test-25fd6a1c3a890933.js -> target/js/foo.js
Options: processContent=false, processContentExclude=[]
Options: processContent=false, processContentExclude=[]
Copying target/js/test-25fd6a1c3a890933.js -> target/js/foo.js
Reading target/js/test-25fd6a1c3a890933.js...OK
Writing target/js/foo.js...OK
Copied 1 files
到目前为止一切顺利。很明显,grunt-contrib-copy对文件本身没有任何问题。所以我用一个流程任务替换重命名任务,并使用一个简单的正则表达式来查看它是否有效:
fileProcessor:
files: [
expand: true
cwd: "target/js/"
dest: "target/js/"
src: ["**/test-*.js"]
processContent: (content, src) ->
re = new RegExp("(angular)+")
content.replace(re, "foo")
]
产生以下输出:
Running "copy:fileProcessor" (copy) task
Verifying property copy.fileProcessor exists in config...OK
Files: target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js -> target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js
Options: processContent=false, processContentExclude=[]
Options: processContent=false, processContentExclude=[]
Copying target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js -> target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js
Reading target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js...OK
Writing target/working/shared/scripts/bootstrap-25fd6a1c3a890933.js...OK
Copied 1 files
所以grunt-contrib-copy找到我需要处理的文件,但处理永远不会发生。 processContent保持设置为默认值false。
查看copy.js,当调用grunt.file.copy(src, dest, copyOptions)
时,copyOptions是一个只有两个属性的对象:
所以我的流程选项永远不会传递。关于为什么会这样的任何想法?
答案 0 :(得分:4)
您的processContent位于错误的位置,需要将其包装到选项中并向上移动一级。另请注意,processContent已折旧并由流程
替换fileProcessor:
files: [
expand: true
cwd: "target/js/"
dest: "target/js/"
src: ["**/test-*.js"]
]
options:
process: (content, src) ->
re = new RegExp("(angular)+")
content.replace(re, "foo")