/
TSTypesTransformer.js
159 lines (138 loc) Β· 4.73 KB
/
TSTypesTransformer.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
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
// @flow strict-local
import {Transformer} from '@parcel/plugin';
import path from 'path';
import SourceMap from '@parcel/source-map';
import type {DiagnosticCodeFrame} from '@parcel/diagnostic';
import type {CompilerOptions} from 'typescript';
import ts from 'typescript';
import {CompilerHost, loadTSConfig} from '@parcel/ts-utils';
import {escapeMarkdown} from '@parcel/diagnostic';
import {TSModuleGraph} from './TSModuleGraph';
import nullthrows from 'nullthrows';
import {collect} from './collect';
import {shake} from './shake';
export default (new Transformer({
loadConfig({config, options}) {
return loadTSConfig(config, options);
},
transform({asset, config, options, logger}) {
let opts: CompilerOptions = {
// React is the default. Users can override this by supplying their own tsconfig,
// which many TypeScript users will already have for typechecking, etc.
jsx: ts.JsxEmit.React,
...config,
// Always emit output
noEmit: false,
noEmitOnError: false,
declaration: true,
declarationMap: true,
isolatedModules: false,
emitDeclarationOnly: true,
outFile: 'index.d.ts',
moduleResolution: ts.ModuleResolutionKind.NodeJs,
// createProgram doesn't support incremental mode
composite: false,
incremental: false,
};
let host = new CompilerHost(options.inputFS, ts, logger);
// $FlowFixMe
let program = ts.createProgram([asset.filePath], opts, host);
for (let file of program.getSourceFiles()) {
if (path.normalize(file.fileName) !== asset.filePath) {
asset.invalidateOnFileChange(
host.redirectTypes.get(file.fileName) ?? file.fileName,
);
}
}
let mainModuleName = path
.relative(program.getCommonSourceDirectory(), asset.filePath)
.slice(0, -path.extname(asset.filePath).length);
let moduleGraph = new TSModuleGraph(mainModuleName);
let emitResult = program.emit(undefined, undefined, undefined, true, {
afterDeclarations: [
// 1. Build module graph
context => sourceFile => {
return collect(moduleGraph, context, sourceFile);
},
// 2. Tree shake and rename types
context => sourceFile => {
return shake(moduleGraph, context, sourceFile);
},
],
});
let diagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
if (diagnostics.length > 0) {
for (let diagnostic of diagnostics) {
let filename = asset.filePath;
let {file} = diagnostic;
let diagnosticMessage =
typeof diagnostic.messageText === 'string'
? diagnostic.messageText
: diagnostic.messageText.messageText;
let codeframe: ?DiagnosticCodeFrame;
if (file != null && diagnostic.start != null) {
let source = file.text || diagnostic.source;
if (file.fileName) {
filename = file.fileName;
}
// $FlowFixMe
if (source) {
let lineChar = file.getLineAndCharacterOfPosition(diagnostic.start);
let start = {
line: lineChar.line + 1,
column: lineChar.character + 1,
};
let end = {
line: start.line,
column: start.column + 1,
};
if (
typeof diagnostic.start === 'number' &&
typeof diagnostic.length === 'number'
) {
let endCharPosition = file.getLineAndCharacterOfPosition(
diagnostic.start + diagnostic.length,
);
end = {
line: endCharPosition.line + 1,
column: endCharPosition.character + 1,
};
}
codeframe = {
filePath: filename,
code: source,
codeHighlights: [
{
start,
end,
message: escapeMarkdown(diagnosticMessage),
},
],
};
}
}
logger.warn({
message: escapeMarkdown(diagnosticMessage),
codeFrames: codeframe ? [codeframe] : undefined,
});
}
}
let code = nullthrows(host.outputCode);
code = code.substring(0, code.lastIndexOf('//# sourceMappingURL'));
let map = JSON.parse(nullthrows(host.outputMap));
map.sources = map.sources.map(source =>
path.join(path.dirname(asset.filePath), source),
);
let sourceMap = null;
if (map.mappings) {
sourceMap = new SourceMap(options.projectRoot);
sourceMap.addVLQMap(map);
}
asset.type = 'ts';
asset.setCode(code);
asset.setMap(sourceMap);
return [asset];
},
}): Transformer);