【问题标题】:grunt-contrib-copy syntax for process option confusion用于进程选项混淆的 grunt-contrib-copy 语法
【发布时间】:2015-06-07 21:33:46
【问题描述】:

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

【问题讨论】:

  • 如果您使用--verbose 标志运行任务,输出是什么?此外,nonull 选项在调试时也很有帮助。
  • 您使用的是哪个版本的grunt-contrib-copy?您可以尝试使用 processContent 而不是 process,因为它在 v0.4.1 及更早版本中使用过。您也可以尝试控制台记录您的 breakpoints.large 和 breakpoints.medium 可能它们在您的配置中设置不正确...
  • 运行时文件是否被复制到输出? +1 --verbose 建议。
  • 我测试了您的 Gruntfile 提取(使用硬编码变量),它按预期完美运行(替换时复制,记录内容)。所以我会怀疑一个不正确的变量,很可能srcThemeDir 好像复制没有找到文件,进程没有执行并且你没有得到日志,正如你所描述的。你在哪里定义变量?
  • 很抱歉在其中包含未声明的变量,我已经编辑并用实际路径替换了它们。这些路径适用于其他一切。我还添加了--verbose 输出 - 我猜process=undefined 是问题所在?其他一切正常 - 我的编辑从 src/ 复制到 /public 很好。 breakpoints 记录正常。

标签: gruntjs grunt-contrib-copy


【解决方案1】:

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

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

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

processContent 被记录为 undefined 因为 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;
    };

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 抱歉,忘记切换了。我已经用它设置的值替换了它。 var 适用于其他一切。另请参阅上面编辑中的--verbose 输出 - 似乎找到了文件。我所做的任何编辑都会被复制 - 只是没有发生替换。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 2018-02-13
    相关资源
    最近更新 更多