-
-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathtranspiler.ts
163 lines (136 loc) · 4.57 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
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"
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") // tslint:disable-line
hasNativeTypeScript = true
} catch (e) {
d("Does not have TypeScript set up")
}
const checkForBabel = (prefix: BabelPackagePrefix) => {
require.resolve(`${prefix}core`) // tslint:disable-line
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) {
// @babel/polyfill is a direct dependency of Danger.
require("@babel/polyfill") // tslint:disable-line
try {
require.resolve(`${babelPackagePrefix}plugin-transform-typescript`) // tslint:disable-line
hasBabelTypeScript = true
} catch (e) {
d("Does not have Babel 7 TypeScript set up")
}
try {
require.resolve(`${babelPackagePrefix}plugin-transform-flow-strip-types`) // tslint:disable-line
hasFlow = true
} catch (e) {
d("Does not have Flow set up")
}
}
hasChecked = true
}
// Now that we have a sense of what exists inside the users' node modules
export const typescriptify = (content: string): string => {
const ts = require("typescript") // tslint:disable-line
// Support custom TSC options, but also fallback to defaults
let compilerOptions: any
if (fs.existsSync("tsconfig.json")) {
compilerOptions = JSON5.parse(fs.readFileSync("tsconfig.json", "utf8"))
} else {
compilerOptions = ts.getDefaultCompilerOptions()
}
let result = ts.transpileModule(content, sanitizeTSConfig(compilerOptions))
return result.outputText
}
const sanitizeTSConfig = (config: any) => {
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) {
safeConfig.compilerOptions.module = "commonjs"
}
return safeConfig
}
export const babelify = (content: string, filename: string, extraPlugins: string[]): string => {
const babel = require(`${babelPackagePrefix}core`) // tslint:disable-line
// 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) => {
if (!hasChecked) {
checkForNodeModules()
}
const filetype = path.extname(filename)
const isModule = filename.includes("node_modules")
if (isModule) {
return code
}
let result = code
if (hasNativeTypeScript && filetype.startsWith(".ts")) {
result = typescriptify(code)
} else if (hasBabel && hasBabelTypeScript && filetype.startsWith(".ts")) {
result = babelify(code, filename, [`${babelPackagePrefix}plugin-transform-typescript`])
} else if (hasBabel && filetype.startsWith(".js")) {
result = babelify(code, filename, hasFlow ? [`${babelPackagePrefix}plugin-transform-flow-strip-types`] : [])
}
return result
}