grunt-contrib-copy语法中关于process选项的混淆

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

1
如果您使用 --verbose 标志运行任务,输出是什么?此外,在调试时,nonull 选项可能会有所帮助。 - steveax
你正在使用哪个版本的 grunt-contrib-copy?你可以尝试使用 processContent 而不是 process,因为它在 v0.4.1 及更早版本中使用。你也可以尝试在控制台记录你的 breakpoints.largebreakpoints.medium,也许它们在你的配置中没有正确设置... - nemesv
当程序运行时,文件是否被复制到输出目录?建议使用--verbose参数来增加详细输出。 - James
我测试了您的Gruntfile提取(使用硬编码变量),它按预期完美地工作(复制并替换,记录内容)。因此,我会怀疑一个不正确的变量,最有可能是srcThemeDir,因为如果复制未找到文件,则不执行进程,并且您将得不到日志,就像您所描述的那样。您在哪里定义这些变量? - Xavier Priour
很抱歉在代码中有未声明的变量,我已经编辑并用实际路径替换了它们。这些路径对其他所有内容都有效。我还添加了 --verbose 输出 - 我猜 process=undefined 是问题所在?其他所有东西都能正常工作 - 我的编辑已经成功从 src/ 复制到 /public。breakpoints 也能正常记录。 - Steve Taylor
2个回答

2
您使用的 grunt-contrib-copy 版本为 0.4.0。正如 @nemesv 所指出的,此版本中应该使用属性名称 processContent 而不是 process
我克隆了您的仓库并切换到 json-breakpoints 分支,然后运行了 grunt copy:js,它替换了内容。
现在,当您运行 grunt copy:js --verbose 时,它仍会显示如下: 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;
    };

0

这似乎与process选项无关,而更多地与srcThemeDir有关。我建议记录一下以确保您知道它是什么,因为它似乎导致copy任务找不到任何文件(因此不调用处理函数)。


抱歉,忘记切换了。我已经用它设置的值替换了它。对于其他所有内容,该变量都可以正常工作。此外,请参见上面的--verbose输出 - 似乎文件被正确找到了。我所做的任何编辑都会被复制 - 只是替换没有发生。 - Steve Taylor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接