Skip to content

Commit dd5710f

Browse files
authored
Initial commit with demo files
0 parents  commit dd5710f

File tree

9 files changed

+4922
-0
lines changed

9 files changed

+4922
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Gulp demo
2+
Compiles Sass files into minified CSS, automaticaly adds the CSS files as `<link>` tags into the HTML file, then uses these CSS links to inject the contents of the CSS file into the HTML file.

dist/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Hello</title>
5+
<!-- inject:css -->
6+
<style>
7+
body{color:blue}
8+
</style>
9+
<!-- endinject -->
10+
</head>
11+
<body>
12+
Hello World
13+
</body>
14+
</html>

gulpfile.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var gulp = require('gulp');
2+
var replace = require('gulp-replace');
3+
var inject = require('gulp-inject');
4+
var sass = require('gulp-sass');
5+
var fs = require('fs');
6+
7+
gulp.task('compile-sass', function() {
8+
return gulp.src('./src/sass/*.scss')
9+
.pipe(sass({outputStyle: 'compressed'}))
10+
.pipe(gulp.dest('./src/css'));
11+
});
12+
13+
gulp.task('add-styles', function () {
14+
var target = gulp.src('./src/index.html');
15+
var sources = gulp.src(['./src/css/*.css'], {read: false, relative:true, removeTags: true});
16+
return target.pipe(inject(sources))
17+
.pipe(gulp.dest('./dist'));
18+
});
19+
20+
function getCSSFilename(linkTag) {
21+
var hrefValue = /href\=\"([A-Za-z0-9/._]*)\"/g;
22+
var cssFilename = linkTag.match(hrefValue);
23+
cssFilename = cssFilename[0].replace("href=\"", "").replace("\"", "");
24+
return cssFilename;
25+
}
26+
27+
gulp.task('inject-styles', function () {
28+
return gulp.src("./dist/index.html")
29+
.pipe(replace(/<link rel="stylesheet" href="[^"]*"*>/g, function(linkTag) {
30+
var style = fs.readFileSync(`.${getCSSFilename(linkTag)}`, 'utf8');
31+
return '<style>\n' + style + '\t</style>';
32+
}))
33+
.pipe(gulp.dest('./dist'));
34+
35+
});
36+
37+
gulp.task('build', gulp.series('compile-sass', 'add-styles', 'inject-styles'));

0 commit comments

Comments
 (0)