-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
65 lines (58 loc) · 1.78 KB
/
index.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
const swc = require('@swc/core');
const path = require('path');
const PluginError = require('plugin-error');
const { Transform } = require('stream');
const { replaceExtension, applySourceMap } = require('./helpers');
module.exports = function (opts) {
opts = opts || {};
return new Transform({
objectMode: true,
transform(file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new PluginError('gulp-swc', 'Streaming not supported'));
return;
}
const fileOpts = Object.assign({}, opts, {
filename: file.path,
sourceMaps: Boolean(file.sourceMap),
caller: Object.assign({ name: 'gulp-swc' }, opts.caller),
});
swc
.transform(file.contents.toString(), fileOpts)
.then((res) => {
if (res) {
if (file.sourceMap && res.map) {
const sourcemaps = JSON.parse(res.map);
sourcemaps.file = replaceExtension(file.relative);
sourcemaps.sources = sourcemaps.sources.map((filePath) => {
return file.path === filePath
? replaceExtension(file.relative)
: replaceExtension(path.relative(file.path, filePath));
});
applySourceMap(file, sourcemaps);
}
file.contents = Buffer.from(res.code);
file.path = replaceExtension(file.path);
}
this.push(file);
})
.catch((error) => {
this.emit(
'error',
new PluginError('gulp-swc', error, {
fileName: file.path,
showProperties: false,
})
);
})
.then(
() => cb(),
() => cb()
);
},
});
};