@@ -41,6 +41,7 @@ import { generateOutput, ModuleImportsSet, OutputInputData } from './generate-ou
41
41
import {
42
42
normalLog ,
43
43
verboseLog ,
44
+ warnLog ,
44
45
} from './logger' ;
45
46
import { CollisionsResolver } from './collisions-resolver' ;
46
47
@@ -1101,7 +1102,9 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
1101
1102
return collisionsResolver . resolveReferencedIdentifier ( nodeName as ts . Identifier ) === exportedName ;
1102
1103
}
1103
1104
1104
- return generateOutput (
1105
+ const renamedAndNotExplicitlyExportedTypes : ts . NamedDeclaration [ ] = [ ] ;
1106
+
1107
+ const output = generateOutput (
1105
1108
{
1106
1109
...collectionResult ,
1107
1110
resolveIdentifierName : ( identifier : ts . Identifier | ts . QualifiedName | ts . PropertyAccessEntityNameExpression ) : string | null => {
@@ -1123,7 +1126,7 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
1123
1126
// an export keyword (like interface, type, etc) otherwise, if there are
1124
1127
// only re-exports with renaming (like export { foo as bar }) we don't need
1125
1128
// to put export keyword for this statement because we'll re-export it in the way
1126
- let result = statementExports . length === 0 || statementExports . find ( ( exp : SourceFileExport ) => {
1129
+ const isExplicitlyExported = statementExports . find ( ( exp : SourceFileExport ) => {
1127
1130
if ( ts . isVariableStatement ( statement ) ) {
1128
1131
for ( const variableDeclaration of statement . declarationList . declarations ) {
1129
1132
if ( ts . isIdentifier ( variableDeclaration . name ) ) {
@@ -1148,22 +1151,27 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
1148
1151
// e.g. classes/functions/etc must be exported from the root source file to have an "export" keyword
1149
1152
// by default interfaces/types are exported even if they aren't directly exported (e.g. when they are referenced by other types)
1150
1153
// but if `exportReferencedTypes` option is disabled we have to check direct export for them either
1151
- const onlyDirectlyExportedShouldBeExported = ! exportReferencedTypes
1154
+ const onlyExplicitlyExportedShouldBeExported = ! exportReferencedTypes
1152
1155
|| ts . isClassDeclaration ( statement )
1153
1156
|| ( ts . isEnumDeclaration ( statement ) && ! hasNodeModifier ( statement , ts . SyntaxKind . ConstKeyword ) )
1154
1157
|| ts . isFunctionDeclaration ( statement )
1155
1158
|| ts . isVariableStatement ( statement )
1156
1159
|| ts . isModuleDeclaration ( statement ) ;
1157
1160
1158
- if ( onlyDirectlyExportedShouldBeExported ) {
1161
+ if ( onlyExplicitlyExportedShouldBeExported ) {
1159
1162
// "valuable" statements must be re-exported from root source file
1160
1163
// to having export keyword in declaration file
1161
- result = result && statementExports . length !== 0 ;
1162
- } else if ( isNodeNamedDeclaration ( statement ) && ! isExportedWithLocalName ( statement , getNodeName ( statement ) ! . getText ( ) ) ) { // eslint-disable-line @typescript-eslint/no-non-null-assertion
1164
+ return isExplicitlyExported ;
1165
+ }
1166
+
1167
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1168
+ if ( isNodeNamedDeclaration ( statement ) && ! isExportedWithLocalName ( statement , getNodeName ( statement ) ! . getText ( ) ) ) {
1169
+ // if a type node was renamed because of name collisions it shouldn't be exported with its new name
1170
+ renamedAndNotExplicitlyExportedTypes . push ( statement ) ;
1163
1171
return false ;
1164
1172
}
1165
1173
1166
- return result ;
1174
+ return isExplicitlyExported || statementExports . length === 0 ;
1167
1175
} ,
1168
1176
needStripConstFromConstEnum : ( constEnum : ts . EnumDeclaration ) => {
1169
1177
if ( ! program . getCompilerOptions ( ) . preserveConstEnums || ! outputOptions . respectPreserveConstEnum ) {
@@ -1195,5 +1203,18 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
1195
1203
noBanner : outputOptions . noBanner ,
1196
1204
}
1197
1205
) ;
1206
+
1207
+ if ( renamedAndNotExplicitlyExportedTypes . length !== 0 ) {
1208
+ warnLog ( `The following type nodes were renamed because of the name collisions and will not be exported from the generated bundle:\n- ${
1209
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1210
+ renamedAndNotExplicitlyExportedTypes . map ( node => `${ getNodeName ( node ) ! . getText ( ) } (from ${ node . getSourceFile ( ) . fileName } )` ) . join ( '\n- ' )
1211
+ } ${
1212
+ '\n'
1213
+ } This might lead to unpredictable and unexpected output, and possible breaking changes to your API.${
1214
+ '\n'
1215
+ } Consider either (re-)exporting them explicitly from the entry point, or disable --export-referenced-types option ('output.exportReferencedTypes' in the config).`) ;
1216
+ }
1217
+
1218
+ return output ;
1198
1219
} ) ;
1199
1220
}
0 commit comments