流程选项混淆的grunt-contrib-copy语法

时间:2015-05-29 12:52:48

标签: gruntjs grunt-contrib-copy

我试图在复制时替换不同文件中的一些占位符。我的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

2 个答案:

答案 0 :(得分:2)

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

我克隆了您的回购并切换到json-breakpoints分支。并运行grunt copy:js并替换了内容。

original-content replaced-content

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

cli

未定义

processContent,因为grunt使用JSON.stringify来记录值。当你传递一个函数定义时,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;
    };

答案 1 :(得分:0)

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