-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
78 lines (66 loc) · 1.9 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
import fs from 'fs'
import { exec } from 'child_process'
import path from 'path'
import gulp from 'gulp'
import through from 'through2'
import source from 'vinyl-source-stream'
import terser from 'gulp-terser'
import rollupStream from '@rollup/stream'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import typescript from '@rollup/plugin-typescript'
import minifyHtml from 'rollup-plugin-minify-html-template-literals'
import cleanup from 'rollup-plugin-cleanup'
const { src, dest, series, parallel, watch } = gulp
const header = filePath =>
through.obj((file, _, callback) => {
const headerContent = String(
fs.readFileSync(path.join(process.cwd(), filePath))
)
file.contents = Buffer.from(headerContent + String(file.contents))
callback(null, file)
})
const clean = () => exec('rm -rf dist')
const build = () => {
const options = {
input: 'src/index.ts',
output: { format: 'iife' },
plugins: [
nodeResolve({ browser: true }),
commonjs(),
typescript(),
minifyHtml(),
cleanup()
]
}
return rollupStream(options)
.pipe(source('app.js'))
.pipe(dest('dist/extension'))
}
const addHeader = () =>
src('dist/extension/app.js')
.pipe(header('src/extension/meta.js'))
.pipe(dest('dist/userscript'))
const minifyJS = () =>
src('dist/extension/app.js', { base: '.' }).pipe(terser()).pipe(dest('.'))
const mix = () =>
src([
'src/extension/content.js',
'src/extension/manifest.json',
'src/img/*'
]).pipe(dest('dist/extension'))
const pack = () => {
const name = 'extension'
return exec(
`cd dist/${name} && zip -r ${name}.zip . && mv ${name}.zip ../${name}.zip`
)
}
const server = () => {
watch('src/**/*', { ignoreInitial: false }, build)
}
export default series(
clean,
parallel(series(build, parallel(addHeader, minifyJS)), mix),
pack
)
export { server }