This repository has been 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
/
index.js
153 lines (116 loc) · 4.67 KB
/
index.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
import * as ts from 'typescript';
import { createFilter } from 'rollup-pluginutils';
import * as path from 'path';
import * as fs from 'fs';
import assign from 'object-assign';
import compareVersions from 'compare-versions';
import { endsWith } from './string';
import { getDefaultOptions, compilerOptionsFromTsConfig, adjustCompilerOptions } from './options.js';
import fixExportClass from './fixExportClass';
import resolveHost from './resolveHost';
/*
interface Options {
tsconfig?: boolean;
include?: string | string[];
exclude?: string | string[];
typescript?: typeof ts;
module?: string;
}
*/
// The injected id for helpers. Intentially invalid to prevent helpers being included in source maps.
const helpersId = '\0typescript-helpers';
const helpersSource = fs.readFileSync( path.resolve( __dirname, '../src/typescript-helpers.js' ), 'utf-8' );
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 = options.typescript || ts;
delete options.typescript;
// Load options from `tsconfig.json` unless explicitly asked not to.
const tsconfig = options.tsconfig === false ? {} :
compilerOptionsFromTsConfig( typescript );
delete options.tsconfig;
// Since the CompilerOptions aren't designed for the Rollup
// use case, we'll adjust them for use with Rollup.
adjustCompilerOptions( typescript, tsconfig );
adjustCompilerOptions( typescript, options );
// Merge all options.
options = assign( tsconfig, getDefaultOptions(), options );
// Verify that we're targeting ES2015 modules.
if ( options.module !== 'es2015' && options.module !== 'es6' ) {
throw new Error( `rollup-plugin-typescript: The module kind should be 'es2015', found: '${ options.module }'` );
}
const parsed = typescript.convertCompilerOptionsFromJson( options, process.cwd() );
if ( parsed.errors.length ) {
parsed.errors.forEach( error => console.error( `rollup-plugin-typescript: ${ error.messageText }` ) );
throw new Error( `rollup-plugin-typescript: Couldn't process compiler options` );
}
const compilerOptions = parsed.options;
return {
resolveId ( importee, importer ) {
// Handle the special `typescript-helpers` import itself.
if ( importee === helpersId ) {
return helpersId;
}
if ( !importer ) return null;
let result;
importer = importer.split('\\').join('/');
if ( compareVersions( typescript.version, '1.8.0' ) < 0 ) {
// Suppress TypeScript warnings for function call.
result = typescript.nodeModuleNameResolver( importee, importer, resolveHost );
} else {
result = typescript.nodeModuleNameResolver( importee, importer, compilerOptions, resolveHost );
}
if ( result.resolvedModule && result.resolvedModule.resolvedFileName ) {
if ( endsWith( result.resolvedModule.resolvedFileName, '.d.ts' ) ) {
return null;
}
return result.resolvedModule.resolvedFileName;
}
return null;
},
load ( id ) {
if ( id === helpersId ) {
return helpersSource;
}
},
transform ( code, id ) {
if ( !filter( id ) ) return null;
const transformed = typescript.transpileModule( fixExportClass( code, id ), {
fileName: id,
reportDiagnostics: true,
compilerOptions
});
// All errors except `Cannot compile modules into 'es6' when targeting 'ES5' or lower.`
const diagnostics = transformed.diagnostics ?
transformed.diagnostics.filter( diagnostic => diagnostic.code !== 1204 ) : [];
let fatalError = false;
diagnostics.forEach( diagnostic => {
const 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` );
}
return {
// Always append an import for the helpers.
code: transformed.outputText +
`\nimport { __assign, __awaiter, __extends, __decorate, __metadata, __param } from '${helpersId}';`,
// Rollup expects `map` to be an object so we must parse the string
map: transformed.sourceMapText ? JSON.parse(transformed.sourceMapText) : null
};
}
};
}