Replace matched content in files with support for relative paths
Similar to gulp-replace this plugin allows to change the files content and like gulp-replace-async it supports asynchronous tasks to retrieve data. It can be used for relative includes and source transformation on demand.
$ npm install --save-dev gulp-replace-relative
var replaceRelative = require('gulp-replace-relative')
gulp.task('replace', function(){
return gulp.src('index.js')
.pipe(replaceRelative(/pattern-(.*?)/g, function (file, match, callback) {
console.log(file, match)
})
})
gulp.task('replace', function(){
return gulp.src('index.js')
.pipe(replaceRelative(/require\('\.\/(.*?\.styl')\/g, function (file, match, callback) {
var includePath = match.match(/\('(.*?\.styl)'\)/)[1]
fs.readFile(path.resolve(path.dirname(file.path), includePath), function (error, data) {
var css = stylus(data.toString()).render()
postcss([
autoprefixer({
browsers: ['last 2 versions']
}),
cssnano
])
.process(css)
.then(function (result) {
return callback('"' + result.css + '"')
})
})
})
})
Returns a transform stream.
Type: string
, regex
Defines the pattern to match.
Type: string
, function
A defined function retrieves following parameters: replace(file, match, callback)
- add tests