-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
104 lines (92 loc) · 2.27 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
/**
* @Author: Caven Chen
* @Date: 2024-04-26
*/
'use strict'
import fse from 'fs-extra'
import path from 'path'
import gulp from 'gulp'
import esbuild from 'esbuild'
import startServer from './server.js'
import GlobalsPlugin from 'esbuild-plugin-globals'
import shell from 'shelljs'
import chalk from 'chalk'
const buildConfig = {
entryPoints: ['src/index.js'],
bundle: true,
color: true,
legalComments: `inline`,
logLimit: 0,
target: `es2019`,
minify: false,
sourcemap: false,
write: true,
logLevel: 'info',
plugins: [],
external: ['three'],
}
async function buildModules(options) {
// Build IIFE
if (options.iife) {
await esbuild.build({
...buildConfig,
format: 'iife',
globalName: '',
minify: options.minify,
plugins: [
GlobalsPlugin({
three: 'THREE',
}),
],
outfile: path.join('dist', 'mtp.min.js'),
})
}
// Build Node
if (options.node) {
await esbuild.build({
...buildConfig,
format: 'esm',
platform: 'node',
minify: options.minify,
outfile: path.join('dist', 'index.js'),
})
}
}
async function regenerate(option, content) {
await fse.remove('dist/index.js')
await buildModules(option)
}
export const server = gulp.series(startServer)
export const dev = gulp.series(() => {
shell.echo(chalk.yellow('============= start dev =============='))
const watcher = gulp.watch('src', {
persistent: true,
awaitWriteFinish: {
stabilityThreshold: 1000,
pollInterval: 100,
},
})
watcher
.on('ready', async () => {
await regenerate({ node: true })
await startServer()
})
.on('change', async () => {
let now = new Date().getTime()
await regenerate({ node: true })
shell.echo(
chalk.green(`regenerate lib takes ${new Date().getTime() - now} ms`)
)
})
return watcher
})
export const buildIIFE = gulp.series(() => buildModules({ iife: true }))
export const buildNode = gulp.series(() => buildModules({ node: true }))
export const build = gulp.series(
() => buildModules({ iife: true }),
() => buildModules({ node: true })
)
export const buildRelease = gulp.series(
() => buildModules({ iife: true, minify: true }),
() => buildModules({ node: true, minify: true })
)