-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
148 lines (142 loc) · 4.85 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
'use strict';
const path = require('path');
const fs = require('fs')
const spawn = require('child_process').spawn;
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass')(require('sass'));
var sourcemaps = require('gulp-sourcemaps');
var autoprefixer = require('gulp-autoprefixer');
var nunjucksRender = require('gulp-nunjucks-render');
var data = require('gulp-data');
var webpack = require('webpack-stream');
var stylelint = require('gulp-stylelint');
var beautify = require('gulp-beautify');
//
const scssPaths = [
"./src/sass/bootsarc.bootstrap.scss"
];
// ======================================================
// GULP TASK
// ======================================================
gulp.task('html:build', () => {
var templateData = {}
try { templateData = JSON.parse(fs.readFileSync('./src/templateData.json')); } catch (error) {}
var getData = function(file) {
return {
...templateData,
file
};
}
var manageEnv = function(environment) {
Object.keys(templateData.dirs).forEach(function(key) {
environment.addFilter(key, url => `/${templateData.dirs[key]}/${url}`);
});
environment.addFilter('slug', function(str) {
return str && str.replace(/\s/g, '-', str).toLowerCase();
});
}
return gulp.src('./src/pages/**/*.+(html|nunjucks)')
.pipe(data(getData))
.pipe(nunjucksRender({
manageEnv,
path: ['./src/layouts']
}))
.pipe(gulp.dest('pages'))
.pipe(browserSync.stream())
});
gulp.task('css:build', () =>
gulp.src(scssPaths)
.pipe(sass({}))
.pipe(gulp.dest("dist/css/"))
.pipe(browserSync.stream())
);
gulp.task('css:build:prod', () =>
gulp.src(scssPaths)
.pipe(sass({
outputStyle: 'compressed'
}))
.pipe(autoprefixer())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest("dist/css/"))
.pipe(browserSync.stream())
);
gulp.task('js:build', () =>
gulp.src('./dist/js/', { allowEmpty: true })
.pipe(webpack(require('./webpack.config.js')))
.pipe(gulp.dest('./dist/js/'))
.pipe(browserSync.stream())
);
gulp.task('css:lint', () =>
gulp.src(scssPaths)
.pipe(stylelint({
failAfterError: true,
reporters: [
{ formatter: 'string', console: true }
],
debug: true
}))
);
gulp.task('html:format', () => {
return gulp.src('./pages/**/*.html')
.pipe(beautify.html({
"indent_size": "2",
"indent_char": " ",
"max_preserve_newlines": "-1",
"preserve_newlines": false,
"keep_array_indentation": false,
"break_chained_methods": false,
"indent_scripts": "normal",
"brace_style": "collapse",
"space_before_conditional": true,
"unescape_strings": false,
"jslint_happy": false,
"end_with_newline": true,
"wrap_line_length": "0",
"indent_inner_html": false,
"comma_first": false,
"e4x": true,
"indent_empty_lines": false
}))
.pipe(gulp.dest('./pages'))
})
gulp.task('watching', gulp.series('css:lint', 'js:build', 'css:build', 'html:build', function() {
browserSync.init({
open: false,
ui: {
port: 4000
},
server: {
baseDir: "./",
index: "index.html",
},
// middleware: function(req, res, next) {
// if (req.url === '/') {
// res.writeHead(301, { Location: '/pages/index.html' });
// res.end();
// }
// return next();
// }
});
gulp.watch("./src/scripts/**/*.js", gulp.series('js:build'));
gulp.watch("./src/sass/**/*.scss", gulp.series('css:lint', 'css:build'));
gulp.watch("./src/**/*.+(html|nunjucks)", gulp.series('html:build'));
gulp.watch("./src/templateData.json", gulp.series('html:build'));
gulp.watch("./pages/**/*.html").on('change', browserSync.reload);
gulp.watch("./assets/**/*.*").on('change', browserSync.reload);
}));
// ======================================================
// GULP COMMAND
// ======================================================
gulp.task('default', gulp.series('watching'));
gulp.task('build', gulp.series('css:lint', 'js:build', 'css:build:prod', 'html:build', 'html:format'));
gulp.task('autoreload', function() {
var p;
gulp.watch('gulpfile.js', spawnChildren);
spawnChildren();
function spawnChildren(callback) {
if(p) { p.kill(); }
p = spawn('gulp', ['default'], { stdio: 'inherit' });
if(callback) { callback(); }
}
});