我正在尝试使用 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是一个只有两个属性的对象:
- 过程,这是错误的,并且
- noProcess,它是一个空数组。
所以我的流程选项永远不会传递。关于为什么会这样的任何想法?