generated from cavencj/lib-cli
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
126 lines (113 loc) · 2.75 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
/**
* @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 inlineImage from 'esbuild-plugin-inline-image'
import { sassPlugin } from 'esbuild-sass-plugin'
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: [
inlineImage({
limit: -1,
}),
sassPlugin(),
],
}
async function buildCSS(options) {
await esbuild.build({
...buildConfig,
minify: !!options.minify,
entryPoints: ['src/themes/index.scss'],
outfile: path.join('dist', 'dm.min.css'),
})
}
async function buildModules(options) {
// Build IIFE
if (options.iife) {
await esbuild.build({
...buildConfig,
format: 'iife',
globalName: 'DM',
minify: !!options.minify,
outfile: path.join('dist', 'dm.min.js'),
})
}
// Build Node
if (options.node) {
await esbuild.build({
...buildConfig,
format: 'esm',
platform: 'node',
minify: options.minify,
define: {
TransformStream: 'null',
},
outfile: path.join('dist', 'index.js'),
})
}
}
async function regenerate(option, content) {
await fse.remove('dist/dm.min.js')
await fse.remove('dist/dm.min.css')
await buildModules(option)
await buildCSS(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({ iife: true })
await startServer()
})
.on('change', async () => {
let now = new Date().getTime()
await regenerate({ iife: true })
shell.echo(
chalk.green(`regenerate lib takes ${new Date().getTime() - now} ms`)
)
})
return watcher
})
export const buildIIFE = gulp.series(
() => buildModules({ iife: true }),
() => buildCSS({ minify: true })
)
export const buildNode = gulp.series(
() => buildModules({ node: true }),
() => buildCSS({ minify: true })
)
export const build = gulp.series(
() => buildModules({ iife: true }),
() => buildModules({ node: true }),
() => buildCSS({ minify: true })
)
export const buildRelease = gulp.series(
() => buildModules({ iife: true, minify: true }),
() => buildModules({ node: true, minify: true }),
() => buildCSS({ minify: true })
)