-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
145 lines (133 loc) · 4.03 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
let gulp = require('gulp'),
readdir = require('recursive-readdir'),
sass = require('gulp-sass'),
fs = require('fs'),
template = require('gulp-template'),
browserSync = require('browser-sync').create(),
babel = require('gulp-babel'),
exec = require('child_process').exec,
uglify = require('gulp-uglify'),
jsonminify = require('gulp-jsonminify'),
replace = require('gulp-replace'),
htmlmin = require('gulp-htmlmin');
let pathToFolder = 'templates';
let generateData = (fs, scss, js, json) => {
let process = { css: '', form: '', js: '', json: ''};
process = generateCSS(process, scss, fs);
process = generateJS(process, js, fs);
process = generateJSON(process, json, fs);
return process;
}
let generateCSS = (process, scss, fs) => {
if (fs.existsSync(scss)) {
return Object.assign(process, {
css: sass.compiler.renderSync({
file: scss,
outputStyle: 'compressed'
}).css,
});
}
return process;
};
let generateJS = (process, js, fs) => {
if (fs.existsSync(js)) {
return Object.assign(process, {
js: fs.readFileSync(js, 'utf8'),
});
}
return process;
};
let generateJSON = (process, json, fs) => {
if (fs.existsSync(json)) {
return Object.assign(process, {
json: JSON.parse(fs.readFileSync(json, "utf8"))
});
}
return process;
};
let generateHTML = (process, html, fs) => {
if (fs.existsSync(html)) {
return Object.assign(process, {
html: fs.readFileSync(html, "utf8")
});
}
return process;
};
gulp.task('templates', function () {
readdir(pathToFolder, ['*.scss', '*.js', '*.json'], function (err, files) {
if(files){
files.forEach(function (file) {
// get file html
let data = file.substr(pathToFolder.length + 1).replace(String.fromCharCode(92), String.fromCharCode(47));
// get file based on its name
let index = data.lastIndexOf('/'),
path = data.substr(0, index),
scss = file.substr(0, file.lastIndexOf('.')) + '.scss',
js = file.substr(0, file.lastIndexOf('.')) + '.js',
json = file.substr(0, file.lastIndexOf('.')) + '.json';
let value = generateData(fs, scss, js, json);
return gulp.src(file)
.pipe(template(value))
.pipe(gulp.dest('src/' + path));
});
}else{
console.error('no files in templates folder');
}
});
});
gulp.task('reloading', function (done) {
browserSync.reload();
done();
});
gulp.task('scripts', function () {
return gulp.src('./library/*.js')
.pipe(babel({
presets: ['env'],
}))
.pipe(uglify())
.pipe(gulp.dest('src/library'));
});
gulp.task('json', function () {
return gulp.src('./content/*.json')
.pipe(jsonminify())
.pipe(gulp.dest('src/content'));
});
gulp.task('dist', function(){
return gulp.src('./src/*.html')
.pipe(replace('../bower_components/', '../'))
.pipe(gulp.dest('./'));
});
gulp.task('index', function () {
return gulp.src('./index/index.html')
.pipe(htmlmin({
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
ignoreCustomComments: [/^#/],
preserveLineBreaks: false,
removeComments: true
}))
.pipe(gulp.dest('./'));
});
gulp.task('default', ['templates', 'scripts', 'json', 'dist'], function () {
// exec('killall node');
exec('polymer serve --port 9081');
exec('browser-sync start --port 9000 --proxy 127.0.0.1:9081 --files \'src/**/*.html, src/**/*.js, images/*\' --online false --open false');
gulp.watch('templates/*', ['templates']);
gulp.watch('templates/**/*', ['templates']);
gulp.watch('library/*', ['scripts']);
gulp.watch('content/*', ['json']);
gulp.watch('src/*', ['dist']);
gulp.watch('./index/index.html', ['index']);
});
gulp.task('express', ['templates', 'scripts', 'index', 'dist'], function () {
// exec('killall node');
exec('polymer serve --port 9081');
exec('browser-sync start --port 9000 --proxy 127.0.0.1:9081 --files \'src/**/*.html, src/**/*.js, images/*\' --online false --open false');
gulp.watch('templates/*', ['templates']);
gulp.watch('templates/**/*', ['templates']);
gulp.watch('library/*', ['scripts']);
gulp.watch('content/*', ['json']);
gulp.watch('src/*', ['dist']);
gulp.watch('./index/index.html', ['index']);
});