-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
62 lines (57 loc) · 1.97 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
import gulp from 'gulp'
import { Transform } from 'stream'
import { transform } from '@swc/core'
const { src, lastRun, dest, parallel, watch } = gulp
export default function () {
return watch('./extension-src/**/*', { ignoreInitial: false }, parallel(compile, copy))
}
function compile() {
return src(['./extension-src/**/*.js'], {
since: lastRun(compile),
})
.pipe(new PluginTransform())
.pipe(dest('./extension/'))
}
function copy() {
return src(['./extension-src/**/*', '!./extension-src/**/*.js'], {
since: lastRun(copy),
}).pipe(dest('./extension/'))
}
class PluginTransform extends Transform {
constructor() {
super({ objectMode: true, defaultEncoding: 'utf-8' })
}
_transform(file, encoding, callback) {
const id = file.path.replace(/\\/g, '/').split('extension-src')[1].split('/')[1]
const registerName = `__HostModuleSourceRegister__`
const virtualPath =
'holoflows-extension://' + id + '/' + file.path.replace(/\\/g, '/').split('extension-src/' + id + '/')[1]
transform(file.contents.utf8Slice(), {
isModule: true,
jsc: {
target: 'es2021',
experimental: {
plugins: [
[
'@masknet/static-module-record-swc',
{
template: {
type: 'callback',
callback: registerName,
firstArg: virtualPath,
},
},
],
],
},
},
}).then(
(output) => {
file.contents = Buffer.from(output.code, 'utf-8')
file.path = file.path
callback(null, file)
},
(err) => callback(err),
)
}
}