This repository was archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathindex.ts
124 lines (96 loc) · 3.37 KB
/
index.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
122
123
124
import * as ts from 'typescript';
import { createFilter } from 'rollup-pluginutils';
import { statSync } from 'fs';
import assign from 'object-assign';
// This is loaded verbatim.
import helpersTemplate from './typescript-helpers.ts';
import { endsWith } from './string';
import fixExportClass from './fixExportClass';
const jsxOptions = {
'preserve': ts.JsxEmit.Preserve,
'react': ts.JsxEmit.React
}
const resolveHost = {
fileExists ( filePath: string ): boolean {
try {
return statSync( filePath ).isFile();
} catch ( err ) {
return false;
}
}
};
function goodErrors ( diagnostic: ts.Diagnostic ): boolean {
// All errors except `Cannot compile modules into 'es6' when targeting 'ES5' or lower.`
return diagnostic.code !== 1204;
}
export default function typescript ( options ) {
options = assign( {}, options || {} );
const filter = createFilter(
options.include || [ '*.ts+(|x)', '**/*.ts+(|x)' ],
options.exclude || [ '*.d.ts', '**/*.d.ts' ] );
delete options.include;
delete options.exclude;
// Allow users to override the TypeScript version used for transpilation.
const typescript: typeof ts = options.typescript || ts;
if ( typeof options.jsx === 'string' ) {
options.jsx = jsxOptions[ options.jsx ] || ts.JsxEmit.None;
}
options = assign( {
noEmitHelpers: true,
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.ES6,
sourceMap: true
}, options );
return {
load ( id ) {
if ( id === 'typescript-helpers' ) {
return helpersTemplate;
}
},
resolveId ( importee: string, importer: string ): string {
// Handle the special `typescript-helpers` import itself.
if ( importee === 'typescript-helpers' ) return 'typescript-helpers';
if ( !importer ) return null;
var result = typescript.nodeModuleNameResolver( importee, importer, resolveHost );
if ( result.resolvedModule && result.resolvedModule.resolvedFileName ) {
if ( endsWith( result.resolvedModule.resolvedFileName, '.d.ts' ) ) {
return null;
}
return result.resolvedModule.resolvedFileName;
}
return null;
},
transform ( code: string, id: string ): { code: string, map: any } {
if ( !filter( id ) ) return null;
const transformed = typescript.transpileModule( fixExportClass( code, id ), {
fileName: id,
reportDiagnostics: true,
compilerOptions: options
});
const diagnostics = transformed.diagnostics.filter( goodErrors );
let fatalError = false;
diagnostics.forEach( diagnostic => {
var message = typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
if ( diagnostic.file ) {
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition( diagnostic.start );
console.error( `${diagnostic.file.fileName}(${line + 1},${character + 1}): error TS${diagnostic.code}: ${message}` );
} else {
console.error( `Error: ${message}` );
}
if ( diagnostic.category === ts.DiagnosticCategory.Error ) {
fatalError = true;
}
});
if ( fatalError ) {
throw new Error( `There were TypeScript errors transpiling "${id}"` );
}
return {
// Always append an import for the helpers.
code: transformed.outputText +
`\nimport { __extends, __decorate, __metadata, __param, __awaiter } from 'typescript-helpers';`,
// Rollup expects `map` to be an object so we must parse the string
map: JSON.parse(transformed.sourceMapText)
};
}
};
}