-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
executable file
·200 lines (150 loc) · 6.26 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
// -----------------------------------------------------------------------------
// Dependencies
// -----------------------------------------------------------------------------
var gulp = require('gulp');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var sassdoc = require('sassdoc');
var browserSync = require('browser-sync').create();
var nunjucksRender = require('gulp-nunjucks-render');
var data = require('gulp-data');
var concat = require('gulp-concat');
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
// var jsonSass = require('gulp-json-sass')
const tap = require('gulp-tap');
const markdown = require('gulp-markdownit');
const grayMatter = require('gulp-gray-matter');
const path = require('path');
const fs = require('fs');
// -----------------------------------------------------------------------------
// Configuration
// -----------------------------------------------------------------------------
var siteOutput = './docs';
var input = './src/scss/**/*.scss';
var inputMain = './src/scss/main.scss';
var output = siteOutput + '/css';
var inputPages = './src/njx-pages/*.html';
var inputData = './src/data/data.json';
var inputTemplates = './src/njx-templates';
var inputScripts = './src/js/';
var sassOptions = { outputStyle: 'expanded' };
var autoprefixerOptions = { browsers: ['last 2 versions', '> 5%', 'Firefox ESR'] };
var sassdocOptions = { dest: siteOutput + '/sassdoc' };
var inputFonts = './src/fonts/**/*.*';
// -----------------------------------------------------------------------------
// Markdown
// -----------------------------------------------------------------------------
const CONTENT = 'src/markdown/**/*.md';
// Moves content to {{ content }} and writes template to file
function useTemplate(file) {
const { data } = file,
template = fs.readFileSync(path.resolve(inputTemplates, data.template));
data.contents = file.contents;
file.contents = Buffer.from(template);
return;
}
//// Generates static site from markdown
gulp.task('markdown', () => {
return gulp.src(CONTENT)
.pipe(grayMatter())
.pipe(markdown())
.pipe(tap(useTemplate))
.pipe(nunjucksRender({ path: inputTemplates }))
.pipe(gulp.dest(siteOutput));
});
// -----------------------------------------------------------------------------
// Nunjucks
// -----------------------------------------------------------------------------
gulp.task('pages', () => {
return gulp.src(inputPages)
.pipe(data(function() {
return require(inputData)
}))
.pipe(nunjucksRender({ path: inputTemplates }))
.pipe(gulp.dest(siteOutput));
});
// -----------------------------------------------------------------------------
// Sass compilation
// -----------------------------------------------------------------------------
gulp.task('sass', function() {
return gulp
// .src(inputData, inputMain)
// .pipe(jsonSass({sass: false}))
// .pipe(concat('output.scss'))
.src(inputMain)
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(autoprefixer(autoprefixerOptions))
.pipe(gulp.dest(output))
.pipe(browserSync.stream());
});
// -----------------------------------------------------------------------------
// Javascript
// -----------------------------------------------------------------------------
gulp.task('scripts', function() {
return gulp.src(inputScripts + '*.js')
// .pipe(concat({ path: 'main.js'}))
.pipe(browserSync.reload({stream:true}))
.pipe(gulp.dest(siteOutput + '/js/'));
});
// -----------------------------------------------------------------------------
// Imagemin
// -----------------------------------------------------------------------------
gulp.task('img', function() {
return gulp.src('./img/**/*')
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngquant()]
}))
.pipe(gulp.dest(siteOutput + '/img'));
});
// -----------------------------------------------------------------------------
// Fonts
// -----------------------------------------------------------------------------
gulp.task('fonts', function() {
return gulp.src([inputFonts])
.pipe(gulp.dest(siteOutput + '/fonts/'));
});
// -----------------------------------------------------------------------------
// Sass documentation generation
// -----------------------------------------------------------------------------
gulp.task('sassdoc', function() {
return gulp
.src(input)
.pipe(sassdoc(sassdocOptions))
.resume();
});
// -----------------------------------------------------------------------------
// Watchers
// -----------------------------------------------------------------------------
gulp.task('watch', function() {
// Watch the sass input folder for change,
// and run `sass` task when something happens
gulp.watch(input, ['sass']).on('change', function(event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});
// reload on js change
gulp.watch(inputScripts + '/*', ['scripts']).on('change', browserSync.reload);
// reload on font change
gulp.watch(inputFonts, ['fonts']).on('change', browserSync.reload);
// reload on json data change
// gulp.watch([inputData], ['nunjucks']).on('change', browserSync.reload); // NOT WORKING TO INJECT NEW DATA
// Watch nunjuck templates and reload browser if change
gulp.watch([inputPages,inputTemplates + '**/*.html'], ['pages']).on('change', browserSync.reload);
});
// -----------------------------------------------------------------------------
// Static server
// -----------------------------------------------------------------------------
gulp.task('browser-sync', function() {
browserSync.init({
server: {
baseDir: siteOutput
}
});
});
// -----------------------------------------------------------------------------
// Default task
// -----------------------------------------------------------------------------
gulp.task('default', ['fonts','sass', 'markdown', 'pages', 'img', 'scripts', 'watch', 'browser-sync']);