6

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

2 回答 2

2

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

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

原创内容 替换内容

现在,当你运行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;
    };
于 2015-06-07T13:33:46.987 回答
0

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

于 2015-06-05T14:32:48.857 回答