|
| 1 | +import { dirname, relative, resolve } from "path"; |
1 | 2 | import * as ts from "typescript"; |
2 | | -import { dirname, resolve, relative } from "path"; |
3 | 3 | import slash = require("slash"); |
4 | 4 |
|
5 | 5 | const transformer = <T extends ts.Node>(_: ts.Program) => { |
6 | 6 | return (context: ts.TransformationContext) => (rootNode: T) => { |
7 | 7 | 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 | + })); |
15 | 23 | let fileDir = ""; |
16 | 24 | function findFileInPaths(text: string) { |
17 | 25 | for (const path of regPaths) { |
|
0 commit comments