import * as upath from 'upath' ; import { CompilerOptions } from 'typescript' ; import { Shout } from './Shout' ; import chalk = require ( 'chalk' ) ; param baseUrl param value function convertTypeScriptPathToWebpackAliasPath ( rootPath , baseUrl , value ) { if ( upath . isAbsolute ( baseUrl ) === false ) { baseUrl = upath . join ( rootPath , baseUrl ) ; } let result = upath . join ( baseUrl , value ) ; if ( result . endsWith ( '/*' ) ) { result = result . substr ( 0 , result . length - 2 ) ; } return result ; } function convertMapSetOfStringToArray ( arg ) { const result = { } ; for ( const key in arg ) { const value = Array . from ( arg [ key ] ) ; if ( value . length ) { result [ key ] = value ; } } return result ; } /**
 * Translates tsconfig.json paths into webpack-compatible aliases!
 */ export function mergeTypeScriptPathAlias ( compilerOptions , rootPath , alias ) { const result = { } ; for ( const key in alias ) { const value = alias [ key ] ; result [ key ] = new Set < string > ( [ value ] ) ; } if ( compilerOptions . paths === undefined ) { return convertMapSetOfStringToArray ( result ) ; } if ( compilerOptions . baseUrl === undefined ) { Shout . warning ( chalk . cyanBright ( 'tsconfig.json' ) , 'paths are defined, but baseUrl is not!' , chalk . grey ( '(Ignoring paths)' ) ) ; return convertMapSetOfStringToArray ( result ) ; } for ( const key in compilerOptions . paths ) { if ( key === '*' ) { continue ; } let compatKey = key + '$' ; if ( key . endsWith ( '/*' ) ) { compatKey = key . substr ( 0 , key . length - 2 ) ; } if ( result [ compatKey ] === undefined ) { result [ compatKey ] = new Set < string > ( ) ; } for ( const value of compilerOptions . paths [ key ] ) { const compatValue = convertTypeScriptPathToWebpackAliasPath ( rootPath , compilerOptions . baseUrl , value ) ; result [ compatKey ] . add ( compatValue ) ; } } return convertMapSetOfStringToArray ( result ) ; } /**
 * Returns lookup folders for non-relative module imports, from TypeScript * paths.
 */ export function getWildcardModules ( compilerOptions , rootPath ) { if ( compilerOptions . baseUrl === undefined ) { return undefined ; } const r = new Set < string > ( ) ; const p = compilerOptions . paths ; if ( p && p [ '*' ] ) { for ( const value of p [ '*' ] ) { const result = convertTypeScriptPathToWebpackAliasPath ( rootPath , compilerOptions . baseUrl , value ) ; r . add ( result ) ; } } else { r . add ( compilerOptions . baseUrl ) ; } r . add ( 'node_modules' ) ; return Array . from ( r ) ; }