-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
167 lines (140 loc) · 4.42 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
/**
* Upstream: https://github.com/pattisahusiwa/tpl-skeleton
* Author: Asis Pattisahusiwa
*/
// pnpm i -D gulp browser-sync del gulp-if gulp-sass sass gulp-sourcemaps \\
// gulp-postcss autoprefixer postcss gulp-rename gulp-twig gulp-plumber gulp-data
const gulp = require('gulp');
let _path, _env, _server;
function generatePath(dest) {
return {
dist: {
root: dest,
assets: `${dest}/assets`
},
src: {
root: 'src',
scss: 'src/scss',
twig: 'src/twig',
twigdata: 'src/twigdata',
assets: 'src/assets'
}
}
}
function clean(done) {
const del = require('del');
return del([_path.dist.root], done);
}
function compileSCSS(done){
const sass = require('gulp-sass')(require('sass'));
const gulpif = require('gulp-if');
const rename = require('gulp-rename');
const sourcemaps = require('gulp-sourcemaps');
const postcss = require("gulp-postcss");
const autoprefixer = require("autoprefixer");
const options = {
precision: 8,
errLogToConsole: true,
includePaths: ['./node_modules'],
};
const task = gulp.src(`${_path.src.scss}/main.scss`, {allowEmpty: true})
.pipe(gulpif(_env === 'dev', sourcemaps.init()))
.pipe(sass(options)).on('error', function(err) {
sass.logError.bind(this)(err);
if (_env !== 'dev') {
process.exit(err.status);
}
})
.pipe(postcss([
autoprefixer()
]))
.pipe(rename(function (path) {
path.extname = ".min.css";
}))
.pipe(gulpif(_env === 'dev', sourcemaps.write('.')))
.pipe(gulp.dest(_path.dist.assets));
if (_server) {
task.pipe(_server.stream({ match: '**/*.css' }));
}
done();
}
function compileTwig(done){
const rename = require('gulp-rename');
const plumber = require('gulp-plumber');
const gdata = require('gulp-data');
const twig = require('gulp-twig');
gulp.src(`${_path.src.twig}/**/*.html.twig`)
// Stay live and reload on error
.pipe(plumber({
errorHandler: function (err) {
process.stderr.write('\n' + err.toString() + '\n\n');
if (_env !== 'dev') {
process.exit(1);
}
this.emit('end');
}
}))
.pipe(gdata((file) => {
const loadFile = (path) => {
if (fs.existsSync(path)) {
return JSON.parse(fs.readFileSync(path));
}
return null;
}
const fs = require('fs');
const nodePath = require('path');
const filename = nodePath.basename(file.path, '.html.twig') + '.json';
let data = loadFile(`${_path.src.twigdata}/${filename}`);
let global = loadFile(`${_path.src.twigdata}/global.json`);
return {...global, ...data};
}))
.pipe(twig({baseDir: _path.src.twig}))
.pipe(rename(function (path) {
path.dirname = "";
path.extname = "";
}))
.pipe(gulp.dest(_path.dist.root));
done();
}
function copyAssets(done){
gulp.src(`${_path.src.assets}/**/*`)
.pipe(gulp.dest(_path.dist.assets));
done();
}
function startServer(done){
_server = require('browser-sync').create();
_server.init({
server: {
baseDir: _path.dist.root
},
open: true,
notify: false,
https: false,
});
done();
}
function watch(done){
const reload = (done) => {
_server.reload();
done();
}
gulp.watch(`${_path.src.scss}/**/*.scss`, compileSCSS);
gulp.watch(`${_path.src.twig}/**/*.twig`, gulp.series(compileTwig, reload));
gulp.watch(`${_path.src.twig}/**/*.svg`, gulp.series(compileTwig, reload));
gulp.watch(`${_path.src.twigdata}/**/*.json`, gulp.series(compileTwig, reload));
gulp.watch(`${_path.src.assets}/**/*`, gulp.series(copyAssets, reload));
done();
}
const devBuild = (done) => {
_path = generatePath('dev');
_env = 'dev';
done();
}
const devProd = (done) => {
_path = generatePath('dist');
_env = 'prod';
done();
}
const build = gulp.parallel(compileSCSS, compileTwig, copyAssets);
exports.default = gulp.series(devBuild, clean, build, startServer, watch);
exports.build = gulp.series(devProd, clean, build);