@@ -22,15 +22,16 @@ export interface TSConfig {
2222}
2323
2424const { compilerOptions } = JSON . parse ( readFileSync ( tsconfigPath || './tsconfig.json' ) . toString ( ) ) as TSConfig ;
25- const moduleMap : { [ name : string ] : string | null } = { } ;
25+ const moduleMap : { [ name : string ] : string | undefined } = { } ;
26+ const commentImportedModules = new Set < string > ( ) ;
2627for ( const moduleName of Object . keys ( compilerOptions . paths ) ) {
2728 if ( compilerOptions . paths [ moduleName ] . some ( ( x : string ) => x . endsWith ( '/COMMENT' ) ) ) {
2829 // COMMENT flag, remove such import statements from the code, because they will be imported with script tags for compatibility
29- moduleMap [ moduleName ] = null ; // eslint-disable-line no-null/no-null
30+ commentImportedModules . add ( moduleName ) ;
3031 } else {
3132 // replace import with full path from config
32- const selectedPath = compilerOptions . paths [ moduleName ] . find ( ( x : string ) => / \. ( m j s | j s ) $ / . exec ( x ) !== null ) ;
33- moduleMap [ moduleName ] = selectedPath ?. replace ( / ^ \. \/ e x t e n s i o n \/ / , '' ) || null ; // eslint-disable-line no-null/no-null
33+ const selectedPath = compilerOptions . paths [ moduleName ] . find ( ( x : string ) => / \. ( m j s | j s ) $ / . test ( x ) ) ;
34+ moduleMap [ moduleName ] = selectedPath ?. replace ( / ^ \. \/ e x t e n s i o n \/ / , '' ) ;
3435 }
3536}
3637
@@ -41,18 +42,18 @@ const importLineEndingWithJsNotStartingWithDot = /^(?!\s*\/\/)(?!\s*\/\*)(?:\s*i
4142
4243const resolveLineImports = ( regex : RegExp , line : string , path : string ) =>
4344 line . replace ( regex , ( found , prefix , libname : string , suffix ) => {
44- // eslint-disable-next-line no-null/no-null
45- if ( moduleMap [ libname ] === null ) {
45+ if ( commentImportedModules . has ( libname ) ) {
4646 return `// ${ prefix } ${ libname } ${ suffix } // commented during build process: imported with script tag` ;
47- } else if ( ! moduleMap [ libname ] ) {
47+ }
48+ const mappedModulePath = moduleMap [ libname ] ;
49+ if ( ! mappedModulePath ) {
4850 return found ;
49- } else {
50- const depth = path . split ( sep ) . length ;
51- const prePath = '../' . repeat ( depth - 3 ) ; // todo:
52- const resolved = `${ prefix } ${ prePath } ${ moduleMap [ libname ] } ${ suffix } ` ;
53- // console.info(`${path}: ${found} -> ${resolved}`);
54- return resolved ;
5551 }
52+ const depth = path . split ( sep ) . length ;
53+ const prePath = '../' . repeat ( depth - 3 ) ; // todo:
54+ const resolved = `${ prefix } ${ prePath } ${ mappedModulePath } ${ suffix } ` ;
55+ // console.info(`${path}: ${found} -> ${resolved}`);
56+ return resolved ;
5657 } ) ;
5758
5859const errIfSrcMissingJsExtensionInImport = ( src : string , path : string ) => {
0 commit comments