1.0.0 • Published 8 years ago

gulp-replace-relative v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

js-standard-style

gulp-replace-relative

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.

Install

$ npm install --save-dev gulp-replace-relative

Usage

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)
						 })
})

Process file transformation ("like webpack loaders on the server")

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 + '"')
						 })
					 })
				 })
})

API

replaceRelative(pattern, replacer)

Returns a transform stream.

pattern

Type: string, regex

Defines the pattern to match.

replacer

Type: string, function

A defined function retrieves following parameters: replace(file, match, callback)

TODO

  • add tests