I'm trying to instruct gulp-replace to replace all the instances of given strings flexes[i][0] in my files into one of two values flexes[i][1] or flexes[i][2].
My setup
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"
]
]
My function
For all the input files, function should replace for all Old with:
- New A if the passed mode argument is 1 or
- New B if the passed mode argument is 2
and save to output folder.
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)
Error
My solution produces an error
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)
How should I fix it?
.pipeexpects a stream..forEachreturns undefined, which is being passed to pipe as a parameter, which is why you are getting that error.The way pipe works, is that you pass streams to
.pipefor it to chain input and output through. What you can do in this case is keep a reference to the gulp chain, and chain pipes on it with a loop:If you have a lot to replace and see performance problems, you can consider creating a concatenated regex using
|. You probably won't need it.