-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
192 lines (172 loc) · 4.41 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
const autoprefixer = require('autoprefixer');
// eslint-disable-next-line security/detect-child-process
const spawn = require('child_process').spawn;
const fs = require('fs');
const gulp = require('gulp');
const babel = require('gulp-babel');
const concat = require('gulp-concat');
const sass = require('gulp-dart-sass');
const inject = require('gulp-inject-string');
const postcss = require('gulp-postcss');
const posthtml = require('gulp-posthtml');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const zip = require('gulp-zip');
const merge = require('merge2');
const path = require('path');
const exp = require('posthtml-expressions');
const include = require('posthtml-include');
gulp.task('clean', async () => {
const dir = 'dist';
fs.statSync(dir, { throwIfNoEntry: false }, (err, stats) => {
if (err) {
if (err.code && err.code !== 'ENOENT') {
throw err;
}
} else if (stats.isDirectory()) {
fs.rmSync(dir, { force: true, recursive: true }, (err) => {
if (err) {
throw err;
}
});
}
});
});
gulp.task('styles', function() {
return gulp.src('./src/**/*.scss')
.pipe(sass({ outputStyle: 'compressed' }).on('error', sass.logError))
.pipe(postcss([
autoprefixer(),
]))
.pipe(concat('style.css'))
.pipe(sourcemaps.init())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./dist'));
});
gulp.task('scripts', () => {
return merge(
gulp.src('src/scripts/lib/**/*.js'),
gulp.src([
'src/scripts/**/*.js',
'!src/scripts/lib',
]),
)
.pipe(sourcemaps.init())
.pipe(babel({
presets: ['@babel/env'],
}))
.pipe(concat('script.js'))
.pipe(uglify())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist'));
});
gulp.task('templates:posthtml', () => {
return gulp
.src([
'src/templates/*.hbs',
'!src/templates/partials',
])
.pipe(
posthtml(
[
exp({
delimiters: ['{{%', '%}}'],
locals: {},
unescapedDelimiters: ['{{{%', '%}}}'],
}),
include({
root: 'src/templates/partials',
}),
],
{
template: false,
},
),
)
.pipe(gulp.dest('./dist/templates'));
});
gulp.task('templates:transform-header', () => {
return gulp.src('dist/templates/header.hbs')
.pipe(
inject.replace(
'<!-- GULP REPLACE - MAIN WRAPPER OPENING TAG -->',
'<!-- GENERATED VIA GULP -->\n<div class="main-wrapper">',
),
)
.pipe(gulp.dest('dist/templates/'));
});
gulp.task('templates:transform-footer', () => {
return gulp.src('dist/templates/footer.hbs')
.pipe(
inject.replace(
'<!-- GULP REPLACE - MAIN WRAPPER CLOSING TAG -->',
'<!-- GENERATED VIA GULP - Closes .main-wrapper from header template -->\n</div>',
),
)
.pipe(gulp.dest('dist/templates/'));
});
gulp.task('templates', gulp.series(
'templates:posthtml',
gulp.parallel('templates:transform-header', 'templates:transform-footer'),
));
gulp.task('copy-files', () => {
return gulp.src([
'src/{assets,settings,translations}/**/*',
'src/manifest.json',
])
.pipe(gulp.dest('dist'));
});
gulp.task('update-build', gulp.parallel(
'styles',
'scripts',
'templates',
'copy-files'),
);
gulp.task('build', gulp.series('clean', 'update-build'));
let previewTask;
gulp.task('preview:start', (cb) => {
previewTask = spawn('zcli', [
'themes:preview',
], {
cwd: path.resolve('./dist'),
});
previewTask.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
previewTask.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
previewTask.on('close', code => code ? cb(new Error(code)) : cb());
});
gulp.task('preview:stop', (cb) => {
previewTask.stdin.pause();
previewTask.kill();
previewTask = undefined;
cb();
});
gulp.task('watch', () => {
// eslint-disable-next-line security/detect-non-literal-fs-filename
gulp.watch(
[
'src/**/*',
],
gulp.series('preview:stop', 'update-build', gulp.parallel('preview:start', 'watch')),
);
});
gulp.task('zip', () => {
return gulp
.src('dist/**')
.pipe(zip('theme.zip'))
.pipe(gulp.dest('.'));
});
gulp.task('package', gulp.series('build', 'zip'));
gulp.task(
'develop',
gulp.series(
'build',
gulp.parallel(
'preview:start',
'watch',
),
),
);