This repository was archived by the owner on Oct 10, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
276 lines (237 loc) · 7.62 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
const gulp = require('gulp');
const sync = require('browser-sync');
const sass = require('gulp-sass');
const concat = require('gulp-concat');
const beautify = require('gulp-cssbeautify');
const minify = require('gulp-clean-css');
const uglify = require('gulp-uglify');
const gulpif = require('gulp-if');
const pug = require('gulp-pug');
const replace = require('gulp-replace');
const header = require('gulp-header');
const babel = require('gulp-babel');
const sourcemaps = require('gulp-sourcemaps');
const del = require('del');
const fs = require('fs');
const util = require('minimist')(process.argv);
// @use npm run dist -- --[development|staging|production]
let envFlag = 'development';
if (util.staging !== undefined) {
envFlag = 'staging';
} else if (util.production !== undefined) {
envFlag = 'production';
}
const config = {
fileName: 'seed',
path: {
source: 'source/',
build: 'build/',
dist: 'dist/'
}
};
/**
* Reset the build folder for development.
*/
gulp.task('clean', () => del([config.path.build]));
/**
* Reset the distribution pack.
*/
gulp.task('clean:dist', () => del([config.path.build, config.path.dist]));
/**
* Copies the images to the dist folder.
*/
gulp.task('build:website-images', () =>
gulp.src(config.path.source + 'images/**/*').pipe(gulp.dest(config.path.build + 'assets/images'))
);
/**
* Builds the sample page styling
*/
gulp.task('build:website-style', () =>
gulp
.src(config.path.source + 'sass/sample.scss')
.pipe(sass().on('error', sass.logError))
.pipe(beautify({ indent: ' ', autosemicolon: true }))
.pipe(concat('style.css'))
.pipe(gulpif(envFlag !== 'development', minify()))
.pipe(gulp.dest(config.path.build + 'assets/css'))
.pipe(sync.reload({ stream: true }))
);
/**
* Builds the product styling
*/
gulp.task('build:sass', (cb) => {
gulp
.src([`${config.path.source}sass/includes/_normalize.scss`, `${config.path.source}sass/main.scss`])
.pipe(sass().on('error', sass.logError))
.pipe(beautify({ indent: ' ', autosemicolon: true }))
.pipe(gulpif(envFlag !== 'development', minify()))
.pipe(concat(`${config.fileName}.css`))
.pipe(gulp.dest(`${config.path.build}assets/css`))
.pipe(sync.reload({ stream: true }));
cb();
});
/**
* Builds the HTML pages
*/
gulp.task('build:website', (cb) => {
const pkg = require('./package.json');
gulp
.src([config.path.source + 'pug/*.pug', config.path.source + 'pug/tests/*.pug'], {
base: config.path.source + 'pug/'
})
.pipe(
pug({ pretty: true }).on('error', function(err) {
console.log('#### PUG ERROR ####', err, '#### #### ####');
this.emit('end');
})
)
.pipe(replace(/\{\%version\%\}/g, 'v' + pkg.version))
.pipe(replace(/\{\%year\%\}/g, new Date().getFullYear()))
.pipe(gulp.dest(config.path.build + '/'));
cb();
});
/**
* Makes easier to watch pug files
*/
gulp.task(
'build:pug-watch',
gulp.series('build:website', (cb) => {
sync.reload();
cb();
})
);
/**
* Transpiles the website javascript.
*/
gulp.task('build:website-js', (cb) => {
const babelOptions = { presets: ['@babel/env'] };
const onUglifyErr = (err) => {
console.error('#### ERROR IN build:website-js TASK ####', err.toString());
};
gulp
.src([config.path.source + 'js/components/*.js', config.path.source + 'js/*.js'])
.pipe(gulpif(envFlag !== 'development', sourcemaps.init()))
.pipe(babel(babelOptions))
.pipe(gulpif(envFlag !== 'development', uglify().on('error', onUglifyErr)))
.pipe(concat('bundle.js'))
.pipe(gulpif(envFlag !== 'development', sourcemaps.write()))
.pipe(gulp.dest(config.path.build + 'assets/js'))
.pipe(sync.reload({ stream: true }));
cb();
});
/**
* Transpiles the javascript source-code.
*/
gulp.task('build:javascript', (cb) => {
const babelOptions = { presets: ['@babel/env'] };
const onUglifyErr = (err) => {
console.error('#### ERROR IN build:javascript TASK ####', err.toString());
};
gulp
.src([config.path.source + 'js/components/*.js'])
.pipe(gulpif(envFlag !== 'development', sourcemaps.init()))
.pipe(babel(babelOptions))
.pipe(gulpif(envFlag !== 'development', uglify().on('error', onUglifyErr)))
.pipe(concat('seedcss.js'))
.pipe(gulpif(envFlag !== 'development', sourcemaps.write()))
.pipe(gulp.dest(config.path.build + 'assets/js'));
cb();
});
/**
* Watches source-code file changes.
* For development.
*/
gulp.task(
'watch',
gulp.series(
gulp.parallel('build:website-style', 'build:website-js', 'build:website'),
'build:website-images',
gulp.parallel('build:sass', 'build:javascript'),
(cb) => {
sync.init({
open: true,
notify: false,
port: 9000,
server: {
baseDir: './build'
}
});
// Watch changes on website styling
gulp.watch(config.path.source + 'sass/sample.scss', gulp.series('build:website-style'));
// Watch changes on website JS
gulp.watch(config.path.source + 'js/**/*.js', gulp.series('build:javascript', 'build:website-js'));
// Watch changes on Seed styling
gulp.watch(config.path.source + 'sass/**/*.scss', gulp.series('build:sass'));
// Watch changes on the website page
gulp.watch(config.path.source + 'pug/**/*.pug', gulp.series('build:pug-watch'));
// Watch changes on any new image (assets)
gulp.watch(config.path.source + 'images/**/*', gulp.series('build:website-images'));
// Reloads the browser is any image is placed on assets
gulp.watch(config.path.build + 'assets/images/**/*').on('change', sync.reload);
cb();
}
)
);
gulp.task('dist:run', (cb) => {
// Get updated package.json
const pkg = require('./package.json');
// prepare files header
const comment =
'/*! \n ' +
'* Seed CSS (<%= pkg.name %>) \n ' +
'* <%= pkg.description %> \n ' +
'* @author <%= pkg.author %> \n ' +
'* @copyright 2016-' +
new Date().getFullYear() +
', <%= pkg.author %> \n ' +
'* @license <%= pkg.license %> \n ' +
'* @version <%= pkg.version %> \n ' +
'*/ \n\n ';
// Copy the SASS source-code to the distribution folder
// This will help developers to customize it or only load what they need.
gulp
.src([
`${config.path.source}sass/components/*.scss`,
`${config.path.source}sass/includes/*.scss`,
`${config.path.source}sass/+(main|essential).scss`
])
.pipe(gulp.dest(`${config.path.dist}src/sass`));
// Copy the Javascript source-code to the distribution folder
// This will help developers to customize it or only load what they need.
gulp.src(`${config.path.source}js/components/*.js`).pipe(gulp.dest(`${config.path.dist}src/js`));
// Buy some time to the script complete writing the files in the disk
// before write the distribution header in the files. Think 1.5 seconds to be enough.
setTimeout(() => {
gulp
.src(`${config.path.build}assets/css/seed.css`)
.pipe(header(comment, { pkg: pkg }))
.pipe(gulp.dest(config.path.dist));
gulp
.src(`${config.path.build}assets/js/seedcss.js`)
.pipe(header(comment, { pkg: pkg }))
.pipe(gulp.dest(config.path.dist));
cb();
}, 1500);
});
/**
* Builds the docs page.
*/
gulp.task(
'pages',
gulp.series(
'clean:dist',
gulp.parallel('build:sass', 'build:javascript', 'build:website', 'build:website-style', 'build:website-js')
)
);
/**
* Creates the distribution pack.
*/
gulp.task(
'build',
gulp.series(
'clean:dist',
gulp.parallel('build:sass', 'build:javascript', 'build:website', 'build:website-style', 'build:website-js'),
'dist:run'
)
);
gulp.task('default', gulp.series('clean', 'watch'));