-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
56 lines (43 loc) · 1.45 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
'use strict';
const Plugin = require('broccoli-persistent-filter');
const swc = require('@swc/core')
class SWC extends Plugin {
constructor(inputTree, options = {}) {
super(inputTree, {
// swc uses native parallelism to achieve improved throughput
async: true,
// TODO: lets experiment with this some, maybe SWC is fast enough to not need this? persist: true
});
this.options = options;
this.swcOptions = {
...this.options.swc
};
if (this.swcOptions.module === undefined) {
this.swcOptions.module = { type: 'amd', moduleId: true };
}
this.extensions = ['js', 'ts'];
}
async processString(content, relativePath) {
const options = {...this.swcOptions};
options.module = {...options.module} || {};
if (options.module.type === 'amd' && this.options.namedAmd || options.module.moduleId === true) {
options.module.moduleId = relativePath.replace(/\.(?:js|ts)$/, '');
}
options.jsc = options.jsc || {};
if (relativePath.endsWith('.ts') && options.jsc.parser === undefined) {
options.jsc.parser = {
"syntax": "typescript",
}
}
const { code } = await swc.transform(content, options);
return code;
}
// this is implemented for persistent cache key creation by broccoli-persistent-filter
baseDir() {
return __dirname;
}
}
module.exports = function swc(input, options) {
return new SWC(input, options);
};
module.exports.Plugin = SWC;