-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
57 lines (50 loc) · 1.71 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
// Used https://github.com/egoist/tsup/blob/dev/src/esbuild/swc.ts
import path from "path";
import swc from "@swc/core";
/**
* Use SWC to emit decorator metadata
*/
export const esbuildDecorators = () => ({
name: "swc",
setup(build) {
// Force esbuild to keep class names as well
build.initialOptions.keepNames = true;
build.onLoad({ filter: /\.[jt]sx?$/ }, async (args) => {
const isTs = /\.tsx?$/.test(args.path);
const jsc = {
parser: {
syntax: isTs ? "typescript" : "ecmascript",
decorators: true,
},
transform: {
legacyDecorator: true,
decoratorMetadata: true,
},
keepClassNames: true,
target: "es2022",
};
const result = await swc.transformFile(args.path, {
jsc,
sourceMaps: true,
configFile: false,
swcrc: false,
});
let code = result.code;
if (result.map) {
const map = JSON.parse(result.map);
// Make sure sources are relative path
map.sources = map.sources.map((source) => {
return path.isAbsolute(source)
? path.relative(path.dirname(args.path), source)
: source;
});
code += `//# sourceMappingURL=data:application/json;base64,${Buffer.from(
JSON.stringify(map)
).toString("base64")}`;
}
return {
contents: code,
};
});
},
});