forked from frankwallis/plugin-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve-options.ts
121 lines (100 loc) · 3.94 KB
/
resolve-options.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
import ts from 'typescript'
import Logger from './logger'
import { formatErrors } from './format-errors'
const logger = new Logger({ debug: false })
export type CombinedOptions = PluginOptions & ts.CompilerOptions
export async function resolveOptions(
globalOptions: any,
fileOptions: any,
fileAddress: string,
fetchJson: FetchFunction): Promise<CombinedOptions> {
const globalTsconfigOptions = await loadTsconfigOptions(globalOptions, '', fetchJson)
const fileTsconfigOptions = await loadTsconfigOptions(fileOptions, fileAddress, fetchJson)
const mergedOptions = {
...globalTsconfigOptions,
...globalOptions,
...fileTsconfigOptions,
...fileOptions
}
const finalOptions = parseOptions(mergedOptions)
validateOptions(finalOptions)
return finalOptions
}
async function loadTsconfigOptions(
options: CombinedOptions,
parentAddress: string,
fetchJson: FetchFunction): Promise<ts.CompilerOptions> {
let tsconfigName = options && options.tsconfig
if (tsconfigName === true) tsconfigName = 'tsconfig.json'
if (tsconfigName) {
const tsconfigText = await fetchJson(tsconfigName, parentAddress)
const result = ts.parseConfigFileTextToJson(tsconfigName, tsconfigText)
if (result.error) {
formatErrors([result.error], logger)
throw new Error(`failed to load tsconfig from ${tsconfigName}`)
}
else {
const extendedTsconfig = result.config.extends
? await loadTsconfigOptions(options, result.config.extends, fetchJson)
: undefined
return {
...extendedTsconfig,
...result.config.compilerOptions
}
}
}
else {
return undefined
}
}
export function parseOptions(options: any): CombinedOptions {
const result = options || {}
result.module = getEnum(result.module, ts.ModuleKind, ts.ModuleKind.System)
result.target = getEnum(result.target, ts.ScriptTarget, ts.ScriptTarget.ES5)
result.jsx = getEnum(result.jsx, ts.JsxEmit, ts.JsxEmit.None)
result.allowNonTsExtensions = (result.allowNonTsExtensions !== false)
result.skipDefaultLibCheck = (result.skipDefaultLibCheck !== false)
result.noResolve = true
result.allowSyntheticDefaultImports = (result.allowSyntheticDefaultImports !== false)
// Force module resolution into 'classic' mode, to prevent node module resolution from kicking in
result.moduleResolution = ts.ModuleResolutionKind.Classic
// When bundling automatically output es6 modules instead of system to enable rollup support
// if (builder) {
// if (result.module === ts.ModuleKind.System) {
// logger.log('switching output from system.register -> es modules to support rollup')
// result.module = ts.ModuleKind.ES6
// }
// }
return result
}
function getEnum(enumValue: any, enumType: any, defaultValue: number): number {
if (enumValue == undefined) return defaultValue
for (var enumProp in enumType) {
if (enumProp.toLowerCase() === enumValue.toString().toLowerCase()) {
if (typeof enumType[enumProp] === "string")
return enumType[enumType[enumProp]]
else
return enumType[enumProp]
}
}
throw new Error(`Unrecognised value [${enumValue}]`)
}
function validateOptions(options: CombinedOptions) {
/* The only time you don't want to output in 'm format is when you are using rollup or babel
downstream to compile es6 output (e.g. for async/await support) */
if ((options.module !== ts.ModuleKind.System) && (options.module !== ts.ModuleKind.ES2015) && (options.module !== ts.ModuleKind.ESNext)) {
logger.warn(`transpiling to ${ts.ModuleKind[options.module]}, consider setting module: "system" in typescriptOptions to transpile directly to System.register format`)
}
if (options['supportHtmlImports']) {
logger.warn("The 'supportHtmlImports' option is no longer supported")
}
if (options['resolveAmbientRefs']) {
logger.warn("The 'resolveAmbientRefs' option is no longer supported")
}
if (options['targetLib']) {
logger.warn("The 'targetLib' option is no longer supported")
}
if (options['typeCheck']) {
logger.error("The 'typeCheck' option is no longer supported")
}
}