-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.ts
102 lines (94 loc) · 2.64 KB
/
gulpfile.ts
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
import * as esbuild from 'esbuild';
import autoprefixer from 'gulp-autoprefixer';
import {exec} from 'child_process';
import glob from 'glob';
import gulp from 'gulp';
import sass from 'gulp-dart-sass';
const ENTRIES = {
js: {
// Files first processed by `tsc`, and later output by `esbuild`.
entrypoints: () => {
return [
'./dist/tsc/main.js',
...glob.sync('./dist/tsc/components/**/*.js'),
...glob.sync('./dist/tsc/partials/**/*.js'),
];
},
// Where to output chunks generated by `esbuild`.
outDir: './dist/chunks/',
watch: ['./src/**/*.ts', './src/**/*.tsx'],
},
sass: {
includePaths: ['./node_modules/', './src/sass/', './src/'],
src: ['./src/partials/**/*.sass', './src/sass/*.sass'],
outDir: './dist/css/',
watch: ['./src/**/*.sass'],
},
};
/**
* esBuild does not do type checks and can build with type errors so we first run
* `tsc` and generate a JS file. esBuild is then run on the outputted JS file.
*
* The entry point of tsc compilation is configured in tsconfig `include`.
*/
const runEsBuild = async prod => {
return new Promise<void>((resolve, reject) => {
exec('tsc', async (error, stderr) => {
if (stderr) {
console.error('TypeScript errors');
console.error(stderr);
reject();
} else {
await esbuild.build({
bundle: true,
chunkNames: 'chunks/[name]-[hash]-min',
define: {
...(prod && {'process.env.NODE_ENV': "'production'"}),
},
entryPoints: ENTRIES.js.entrypoints(),
format: 'esm',
jsxFactory: 'preact.h',
jsxFragment: 'preact.Fragment',
minify: prod,
outdir: ENTRIES.js.outDir,
platform: 'browser',
splitting: true,
});
resolve();
}
});
});
};
gulp.task('build:js', async () => {
await runEsBuild(true);
});
gulp.task('build:sass', () => {
return gulp
.src(ENTRIES.sass.src)
.pipe(
sass({
outputStyle: 'compressed',
includePaths: ENTRIES.sass.includePaths,
})
)
.on('error', sass.logError)
.pipe(autoprefixer())
.pipe(gulp.dest(ENTRIES.sass.outDir));
});
gulp.task('watch:sass', () => {
return gulp.watch(
ENTRIES.sass.watch,
{ignoreInitial: false},
gulp.series('build:sass')
);
});
gulp.task('watch:js', async cb => {
await runEsBuild(false);
gulp.watch(ENTRIES.js.watch, async () => {
await runEsBuild(false);
cb();
});
});
gulp.task('watch', gulp.parallel('watch:js', 'watch:sass'));
gulp.task('build', gulp.parallel('build:sass', 'build:js'));
gulp.task('default', gulp.series('watch'));