Skip to content

Instantly share code, notes, and snippets.

@ClydeDz
Last active April 19, 2020 01:47
Show Gist options
  • Save ClydeDz/b995f6f3338173c49ac24a10db57662a to your computer and use it in GitHub Desktop.
Save ClydeDz/b995f6f3338173c49ac24a10db57662a to your computer and use it in GitHub Desktop.
gulp-replace demo - Medium article
var gulp = require('gulp');
var replace = require('gulp-replace');
var fs = require('fs');
function getCSSFilename(linkTag) {
var hrefValue = /href\=\"([A-Za-z0-9/._]*)\"/g;
var cssFilename = linkTag.match(hrefValue);
cssFilename = cssFilename[0].replace("href=\"", "").replace("\"", "");
return cssFilename;
}
gulp.task('inject-styles', function () {
return gulp.src("./dist/index.html")
.pipe(replace(/<link rel="stylesheet" href="[^"]*"*>/g, function(linkTag) {
var style = fs.readFileSync(`.${getCSSFilename(linkTag)}`, 'utf8');
return '<style>\n' + style + '\t</style>';
}))
.pipe(gulp.dest('./dist'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment