2015-05-29 58 views
6

我试图在复制时将一些占位符替换为不同的文件。我的gruntfile工作正常,但在进程选项中添加替换项,它只是不起作用。下面是我gruntfile的相关章节:流程选项混乱的grunt-contrib-copy语法

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(public目录不承诺回购,因为这是一个入门的主题)。这些路径是我原始的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 
+1

如果您运行带有'--verbose'标志的任务有什么输出方法reponsible?另外,'nonull'选项在调试时会很有帮助。 – steveax

+0

您使用的是哪个版本的'grunt-contrib-copy'?您可以尝试使用'processContent'而不是'process',因为它已在v0.4.1及更早版本中使用。你也可以尝试控制台登录你的'breakpoints.large'和'breakpoints.medium',可能它们没有在你的配置中正确设置... – nemesv

+0

运行时文件是否被复制到输出? +1 - 关于'--verbose'的建议。 – James

回答

2

您的版本grunt-contrib-copy是0.4.0。正如由@nemesv正确地指出在此版本中使用的属性名称将是processContent而不是process

我克隆你的回购,并切换到json-breakpoints分支。并运行grunt copy:js,它取代了内容。因为咕噜使用JSON.stringify登录值

original-content replaced-content

现在,当你运行grunt copy:js --verbose它仍然会显示此

cli

processContent登录不确定的。当你传递一个函数定义时,JSON.stringify返回undefined


如果你有兴趣,这里是为记录所有选项

Log.prototype.writeflags = function(obj, prefix) { 
     var wordlist; 
     if (Array.isArray(obj)) { 
      wordlist = this.wordlist(obj); 
     } else if (typeof obj === 'object' && obj) { 
      wordlist = this.wordlist(Object.keys(obj).map(function(key) { 
       var val = obj[key]; 
       return key + (val === true ? '' : '=' + JSON.stringify(val)); 
      })); 
     } 
     this._writeln((prefix || 'Flags') + ': ' + (wordlist || '(none)'.cyan)); 
     return this; 
    }; 
0

这似乎并非是与所有的process选项的问题,但更多的与srcThemeDir问题。我会记录它以确保您确切知道它是什么,因为它似乎导致copy任务找不到任何文件(因此不会调用过程函数)。

+0

对不起,忘了切换。我用它设置的值替换了它。该变种适用于其他一切。在上面的编辑中也可以看到'--verbose'输出 - 文件被发现OK。我所做的任何修改都被复制 - 只是替换不会发生。 –