-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
172 lines (148 loc) · 4.65 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
var path = require('path');
var fs = require('fs');
var exists = require('fs-exists-sync')
var gulp = require('gulp');
var clean = require('gulp-clean');
var gutil = require('gulp-util');
var data = require("gulp-data");
var rename = require("gulp-rename");
var uglify = require('gulp-uglify-es').default;
var minify = require('gulp-minify-css');
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync').create();
var nunjucksRender = require('gulp-nunjucks-render');
var importOnce = require('node-sass-import-once');
var sass = require('gulp-sass');
sass.compiler = require('node-sass');
var flatten = require('gulp-flatten');
var concat = require('gulp-concat-multi');
var browserify = require('gulp-browserify');
var argv = require('yargs').argv;
var src = "./src";
var dist = "./dist";
var config = require('./config')(src);
const env = (argv.env || 'dev').trim().toLowerCase();
function noop() {
return;
}
function getDataForFile(file) {
var data = {};
var file = path.parse(file.relative).name;
try {
data = JSON.parse(fs.readFileSync(path.join('./src/html/pages', file + ".json")));
} catch (err) {
try {
data = require('./src/html/pages/' + file);
} catch (err) {}
}
var default_data = {
page_name : file,
assets: function(path) {
return "assets/" + path;
},
isPageCssExists : function(page_name){
return exists(src+"/html/pages/"+page_name+"/"+page_name+".scss");
}
}
return Object.assign(data, default_data);
}
gulp.task('clean', function() {
return gulp.src(dist).pipe(clean({
force: true
}));
});
gulp.task('copy', ['clean'], function() {
return gulp.src(['./src/assets/**/*.*'])
.pipe(gulp.dest(dist+"/assets"))
.pipe(browserSync.reload({stream: true}));
});
gulp.task('sass', ['clean'], function() {
var options = {
includePaths: [
path.join(src, 'scss/common'),
'node_modules'
],
sourceMap: true,
importer: importOnce,
importOnce: {
css: true,
}
}
var bootstrap = concat(config.css)
.pipe(autoprefixer());
if (env == 'prod') {
bootstrap.pipe(minify());
}
bootstrap.pipe(gulp.dest(dist + '/assets/css/'))
.pipe(browserSync.reload({stream: true }));
var css = gulp.src(['./src/scss/css/*.css'])
.pipe(autoprefixer())
.pipe(flatten())
if (env == 'prod') {
css.pipe(minify());
}
css.pipe(gulp.dest(dist + '/assets/css/'))
.pipe(browserSync.reload({stream: true }));
var site = gulp.src(['./src/scss/styles.scss', './src/html/pages/**/*.scss'])
.pipe(sass(options).on('error', sass.logError))
.pipe(autoprefixer())
.pipe(flatten())
if (env == 'prod') {
site.pipe(minify());
}
site.pipe(gulp.dest(dist + '/assets/css/'))
.pipe(browserSync.reload({stream: true }));
return Promise.all([bootstrap, css, site]);
});
gulp.task('js', ['clean'], function() {
var ignore = [
'popper.js'
];
var bootstrap = concat({
"vendor.js" : [path.join(src, 'js/vendor.js') ],
"app.js" : [path.join(src, 'js/app.js') ],
})
.pipe(browserify({
ignore: ignore
}))
if (env == 'prod') {
bootstrap.pipe(uglify());
}
bootstrap.pipe(gulp.dest(dist + '/assets/js/'))
.pipe(browserSync.reload({stream: true }));
var site = gulp.src(['./src/js/*.js', ])
.pipe(browserify({
ignore: ignore
}))
.pipe(flatten())
if (env == 'prod') {
site.pipe(uglify())
}
site
.pipe(gulp.dest(dist + '/assets/js'))
.pipe(browserSync.reload({stream: true }));
return Promise.all([site]);
});
gulp.task('html', ['clean', 'copy', 'sass', 'js'], function() {
return gulp.src('./src/html/pages/**/*.+(html|nunjucks)').pipe(data(getDataForFile)).pipe(nunjucksRender({
path: ['src/html/'] // String or Array
}))
.pipe(flatten())
.pipe(gulp.dest(dist))
.pipe(browserSync.reload({stream: true }));
});
gulp.task('build', ['html']);
gulp.task('browser-sync', ['build'], function() {
browserSync.init({
server: dist,
watch: true,
});
});
gulp.task('watch', ['browser-sync'], function() {
gulp.watch('./src/**/*', ['build']);
//gulp.watch('./src/html/*', ['html']);
//gulp.watch('./src/assets/*', ['assets'])
gulp.watch(dist).on('change', browserSync.reload);
});
gulp.task('default', ['watch']);