0
# Internal API
1
2
Core internal classes and methods that make up tsify's TypeScript compilation engine.
3
4
## Capabilities
5
6
### Tsifier Class
7
8
Main compilation orchestrator that manages TypeScript compilation and caching.
9
10
```javascript { .api }
11
/**
12
* Main TypeScript compilation orchestrator
13
* @param opts - Parsed TypeScript compiler options
14
* @param bopts - Browserify options
15
*/
16
class Tsifier extends EventEmitter {
17
constructor(opts: any, bopts: any);
18
19
/** Reset compilation state and clear caches */
20
reset(): void;
21
22
/** Generate compilation cache for given files */
23
generateCache(files: string[], ignoredFiles?: string[]): void;
24
25
/** Add files to compilation scope */
26
addFiles(files: string[]): void;
27
28
/** Perform TypeScript compilation */
29
compile(): void;
30
31
/** Check syntax errors in program */
32
checkSyntax(program: any): any[];
33
34
/** Check semantic errors in program */
35
checkSemantics(program: any): any[];
36
37
/** Transform stream for processing files */
38
transform(file: string, data: string): any;
39
}
40
```
41
42
**Usage Example:**
43
44
```javascript
45
const { Tsifier } = require('tsify/lib/Tsifier')(require('typescript'));
46
47
// Tsifier is used internally by the main plugin function
48
// Not typically used directly by consumers
49
```
50
51
### Host Class
52
53
Virtual file system implementation that manages TypeScript source files in memory.
54
55
```javascript { .api }
56
/**
57
* TypeScript compiler host implementation
58
* @param currentDirectory - Base directory for compilation
59
* @param opts - TypeScript compiler options
60
*/
61
class Host extends EventEmitter {
62
constructor(currentDirectory: string, opts: any);
63
64
/** Reset file cache and increment version */
65
_reset(): void;
66
67
/** Add file to virtual file system */
68
_addFile(filename: string, root?: boolean): void;
69
70
/** Compile TypeScript program */
71
_compile(opts: any): any;
72
73
/** Get canonical file name (handles case sensitivity) */
74
getCanonicalFileName(filename: string): string;
75
76
/** Check if file exists in virtual file system */
77
fileExists(filename: string): boolean;
78
79
/** Read file from virtual file system */
80
readFile(filename: string): string;
81
82
/** Get source file from compilation */
83
getSourceFile(filename: string, languageVersion: any): any;
84
85
/** Write compiled output */
86
writeFile(filename: string, data: string): void;
87
88
/** Get current directory */
89
getCurrentDirectory(): string;
90
91
/** Get new line character */
92
getNewLine(): string;
93
94
/** Check if file name casing is correct */
95
useCaseSensitiveFileNames(): boolean;
96
}
97
```
98
99
### CompileError Class
100
101
Error class for TypeScript compilation errors with detailed diagnostic information.
102
103
```javascript { .api }
104
/**
105
* TypeScript compilation error with detailed diagnostics
106
* @param diagnostic - TypeScript diagnostic object
107
*/
108
class CompileError extends SyntaxError {
109
constructor(diagnostic: any);
110
111
/** File name where error occurred */
112
fileName?: string;
113
114
/** Line number of error */
115
line?: number;
116
117
/** Column number of error */
118
column?: number;
119
120
/** Error name */
121
name: string;
122
123
/** Formatted error message */
124
message: string;
125
}
126
```
127
128
**Error Message Format:**
129
```
130
filename.ts(line,column): category TS#### error message
131
```
132
133
**Usage Example:**
134
135
```javascript
136
browserify()
137
.add('main.ts')
138
.plugin(tsify)
139
.bundle()
140
.on('error', function(error) {
141
if (error instanceof CompileError) {
142
console.log('File:', error.fileName);
143
console.log('Location:', error.line + ':' + error.column);
144
console.log('Message:', error.message);
145
}
146
});
147
```
148
149
## Utility Functions
150
151
### File Type Detection
152
153
Internal utility functions for determining file types.
154
155
```javascript { .api }
156
/** Check if file is TypeScript */
157
function isTypescript(file: string): boolean;
158
159
/** Check if file is TSX */
160
function isTsx(file: string): boolean;
161
162
/** Check if file is JavaScript */
163
function isJavascript(file: string): boolean;
164
165
/** Check if file is TypeScript declaration */
166
function isTypescriptDeclaration(file: string): boolean;
167
```
168
169
### File System Utilities
170
171
```javascript { .api }
172
/** Replace file extension */
173
function replaceFileExtension(file: string, extension: string): string;
174
175
/** Check if file exists on disk */
176
function fileExists(file: string): boolean;
177
```
178
179
## Types
180
181
```typescript { .api }
182
interface ParsedOptions {
183
/** TypeScript compiler options */
184
options: any;
185
/** Array of file names to compile */
186
fileNames: string[];
187
}
188
189
interface TsifierInstance {
190
/** Parsed TypeScript options */
191
opts: any;
192
/** Files to compile */
193
files: string[];
194
/** Files to ignore */
195
ignoredFiles: string[];
196
/** Browserify options */
197
bopts: any;
198
/** Compiler host instance */
199
host: Host;
200
}
201
```