-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
119 lines (104 loc) · 3.09 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const gulp = require('gulp'),
fs = require('fs'),
path = require('path'),
del = require('del'),
uglify = require('gulp-uglify'),
pump = require('pump'),
cleanCSS = require('gulp-clean-css'),
paths = { dist: 'dist' };
/**
* function to get separator based on OS format
* @param file {String} path to current file
* @return {String}
*/
var getSeparator = function(file) {
var separator = '/';
if (file.path.indexOf(separator) === -1) {
separator = '\\';
}
return separator;
};
/**
* function to get destination folder for a file
* @param folder {String} path to destination folder
* @return {String}
*/
var getDestinationFolder = function(folder) {
return path.join(__dirname + '/' + folder);
};
gulp.task('copy:static:scripts', function() {
return gulp.src([
'static/scripts/**/*.js',
'static/scripts/**/*.css'
])
.pipe(gulp.dest(function(file) {
return getDestinationFolder(paths.dist + '/scripts');
}));
});
gulp.task('copy:static:images', function() {
return gulp.src([
'static/images/**/*.png',
'static/images/**/*.jpg',
'static/images/**/*.jpeg',
'static/images/**/*.gif',
])
.pipe(gulp.dest(function(file) {
return getDestinationFolder(paths.dist + '/images');
}));
});
gulp.task('copy:static:fonts', function() {
return gulp.src([
'static/fonts/**/*.eot',
'static/fonts/**/*.svg',
'static/fonts/**/*.ttf',
'static/fonts/**/*.woff',
'static/fonts/**/*.woff2',
])
.pipe(gulp.dest(function(file) {
return getDestinationFolder(paths.dist + '/fonts');
}));
});
gulp.task('copy:static:css', function() {
return gulp.src([
'static/styles/**/*'
])
.pipe(gulp.dest(function(file) {
return getDestinationFolder(paths.dist + '/styles');
}));
});
gulp.task('update:index', function() {
fs.readFile(paths.dist + '/index.html', "utf-8", function(err, _data) {
let pugFile = 'views/index.pug';
let scripts = _data.match(/<script\b[^>]*>([\s\S]*?)<\/script>/gm);
del([paths.dist + '/index.html']);
fs.readFile(pugFile, "utf-8", function(err, _data) {
_data = _data.substring(0, _data.indexOf('<!-- injected content -->') + 25);
for (let i = 0; i < scripts.length; i++) {
_data += '\r\n ' + scripts[i].replace('src="', 'src="/');
}
fs.writeFile(pugFile, _data, function(err) {
if(err) {
return console.log(err);
}
});
});
});
});
gulp.task('compressjs', ['copy:static:scripts'], function (cb) {
pump([
gulp.src(paths.dist + '/scripts/**/*.js'),
uglify(),
gulp.dest(function (file) {
return file.base;
})
],cb );
});
gulp.task('compresscss', ['copy:static:css'], function () {
return gulp.src(paths.dist + '/styles/**/*.css')
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(gulp.dest(function (file) {
return file.base;
}));
});
gulp.task('copy', ['copy:static:scripts', 'copy:static:fonts', 'copy:static:css', 'copy:static:images']);
gulp.task('default', ['copy', 'update:index', 'compressjs', 'compresscss']);