-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
gulpfile.js
173 lines (154 loc) · 5.13 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
const gulp = require('gulp');
const less = require('gulp-less');
const rename = require('gulp-rename');
const livereload = require('gulp-livereload');
const sourcemaps = require('gulp-sourcemaps');
const autoprefixer = require('gulp-autoprefixer');
const minifyCss = require('gulp-clean-css');
const plumber = require('gulp-plumber');
const terser = require('gulp-terser');
const del = require('del');
const rollupStream = require('@rollup/stream');
const rollupRactive = require('rollup-plugin-ractive');
const rollupCommonjs = require('rollup-plugin-commonjs');
const rollupBabel = require('rollup-plugin-babel');
const rollupJson = require('rollup-plugin-json');
const buffer = require('vinyl-buffer');
const source = require('vinyl-source-stream');
const site = process.env.SITE === 'globalping' ? 'globalping' : 'jsdelivr';
const liveReloadOptions = { port: site === 'globalping' ? 35730 : 35729 };
const srcDir = './src';
const srcAssetsDir = `${srcDir}/assets`;
const srcPublicDir = `${srcDir}/public${site === 'globalping' ? '/globalping' : ''}`;
const dstAssetsDir = `./dist${site === 'globalping' ? '/globalping' : '/jsdelivr'}/assets`;
const dstPublicDir = `./dist${site === 'globalping' ? '/globalping' : '/jsdelivr'}`;
let cache;
const getRollupStream = file => rollupStream({
cache,
input: `${srcAssetsDir}/js/${file}`,
external: [
'algoliasearch',
'ractive',
],
plugins: [
{
resolveId (source) {
if (/^core-js(?:\/|$)/.test(source)) {
return require.resolve(source);
}
},
},
rollupRactive({ format: 'cjs', parseOptions: { interpolate: { script: true, style: true }, includeLinePositions: false, stripComments: false } }),
rollupCommonjs({ extensions: [ '.html', '.js' ], ignore: [ ] }),
rollupJson(),
rollupBabel({ extensions: [ '.html', '.js' ], presets: [] }),
],
output: {
name: 'app',
format: 'umd',
sourcemap: true,
globals: {
algoliasearch: 'algoliasearch',
ractive: 'Ractive',
},
},
}).on('bundle', (bundle) => {
cache = bundle;
});
gulp.task('clean', () => {
return del([ dstPublicDir ]);
});
gulp.task('copy', gulp.parallel(
() => gulp.src(`${srcAssetsDir}/**/*.!(js|less)`, { base: srcAssetsDir, since: gulp.lastRun('copy') })
.pipe(gulp.dest(dstAssetsDir))
.pipe(livereload(liveReloadOptions)),
() => gulp.src(`${srcPublicDir}/**/*`, { base: srcPublicDir, since: gulp.lastRun('copy') })
.pipe(gulp.dest(dstPublicDir))
.pipe(livereload(liveReloadOptions)),
));
gulp.task('less', () => {
return gulp.src(site === 'globalping'
? [
`${srcAssetsDir}/less/app-globalping.less`,
]
: [
`${srcAssetsDir}/less/app.less`,
])
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(less({ relativeUrls: true, strictMath: true }))
.pipe(rename('app.css'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${dstAssetsDir}/css`))
.pipe(livereload(liveReloadOptions));
});
gulp.task('less:prod', () => {
return gulp.src(site === 'globalping'
? [
`${srcAssetsDir}/less/app-globalping.less`,
]
: [
`${srcAssetsDir}/less/app.less`,
])
.pipe(sourcemaps.init())
.pipe(less({ relativeUrls: true, strictMath: true }))
.pipe(autoprefixer())
.pipe(minifyCss())
.pipe(rename('app.css'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${dstAssetsDir}/css`));
});
gulp.task('js', gulp.parallel(
() => getRollupStream(site === 'globalping' ? 'app-globalping.js' : 'app.js')
.pipe(plumber())
.pipe(source(site === 'globalping' ? 'app-globalping.js' : 'app.js', srcAssetsDir))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${dstAssetsDir}/js`))
.pipe(livereload(liveReloadOptions)),
() => getRollupStream('app-docs.js')
.pipe(plumber())
.pipe(source(`app-docs.js`, srcAssetsDir))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${dstAssetsDir}/js`))
.pipe(livereload(liveReloadOptions)),
));
gulp.task('js:prod', gulp.parallel(
() => getRollupStream(site === 'globalping' ? 'app-globalping.js' : 'app.js')
.pipe(source(site === 'globalping' ? 'app-globalping.js' : 'app.js', srcAssetsDir))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(terser({ sourceMap: { includeSources: true } }))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${dstAssetsDir}/js`)),
() => getRollupStream('app-docs.js')
.pipe(source(`app-docs.js`, srcAssetsDir))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(terser({ sourceMap: { includeSources: true } }))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${dstAssetsDir}/js`)),
));
gulp.task('build', gulp.series('clean', 'copy', 'less:prod', 'js:prod'));
gulp.task('dev', gulp.series('copy', 'less', 'js'));
gulp.task('serve', () => {
require('./src');
});
gulp.task('watch', () => {
livereload.listen(liveReloadOptions);
gulp.watch([
`${srcAssetsDir}/**/*.!(html|js|less)`,
`${srcPublicDir}/**/*`,
], gulp.series('copy'));
gulp.watch([
`${srcDir}/**/*.html`,
`${srcAssetsDir}/**/*.(js|json)`,
], gulp.series('js'));
gulp.watch([
`${srcAssetsDir}/**/*.less`,
], gulp.series('less'));
});
gulp.task('default', gulp.series('clean', 'dev', gulp.parallel('serve', 'watch')));