-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompilerOptions.js
123 lines (111 loc) · 4.18 KB
/
compilerOptions.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
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
const defaultCompilerOptions = {
declarationMap: true,
importsNotUsedAsValues: 'error',
};
const requiredCompilerOptions = {
// required for babel compatibility
target: 'esnext',
module: 'esnext',
moduleResolution: 'node',
noEmit: true,
isolatedModules: true,
esModuleInterop: true,
resolveJsonModule: true,
allowSyntheticDefaultImports: undefined, // already enabled by esModuleInterop
// not currently supported, but maybe possible to translate to babel config?
baseUrl: undefined,
paths: undefined,
allowUmdGlobalAccess: undefined,
preserveSymlinks: undefined,
// options managed internally by generateDeclaration
declarationDir: undefined,
emitDeclarationOnly: undefined,
// compiler flags which are unused (as tsc is not used for compilation)
downlevelIteration: undefined,
emitDecoratorMetadata: undefined,
experimentalDecorators: undefined,
importHelpers: undefined,
inlineSourceMap: undefined,
inlineSources: undefined,
jsx: undefined,
lib: undefined,
mapRoot: undefined,
outDir: undefined,
outFile: undefined,
removeComments: undefined,
sourceMap: undefined,
sourceRoot: undefined,
};
const errorDetail = {
emitDecoratorMetadata: 'to enable decorator metadata, install and configure babel-plugin-transform-typescript-metadata instead - see https://github.com/davidje13/neutrino-typescript/issues/7 for details',
experimentalDecorators: 'to enable decorators, install and configure @babel/plugin-proposal-decorators with {legacy: true} instead - see https://github.com/davidje13/neutrino-typescript/issues/7 for details',
};
function informDeprecated(oldKey, newKey, value, newValue = value, extra = '') {
const oldConfig = { [oldKey]: value };
const newConfig = { tsconfig: { compilerOptions: { [newKey]: newValue } } };
console.warn(`typescript(${JSON.stringify(oldConfig)}) is deprecated; use typescript(${JSON.stringify(newConfig)}) instead${extra}`);
}
function getCompilerOptions(tsconfig, deprecated) {
const compilerOptions = { ...tsconfig.compilerOptions };
if (deprecated.declaration !== undefined) {
informDeprecated('declaration', 'declaration', deprecated.declaration);
if (compilerOptions.declaration === undefined) {
compilerOptions.declaration = deprecated.declaration;
}
}
if (deprecated.declarationMap !== undefined) {
informDeprecated('declarationMap', 'declarationMap', deprecated.declarationMap);
if (compilerOptions.declarationMap === undefined) {
compilerOptions.declarationMap = deprecated.declarationMap;
}
}
if (deprecated.onlyRemoveTypeImports !== undefined) {
const equivalent = deprecated.onlyRemoveTypeImports ? 'preserve' : 'remove';
informDeprecated(
'onlyRemoveTypeImports',
'importsNotUsedAsValues',
deprecated.onlyRemoveTypeImports,
equivalent,
', or specify "error" to ensure type imports are used correctly',
);
if (compilerOptions.importsNotUsedAsValues === undefined) {
compilerOptions.importsNotUsedAsValues = equivalent;
}
}
if (deprecated.jsxPragma !== undefined) {
informDeprecated(
'jsxPragma',
'jsxFactory',
deprecated.jsxPragma,
deprecated.jsxPragma,
', or specify neither for auto-discovery',
);
if (compilerOptions.jsxFactory === undefined) {
compilerOptions.jsxFactory = deprecated.jsxPragma + '.createElement';
}
}
Object.keys(defaultCompilerOptions).forEach((k) => {
if (compilerOptions[k] === undefined) {
compilerOptions[k] = defaultCompilerOptions[k];
}
});
Object.keys(requiredCompilerOptions).forEach((k) => {
const fixedValue = requiredCompilerOptions[k];
const userValue = compilerOptions[k];
if (userValue !== undefined) {
const ignoredConfig = { tsconfig: { compilerOptions: { [k]: userValue } } };
let info = '';
if (errorDetail[k]) {
info = ` (${errorDetail[k]})`;
} else if (fixedValue !== undefined) {
info = ` (${k} is always ${JSON.stringify(fixedValue)})`;
}
console.warn(`typescript(${JSON.stringify(ignoredConfig)}) will be ignored${info}`);
}
compilerOptions[k] = fixedValue;
});
return compilerOptions;
}
module.exports = {
getCompilerOptions,
};