Last active
April 19, 2020 01:47
-
-
Save ClydeDz/b995f6f3338173c49ac24a10db57662a to your computer and use it in GitHub Desktop.
gulp-replace demo - Medium article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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