-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
102 lines (93 loc) · 2.61 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const gulp = require('gulp');
const packages = require('/www/package.json');
const $ = require('gulp-load-plugins')({
config: packages
});
const browserSync = require('browser-sync').create();
const path = require('path');
var srcPath = 'demo/';
var config = {
html: srcPath + 'index.html',
sass: srcPath + 'scss/main.scss',
css: srcPath + 'css/main.css',
dest: srcPath + '',
postfixTemp: '-temp',
postfixOutput: '-final',
};
function errorlog (error) {
console.error.bind(error);
this.emit('end');
}
// Default Task
gulp.task('default', [
// 'inject',
// 'inline',
// 'compile',
'server',
'watch',
]);
// watch
gulp.task('watch', function () {
gulp.watch(config.sass, ['compile']);
gulp.watch(config.html, ['concat']);
gulp.watch('**/*.html').on('change', browserSync.reload);
});
// SASS Task
gulp.task('sass', function () {
return gulp.src(config.sass)
.pipe($.sourcemaps.init())
.pipe($.sass({
outputStyle: 'expanded',
precision: 7
}).on('error', $.sass.logError))
.pipe($.rename(function (path) {
path.extname = '.css';
return path;
}))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest(srcPath + 'css'))
.pipe(browserSync.stream());
});
function inlineStyles () {
return gulp.src(config.html.replace('.html', config.postfixTemp + '.html'))
.pipe($.inlineCss({
applyLinkTags: false,
removeLinkTags: true,
// applyStyleTags: false,
// removeStyleTags: false,
preserveMediaQueries: true
}))
.pipe($.replace(/(calc\()(\S+)(\+|-|\*|\/)(\S+)(\))/g, '$1$2 $3 $4$5'))
.pipe($.rename(function (path) {
path.basename = path.basename.replace(config.postfixTemp, config.postfixOutput);
}))
.pipe(gulp.dest(config.dest));
}
function injectStyles () {
return gulp.src(config.html)
.pipe($.inject(gulp.src(config.css), {
starttag: '/* css:css */',
endtag: '/* endinject */',
transform: function (filePath, file) { return file.contents.toString(); }
}))
.pipe($.rename(function (path) { path.basename += config.postfixTemp; }))
.pipe(gulp.dest(config.dest));
};
gulp.task('inject', () => { injectStyles() });
gulp.task('inline', () => { inlineStyles() });
gulp.task('pre-compile', ['sass'], () => { injectStyles() });
gulp.task('compile', ['pre-compile'], () => { inlineStyles() });
gulp.task('concat', ['inject'], () => { inlineStyles() });
// Server
gulp.task('server', () => {
browserSync.init({
server: { baseDir: './'},
ghostMode: {
clicks: false,
forms: false,
scroll: false
},
open: false,
notify: false
});
});