/
normalize.ts
71 lines (65 loc) 路 2.5 KB
/
normalize.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
/* eslint-disable no-param-reassign */
import { resolve } from 'path';
import { CompilerOptions, PartialCompilerOptions } from './interfaces';
export const DIRECTORY_PROPS = ['outDir', 'declarationDir'] as const;
/**
* Mutates the compiler options to convert paths from relative to absolute.
* This should be used with compiler options passed through the Rollup plugin options,
* not those found from loading a tsconfig.json file.
* @param compilerOptions Compiler options to _mutate_.
* @param relativeTo Paths are resolved relative to this path.
*/
export function makePathsAbsolute(compilerOptions: PartialCompilerOptions, relativeTo: string) {
for (const pathProp of DIRECTORY_PROPS) {
if (compilerOptions[pathProp]) {
compilerOptions[pathProp] = resolve(relativeTo, compilerOptions[pathProp] as string);
}
}
}
/**
* Mutates the compiler options to normalize some values for Rollup.
* @param compilerOptions Compiler options to _mutate_.
* @returns True if the source map compiler option was not initially set.
*/
export function normalizeCompilerOptions(
ts: typeof import('typescript'),
compilerOptions: CompilerOptions
) {
let autoSetSourceMap = false;
if (compilerOptions.inlineSourceMap) {
// Force separate source map files for Rollup to work with.
compilerOptions.sourceMap = true;
compilerOptions.inlineSourceMap = false;
} else if (typeof compilerOptions.sourceMap !== 'boolean') {
// Default to using source maps.
// If the plugin user sets sourceMap to false we keep that option.
compilerOptions.sourceMap = true;
// Using inlineSources to make sure typescript generate source content
// instead of source path.
compilerOptions.inlineSources = true;
autoSetSourceMap = true;
}
switch (compilerOptions.module) {
case ts.ModuleKind.ES2015:
case ts.ModuleKind.ESNext:
case ts.ModuleKind.Node16:
case ts.ModuleKind.NodeNext:
case ts.ModuleKind.CommonJS:
// OK module type
return autoSetSourceMap;
case ts.ModuleKind.None:
case ts.ModuleKind.AMD:
case ts.ModuleKind.UMD:
case ts.ModuleKind.System: {
// Invalid module type
const moduleType = ts.ModuleKind[compilerOptions.module];
throw new Error(
`@rollup/plugin-typescript: The module kind should be 'ES2015', 'ESNext', 'node16' or 'nodenext', found: '${moduleType}'`
);
}
default:
// Unknown or unspecified module type, force ESNext
compilerOptions.module = ts.ModuleKind.ESNext;
}
return autoSetSourceMap;
}