-
-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathtranspiler.ts
223 lines (188 loc) · 6.17 KB
/
transpiler.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import * as fs from "fs"
import * as path from "path"
import JSON5 from "json5"
import { debug } from "../../../debug"
const enum BabelPackagePrefix {
V7 = "@babel/",
BEFORE_V7 = "babel-",
}
const disableTranspilation = process.env.DANGER_DISABLE_TRANSPILATION === "true"
const disableTsc = process.env.DANGER_DISABLE_TSC === "true"
let hasNativeTypeScript = false
let hasBabel = false
let hasBabelTypeScript = false
let hasFlow = false
let hasChecked = false
// By default assume Babel 7 is used
let babelPackagePrefix = BabelPackagePrefix.V7
const d = debug("transpiler:setup")
// Yes, lots of linter disables, but I want to support TS/Babel/Neither correctly
export const checkForNodeModules = () => {
if (disableTranspilation) {
hasChecked = true
d("DANGER_DISABLE_TRANSPILATION environment variable has been set to true, skipping transpilation")
return
}
try {
require.resolve("typescript")
hasNativeTypeScript = true && !disableTsc
} catch (e) {
d("Does not have TypeScript set up")
}
const checkForBabel = (prefix: BabelPackagePrefix) => {
require.resolve(`${prefix}core`)
babelPackagePrefix = prefix
hasBabel = true
}
try {
// Check for Babel 7
checkForBabel(BabelPackagePrefix.V7)
} catch (e) {
try {
// Check for older Babel versions
checkForBabel(BabelPackagePrefix.BEFORE_V7)
} catch (e) {
d("Does not have Babel set up")
}
}
if (hasBabel) {
require("core-js/stable") // tslint:disable-line
require("regenerator-runtime/runtime") // tslint:disable-line
try {
require.resolve(`${babelPackagePrefix}plugin-transform-typescript`)
hasBabelTypeScript = true
} catch (e) {
d("Does not have Babel 7 TypeScript set up")
}
try {
require.resolve(`${babelPackagePrefix}plugin-transform-flow-strip-types`)
hasFlow = true
} catch (e) {
d("Does not have Flow set up")
}
}
hasChecked = true
}
export const dirContains = (rootDir: string, dir: string): boolean => {
const relative = path.relative(rootDir, dir)
// on win32, relative can refer to a different drive
if (path.isAbsolute(relative)) {
return false
}
return !relative.startsWith("..")
}
// Now that we have a sense of what exists inside the users' node modules
export const lookupTSConfig = (dir: string): string | null => {
const filename = "tsconfig.json"
let filepath = path.join(dir, filename)
if (fs.existsSync(filepath)) {
return filepath
}
const rootDir = path.resolve()
dir = path.resolve(dir)
if (rootDir === dir) {
return null
}
// if root dir is disconnected, we only check in the root
if (!dirContains(rootDir, dir)) {
filepath = filename
return fs.existsSync(filepath) ? filepath : null
}
dir = path.dirname(dir)
do {
filepath = path.join(dir, filename)
if (fs.existsSync(filepath)) {
return path.relative(rootDir, filepath)
}
dir = path.dirname(dir)
} while (dirContains(rootDir, dir))
return null
}
export const typescriptify = (content: string, dir: string, esm: boolean = false): string => {
const ts = require("typescript")
// Support custom TSC options, but also fallback to defaults
let compilerOptions: any
const tsConfigPath = lookupTSConfig(dir)
if (tsConfigPath) {
compilerOptions = JSON5.parse(fs.readFileSync(tsConfigPath, "utf8"))
} else {
compilerOptions = ts.getDefaultCompilerOptions()
}
let result = ts.transpileModule(content, sanitizeTSConfig(compilerOptions, esm))
return result.outputText
}
const sanitizeTSConfig = (config: any, esm: boolean = false) => {
if (!config.compilerOptions) {
return config
}
const safeConfig = config
// It can make sense to ship TS code with modules
// for `import`/`export` syntax, but as we're running
// the transpiled code on vanilla node - it'll need to
// be used with plain old commonjs
//
// @see https://github.com/apollographql/react-apollo/pull/1402#issuecomment-351810274
//
if (safeConfig.compilerOptions.module) {
if (!esm) {
// .ts files should fall back to commonjs
safeConfig.compilerOptions.module = "commonjs"
} else {
// .mts files must use `import`/`export` syntax
safeConfig.compilerOptions.module = "es6"
}
}
return safeConfig
}
export const babelify = (content: string, filename: string, extraPlugins: string[]): string => {
const babel = require(`${babelPackagePrefix}core`)
// Since Babel 7, it is recommended to use `transformSync`.
// For older versions, we fallback to `transform`.
// @see https://babeljs.io/docs/en/babel-core#transform
const transformSync = babel.transformSync || babel.transform
if (!transformSync) {
return content
}
const options = babel.loadOptions ? babel.loadOptions({ filename }) : { plugins: [] }
const fileOpts = {
filename,
filenameRelative: filename,
sourceMap: false,
sourceFileName: undefined,
sourceType: "module",
plugins: [...extraPlugins, ...options.plugins],
}
const result = transformSync(content, fileOpts)
d("Result from Babel:")
d(result)
return result.code
}
export default (code: string, filename: string, remoteFile: boolean = false) => {
if (!hasChecked) {
checkForNodeModules()
}
let filetype: string
if (remoteFile) {
d(`Parsing the file from the remote reference ${filename}`)
let [file, _] = filename.split("@")
filetype = path.extname(file)
} else {
filetype = path.extname(filename)
}
const isModule = filename.includes("node_modules")
if (isModule) {
return code
}
let result = code
if (hasNativeTypeScript && (filetype.startsWith(".ts") || filetype.startsWith(".mts"))) {
d("compiling with typescript")
result = typescriptify(code, path.dirname(filename), filename.endsWith(".mts"))
} else if (hasBabel && hasBabelTypeScript && filetype.startsWith(".ts")) {
d("compiling as typescript with babel")
result = babelify(code, filename, [`${babelPackagePrefix}plugin-transform-typescript`])
} else if (hasBabel && filetype.startsWith(".js")) {
d("babelifying as javascript")
result = babelify(code, filename, hasFlow ? [`${babelPackagePrefix}plugin-transform-flow-strip-types`] : [])
}
return result
}