This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
/
gulpfile.js
166 lines (151 loc) · 3.85 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
const gulp = require('gulp');
const util = require('util');
const pump = util.promisify(require('pump'));
const { sync: del } = require('del');
const replace = require('gulp-replace');
const commitHash = require('./scripts/commit-hash');
const htmlmin = require('gulp-htmlmin');
const inlinesource = require('gulp-inline-source');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const flexbugs = require('postcss-flexbugs-fixes');
const cssnano = require('cssnano');
const uglify = require('gulp-uglify');
const concat = require('gulp-concat');
const { prepend } = require('gulp-insert');
const rename = require('gulp-rename');
const sourcemaps = require('gulp-sourcemaps');
const imagemin = require('gulp-imagemin');
const { createInterface } = require('readline');
const { createReadStream } = require('fs');
const runSequence = require('run-sequence');
const paths = {
html: 'src/**/*.html',
css: 'src/**/css/*.css',
widget: 'src/widget.js',
scripts: 'src/**/js/*.js',
images: 'src/**/images/*.{gif,jpg,jpeg,png,svg}'
};
let licenseComment;
gulp.task('clean', () => {
return del(['dist']);
});
gulp.task('license', () => {
licenseComment = `/**\n * @license\n`;
return new Promise((resolve, reject) => {
createInterface({ input: createReadStream('LICENSE') })
.on('line', line => {
licenseComment += ` * ${line}\n`;
})
.on('close', () => {
licenseComment += ` */\n`;
resolve(licenseComment);
});
});
});
gulp.task('html', async () => {
const rev = await commitHash()
return pump([
gulp.src(paths.html),
// Set release from base branch instead of generated output
replace(/\{\{\s*site.github.build_revision\s*\}\}/, rev),
replace('CACHE_BUST', rev),
htmlmin({
collapseBooleanAttributes: true,
collapseWhitespace: false,
decodeEntities: true,
minifyCSS: true,
minifyJS: true,
minifyURLs: true,
removeAttributeQuotes: true,
removeComments: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortAttributes: true,
sortClassName: true,
useShortDoctype: true
}),
gulp.dest('dist')
]);
});
gulp.task('css', () => {
return pump([
gulp.src(paths.css),
sourcemaps.init(),
postcss([
autoprefixer,
flexbugs,
cssnano
]),
rename({ extname: '.min.css' }),
sourcemaps.write('.'),
gulp.dest('dist')
]);
});
gulp.task('widget', ['license'], () => {
return pump([
gulp.src(paths.widget),
sourcemaps.init(),
uglify(),
prepend(licenseComment),
sourcemaps.write('.'),
gulp.dest('dist')
]);
});
gulp.task('scripts', ['license'], () => {
return pump([
gulp.src(paths.scripts),
sourcemaps.init(),
concat('main.js'),
uglify(),
prepend(licenseComment),
rename({ extname: '.min.js' }),
sourcemaps.write('.'),
gulp.dest('dist/iframe/js')
]);
});
gulp.task('images', () => {
return pump([
gulp.src(paths.images),
imagemin(),
gulp.dest('dist')
]);
});
gulp.task('copy', () => {
return pump([
gulp.src([
'CNAME'
]),
gulp.dest('dist')
]);
});
gulp.task('inline', () => {
var opts = {
compress: false
};
return gulp.src('./dist/iframe/iframe.html')
.pipe(inlinesource(opts))
.pipe(gulp.dest('./dist/iframe/'));
});
gulp.task('watch', ['default'], () => {
gulp.watch(paths.demos, ['demos']);
gulp.watch(paths.html, ['html']);
gulp.watch(paths.css, ['css']);
gulp.watch(paths.scripts, ['scripts']);
gulp.watch(paths.images, ['images']);
gulp.watch(paths.widget, ['widget']);
});
gulp.task('default', function(callback) {
runSequence(
'clean',
'html',
'css',
'widget',
'scripts',
'images',
'copy',
'inline',
callback)
})