-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
208 lines (177 loc) · 5.57 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
201
202
203
204
205
206
207
208
const fs = require('fs');
const http = require('http');
const gulp = require('gulp');
const browserSync = require('browser-sync').create();
// const imagemin = require('gulp-imagemin');
const sass = require('gulp-sass');
sass.compiler = require('node-sass');
const uglify = require('gulp-uglify');
// const babel = require('gulp-babel');
const watch = require('gulp-watch');
const run = require('gulp-run-command').default;
const concat = require('gulp-concat');
const refresh = require('gulp-livereload');
const lr = require('tiny-lr');
const lrserver = lr();
const minifyCSS = require('gulp-minify-css');
const embedlr = require('gulp-embedlr');
const ecstatic = require('ecstatic');
const imagemin = require('gulp-imagemin');
const browserify = require('gulp-browserify');
const nunjucksRender = require('gulp-nunjucks-render');
const livereloadport = 35729, serverport = 9999;
// use npm tasks
async function npmBuild(cb) {
run('npm run build')();
cb();
};
exports.build = npmBuild;
async function copyFiles(cb) {
run('npm run build:copy')();
cb();
};
exports.copy_files = copyFiles;
async function npmClean(cb) {
run('npm run clean')();
cb();
};
exports.clean = npmClean;
async function npmFormat(cb) {
run('npm run format')();
cb();
}
exports.format = npmFormat;
// generate css from scss
function generateCSS(cb) {
gulp.src('./src/scss/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('dist/css'));
cb();
}
exports.css = generateCSS;
// helper function for browserSync
function bsInit() {
browserSync.init({
server: "dist",
port: 9999, // this can be any port, it will show our app
reloadDelay: 1000 // Important, otherwise syncing will not work
});
}
function sync() {
bsInit();
gulp.watch(['./dist/**/*.js', './dist/**/*.html', './dist/**/*.css']).on("change", browserSync.reload);
}
gulp.task('sync', sync);
gulp.task('default', gulp.series('sync'));
gulp.task('html', () =>
gulp.src('./src/*.html')
.pipe(gulp.dest('dist'))
);
gulp.task('sass', () =>
gulp.src('./src/sass/*.sass')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./dist/css'))
);
// gulp.task('babel', () =>
// gulp.src('./src/js/*.es6')
// .pipe(babel({
// presets: ['@babel/env']
// }))
// .pipe(gulp.dest('./src/js'))
// );
gulp.task('uglifyJS', () =>
gulp.src('./src/js/*.js')
.pipe(uglify())
.pipe(gulp.dest('dist/js'))
);
gulp.task('imagemin', () =>
gulp.src('./src/assets/images/*')
.pipe(imagemin())
.pipe(gulp.dest('./dist/assets/images'))
);
gulp.task('stream', () =>
gulp.watch('./src', () =>
gulp.series('html',
'sass',
// 'babel',
'uglifyJS',
// 'imagemin'
)
)
);
function renderTemplates() {
// Gets nunjucks files in pages
return gulp.src('src/[a-zA-Z]*.+(nunjucks|njk|tpl)')
// Renders template with nunjucks
// .pipe(data(() => { let data = require('./src/data.json'); console.log(data); data }))
.pipe(nunjucksRender({
path: ['src/'],
manageEnv: function(env){
var data = JSON.parse(fs.readFileSync('src/data.json'));
env.addGlobal('data', data);
}
}))
// output files in app folder
.pipe(gulp.dest('dist/'))
}
gulp.task('nunjucks', renderTemplates);
exports.nunjucks = renderTemplates;
function processScripts() {
return gulp.src(['src/**/*.js'])
.pipe(browserify())
.pipe(concat('app.js'))
.pipe(gulp.dest('dist/js/'))
.pipe(refresh(lrserver));
}
gulp.task('scripts', processScripts);
exports.scripts = processScripts;
gulp.task('styles', function() {
return gulp.src(['src/scss/*.scss'])
.pipe(sass())
.on('error', console.log)
.pipe(minifyCSS())
.pipe(gulp.dest('dist/css'))
.pipe(browserSync.stream())
});
gulp.task('serve', function() {
//Set up your static fileserver, which serves files in the build dir
http.createServer(ecstatic({ root: __dirname + '/dist' })).listen(serverport);
//Set up your livereload server
lrserver.listen(livereloadport);
});
function renderHTML() {
return gulp.src("src/*.html")
.pipe(embedlr())
.pipe(gulp.dest('dist/'))
// .pipe(refresh(lrserver));
}
gulp.task('html', renderHTML);
gulp.task('assets', function() {
return gulp.src("src/assets/**")
.pipe(imagemin({optimizationLevel: 5}))
.pipe(gulp.dest('dist/assets/'))
// .pipe(refresh(lrserver));
});
//compile scss into css
function style() {
return gulp.src('src/scss/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('dist/css'))
.pipe(browserSync.stream({match: '**/*.css'}));
}
transpileSass = run('npm run build:scss');
exports.sass = transpileSass;
gulp.task('build:sass', transpileSass);
function watchFiles() {
bsInit()
gulp.watch('src/data.json', renderTemplates).on('change', browserSync.reload);
gulp.watch('src/**/*.(njk|tpl|nunjucks)', renderTemplates).on('change', browserSync.reload);
gulp.watch('src/scss/**/*.scss', transpileSass).on('change', browserSync.reload);
// gulp.watch('src/*.html', renderHTML).on('change', browserSync.reload);
gulp.watch('dist/css/*.css').on('change', browserSync.reload);
gulp.watch('dist/**.html').on('change', browserSync.reload);
gulp.watch('src/**/*.js', processScripts).on('change', browserSync.reload);
}
exports.style = style;
exports.watch = watchFiles;
gulp.task('default', gulp.series(['scripts', 'build:sass', 'assets', renderTemplates, watchFiles]));