-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathregister.ts
117 lines (107 loc) · 3.53 KB
/
register.ts
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { platform } from 'os'
import { resolve } from 'path'
import { Options, transform, transformSync } from '@swc-node/core'
import { installSourceMapSupport, SourcemapMap } from '@swc-node/sourcemap-support'
import { addHook } from 'pirates'
import * as ts from 'typescript'
import { readDefaultTsConfig, tsCompilerOptionsToSwcConfig } from './read-default-tsconfig'
const DEFAULT_EXTENSIONS = ['.js', '.jsx', '.es6', '.es', '.mjs', '.ts', '.tsx']
const PLATFORM = platform()
const injectInlineSourceMap = ({
filename,
code,
map,
}: {
filename: string
code: string
map: string | undefined
}): string => {
if (map) {
SourcemapMap.set(filename, map)
const base64Map = Buffer.from(map, 'utf8').toString('base64')
const sourceMapContent = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}`
return `${code}\n${sourceMapContent}`
}
return code
}
export function compile(
sourcecode: string,
filename: string,
options: ts.CompilerOptions & { fallbackToTs?: (filename: string) => boolean },
): string
export function compile(
sourcecode: string,
filename: string,
options: ts.CompilerOptions & { fallbackToTs?: (filename: string) => boolean },
async: false,
): string
export function compile(
sourcecode: string,
filename: string,
options: ts.CompilerOptions & { fallbackToTs?: (filename: string) => boolean },
async: true,
): Promise<string>
export function compile(
sourcecode: string,
filename: string,
options: ts.CompilerOptions & { fallbackToTs?: (filename: string) => boolean },
async: boolean,
): string | Promise<string>
export function compile(
sourcecode: string,
filename: string,
options: ts.CompilerOptions & { fallbackToTs?: (filename: string) => boolean },
async = false,
) {
if (filename.endsWith('.d.ts')) {
return ''
}
if (options.files && (options.files as string[]).length) {
if (
PLATFORM === 'win32' &&
(options.files as string[]).every((file) => filename !== resolve(process.cwd(), file))
) {
return sourcecode
}
if (PLATFORM !== 'win32' && (options.files as string[]).every((file) => !filename.endsWith(file))) {
return sourcecode
}
}
if (options && typeof options.fallbackToTs === 'function' && options.fallbackToTs(filename)) {
delete options.fallbackToTs
const { outputText, sourceMapText } = ts.transpileModule(sourcecode, {
fileName: filename,
compilerOptions: options,
})
return injectInlineSourceMap({ filename, code: outputText, map: sourceMapText })
} else if (async) {
return transform(sourcecode, filename, tsCompilerOptionsToSwcConfig(options, filename)).then(({ code, map }) => {
return injectInlineSourceMap({ filename, code, map })
})
} else {
let swcRegisterConfig: Options
if (process.env.SWCRC) {
// when SWCRC environment variable is set to true it will use swcrc file
swcRegisterConfig = {
swc: {
swcrc: true,
},
}
} else {
swcRegisterConfig = tsCompilerOptionsToSwcConfig(options, filename)
}
const { code, map } = transformSync(sourcecode, filename, swcRegisterConfig)
return injectInlineSourceMap({ filename, code, map })
}
}
export function register(options: Partial<ts.CompilerOptions> = {}, hookOpts = {}) {
if (!process.env.SWCRC) {
options = readDefaultTsConfig()
}
options.module = ts.ModuleKind.CommonJS
installSourceMapSupport()
return addHook((code, filename) => compile(code, filename, options), {
exts: DEFAULT_EXTENSIONS,
...hookOpts,
})
}