javascript - 使用 Gulp 将字符串替换为数组中的值
问题描述
我正在尝试指示gulp-replaceflexes[i][0]
将my中给定字符串的所有实例替换files
为两个值之一flexes[i][1]
或flexes[i][2]
.
我的设置
const gulp = require('gulp'),
replace = require('gulp-replace'),
files = {
input: 'input/*.html',
output: 'output/'
},
flexes = [
[
"Old 1",
"New 1A",
"New 1B"
],[
"Old 2",
"New 2A",
"New 2B"
]
]
我的功能
对于所有输入文件,函数应将所有Old替换为:
- 如果传递的模式参数为 1,则为新 A或
- 如果传递的模式参数为 2,则为新 B
并保存到输出文件夹。
function repla(mode) {
return gulp
.src(files.test_htm)
.pipe(
flexes.forEach(
element => replace(
element[0],
element[mode]
)
)
)
.pipe(gulp.dest(files.test))
}
exports.prev = repla(1)
exports.prod = repla(2)
exports.default = repla(1)
错误
我的解决方案产生错误
TypeError: Cannot read property 'on' of undefined
at DestroyableTransform.Readable.pipe (/node_modules/readable-stream/lib/_stream_readable.js:564:8)
at repla (/gulpfile.js:39:10)
at default (/node_modules/undertaker/lib/set-task.js:13:15)
at bound (domain.js:426:14)
at runBound (domain.js:439:12)
at asyncRunner (/node_modules/async-done/index.js:55:18)
at processTicksAndRejections (internal/process/task_queues.js:79:11)
我应该如何解决它?
解决方案
这是一种不需要任何类型的 for 循环的方法。它使用gulp-replace
' 获取函数的能力:
// this form of your replaces is much easier to work with
let flexes = {
"Old 1": ["New 1A", "New 1B"],
"Old 2": ["New 2A", "New 2B"]
};
function replaceStrings(mode) {
return gulp.src(files.input)
.pipe(replace(/Old 1|Old 2/g, function (match) {
// console.log(flexes[match][mode]); // e.g., "New 1A"
return flexes[match][mode];
}))
.pipe(gulp.dest('build/'));
};
exports.prev = gulp.series(() => replaceStrings(0));
exports.prod = gulp.series(() => replaceStrings(1));
这exports.prev = gulp.series(() => replaceStrings(0));
是将参数传递给任务函数的简单方法。
致电:gulp prev
或gulp prod
这种方法确实要求您能够制作一个匹配所有要替换的字符串的正则表达式,如/Old 1|Old 2/g
. 这对您来说可能是不可能的,但它可以简单地/string1|string2|string3/g
或更复杂。显然,如果你有很多这样的for..in
循环可能是必要的(但我不会使用forEach
循环 - 搜索 SO 以查找使用forEach
andgulp
一起的问题)。
修改后的flexes
数据结构,允许一个非常简单的替换功能:
return flexes[match][mode];
我个人认为更好的方法是在命令行中使用 args。
const yargs = require('yargs');
const argv = yargs.argv;
function replaceStrings() {
return gulp.src(files.input)
.pipe(replace(/Old 1|Old 2/g, function (match) {
// console.log(argv.mode); // 0 or 1
return flexes[match][argv.mode];
}))
.pipe(gulp.dest('build/'));
};
exports.replace = gulp.series(replaceStrings);
致电:gulp replace --mode=0
或gulp replace --mode=1
推荐阅读
- python - 无法让python加载json文件
- openssl - RSA_generate_key_ex 函数调用中的 RSA 密钥生成错误
- wordpress - 如何禁用
在wordpress中没有禁用标签的标签 - haskell - 对于具有嵌套 Maybe 值的 Tree 数据类型,Traversable 实例应该是什么样子?
- reactjs - 输入标签不在选择下拉框的中心
- excel - 同一范围内的最后 2 行
- python - 解决问题 - 编码面试问题?
- sql - 如何查询表中的传递匹配?
- c# - 如何在异步数据库连接期间避免“访问冲突”?
- linux - 从数据文件的每一行中提取 2 个字段并插入到外部程序中