Skip to content

Commit c3710c4

Browse files
committed
fix: check for paths and baseUrl in tsconfig
1 parent 73dd8e2 commit c3710c4

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/index.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1+
import { dirname, relative, resolve } from "path";
12
import * as ts from "typescript";
2-
import { dirname, resolve, relative } from "path";
33
import slash = require("slash");
44

55
const transformer = <T extends ts.Node>(_: ts.Program) => {
66
return (context: ts.TransformationContext) => (rootNode: T) => {
77
const compilerOptions = context.getCompilerOptions();
8-
// TODO should check if baseUrl and paths are defined
9-
const baseUrl = compilerOptions.baseUrl!;
10-
const paths = compilerOptions.paths!;
11-
const regPaths = Object.keys(paths).map(key => ({
12-
regexp: new RegExp("^" + key.replace("*", "(.*)") + "$"),
13-
resolve: paths[key][0] // TODO should check if is not empty
14-
}));
8+
if (
9+
compilerOptions.baseUrl === undefined ||
10+
compilerOptions.paths === undefined
11+
) {
12+
throw new Error(
13+
"Should define baseUrl and paths properties in the tsconfig"
14+
);
15+
}
16+
const baseUrl = compilerOptions.baseUrl;
17+
const paths = compilerOptions.paths;
18+
const regPaths = Object.keys(paths)
19+
.map(key => ({
20+
regexp: new RegExp("^" + key.replace("*", "(.*)") + "$"),
21+
resolve: paths[key][0]
22+
}));
1523
let fileDir = "";
1624
function findFileInPaths(text: string) {
1725
for (const path of regPaths) {

0 commit comments

Comments
 (0)