Skip to content

Commit fb4ffc0

Browse files
committed
Add support for path mappings, fix #299
1 parent 2d6852c commit fb4ffc0

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

lib/main.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function getJsxEmit(typescript: typeof ts, jsx: string) {
146146
return map[jsx.toLowerCase()];
147147
}
148148

149-
function getCompilerOptions(settings: compile.Settings): ts.CompilerOptions {
149+
function getCompilerOptions(settings: compile.Settings, projectPath: string): ts.CompilerOptions {
150150
const tsSettings: ts.CompilerOptions = {};
151151

152152
var typescript = settings.typescript || ts;
@@ -218,6 +218,15 @@ function getCompilerOptions(settings: compile.Settings): ts.CompilerOptions {
218218
// Suppress errors when providing `allowJs` without `outDir`.
219219
(<tsApi.TSOptions18> tsSettings).suppressOutputPathCheck = true;
220220

221+
if ((<tsApi.TSOptions20> tsSettings).baseUrl) {
222+
(<tsApi.TSOptions20> tsSettings).baseUrl = path.resolve(projectPath, (<tsApi.TSOptions20> tsSettings).baseUrl);
223+
}
224+
if ((<tsApi.TSOptions20> tsSettings).rootDirs) {
225+
(<tsApi.TSOptions20> tsSettings).rootDirs = (<tsApi.TSOptions20> tsSettings).rootDirs.map(
226+
dir => path.resolve(projectPath, dir)
227+
);
228+
}
229+
221230
return tsSettings;
222231
}
223232

@@ -272,9 +281,11 @@ module compile {
272281
export function createProject(fileNameOrSettings?: string | Settings, settings?: Settings): Project {
273282
let tsConfigFileName: string = undefined;
274283
let tsConfigContent: tsConfig.TsConfig = undefined;
284+
let projectDirectory = process.cwd();
275285
if (fileNameOrSettings !== undefined) {
276286
if (typeof fileNameOrSettings === 'string') {
277287
tsConfigFileName = fileNameOrSettings;
288+
projectDirectory = path.dirname(fileNameOrSettings);
278289
// load file and strip BOM, since JSON.parse fails to parse if there's a BOM present
279290
let tsConfigText = fs.readFileSync(fileNameOrSettings).toString();
280291
const typescript = (settings && settings.typescript) || ts;
@@ -300,7 +311,7 @@ module compile {
300311
}
301312
}
302313

303-
const project = new Project(tsConfigFileName, tsConfigContent, getCompilerOptions(settings), settings.noExternalResolve ? true : false, settings.sortOutput ? true : false, settings.typescript);
314+
const project = new Project(tsConfigFileName, tsConfigContent, getCompilerOptions(settings, projectDirectory), settings.noExternalResolve ? true : false, settings.sortOutput ? true : false, settings.typescript);
304315

305316
// Isolated modules are only supported when using TS1.5+
306317
if (project.options['isolatedModules'] && !tsApi.isTS14(project.typescript)) {

lib/tsapi.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ export interface TSFile15 {
7373
getLineAndCharacterOfPosition(pos: number): ts.LineAndCharacter;
7474
}
7575

76-
export interface TSOptions18 extends ts.CompilerOptions {
77-
allowJs: boolean;
78-
suppressOutputPathCheck: boolean;
76+
export interface TSOptions18 {
77+
allowJs?: boolean;
78+
suppressOutputPathCheck?: boolean;
79+
}
80+
export interface TSOptions20 extends TSOptions18 {
81+
baseUrl?: string;
82+
rootDirs?: string[];
7983
}
8084

8185
export function isTS14(typescript: typeof ts) {

0 commit comments

Comments
 (0)