gpt4 book ai didi

gruntjs - 用于进程选项混淆的 grunt-contrib-copy 语法

转载 作者:行者123 更新时间:2023-12-04 15:42:59 24 4
gpt4 key购买 nike

我试图在复制时替换不同文件中的一些占位符。我的 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 (公共(public)目录没有提交给 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

最佳答案

您的 版本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,所以被记录为 undefined记录一个值。和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;
};

关于gruntjs - 用于进程选项混淆的 grunt-contrib-copy 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30530160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com