https://github.com/catdad/gulp-file-inject
A Gulp task to do a regex replace with dynamic content based on the source file
https://github.com/catdad/gulp-file-inject
Last synced: about 2 months ago
JSON representation
A Gulp task to do a regex replace with dynamic content based on the source file
- Host: GitHub
- URL: https://github.com/catdad/gulp-file-inject
- Owner: catdad
- Created: 2015-04-01T01:51:47.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-06-27T01:52:09.000Z (almost 10 years ago)
- Last Synced: 2025-03-17T12:06:22.762Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 141 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gulp-file-inject
[![Downloads][1]][2] [![Version][3]][2] [![ISC License][4]][5]
[1]: https://img.shields.io/npm/dm/gulp-file-inject.svg?style=flat
[2]: https://www.npmjs.com/package/gulp-file-inject[3]: https://img.shields.io/npm/v/gulp-file-inject.svg
[4]: https://img.shields.io/npm/l/gulp-file-inject.svg?style=flat
[5]: http://opensource.org/licenses/ISC## Install:
npm install -D gulp-file-inject
## Use:var gulp = require('gulp');
var inject = require('gulp-file-inject');
var fs = require('fs');
function replacer(filepath, filename, callback) {
// template to inject
var str = "var template = '{{template}}'";
// get the html file by the same name
fs.readFile(filename.replace('.js', '.html'), function(err, file) {
if (err) {
return callback(err);
}
// make sure you minify the html first
file = file.replace(/\n\r/, ' ').replace("'", '"');
str = str.replace('{{template}}', file);
callback(undefined, str);
});
}
gulp.task('inject', function(){
return gulp.src('js/*.js')
.pipe(inject(/myregex/g, replacer))
.pipe(gulp.dest('build/js'));
});
## Notes:More docs are soon to come, I promise.
This is heavily based on [gulp-replace](https://github.com/lazd/gulp-replace) by [lazd](https://github.com/lazd).
[](https://github.com/igrigorik/ga-beacon)