@@ -9,7 +9,8 @@ import * as path from 'path';
9
9
import * as ts from 'typescript' ;
10
10
11
11
import { MetadataCollector } from './collector' ;
12
- import { ClassMetadata , ConstructorMetadata , FunctionMetadata , MemberMetadata , MetadataEntry , MetadataError , MetadataImportedSymbolReferenceExpression , MetadataMap , MetadataObject , MetadataSymbolicExpression , MetadataSymbolicReferenceExpression , MetadataValue , MethodMetadata , ModuleMetadata , VERSION , isClassMetadata , isConstructorMetadata , isFunctionMetadata , isInterfaceMetadata , isMetadataError , isMetadataGlobalReferenceExpression , isMetadataImportedSymbolReferenceExpression , isMetadataModuleReferenceExpression , isMetadataSymbolicExpression , isMethodMetadata } from './schema' ;
12
+ import { ClassMetadata , ConstructorMetadata , FunctionMetadata , MemberMetadata , MetadataEntry , MetadataError , MetadataImportedSymbolReferenceExpression , MetadataMap , MetadataObject , MetadataSymbolicExpression , MetadataSymbolicReferenceExpression , MetadataValue , MethodMetadata , ModuleExportMetadata , ModuleMetadata , VERSION , isClassMetadata , isConstructorMetadata , isFunctionMetadata , isInterfaceMetadata , isMetadataError , isMetadataGlobalReferenceExpression , isMetadataImportedSymbolReferenceExpression , isMetadataModuleReferenceExpression , isMetadataSymbolicExpression , isMethodMetadata } from './schema' ;
13
+
13
14
14
15
15
16
// The character set used to produce private names.
@@ -44,6 +45,10 @@ interface Symbol {
44
45
// a referenced symbol's value.
45
46
referenced ?: boolean ;
46
47
48
+ // A symbol is marked as a re-export the symbol was rexported from a module that is
49
+ // not part of the flat module bundle.
50
+ reexport ?: boolean ;
51
+
47
52
// Only valid for referenced canonical symbols. Produces by convertSymbols().
48
53
value ?: MetadataEntry ;
49
54
@@ -98,14 +103,19 @@ export class MetadataBundler {
98
103
module : s . declaration . module
99
104
} ) ) ;
100
105
const origins = Array . from ( this . symbolMap . values ( ) )
101
- . filter ( s => s . referenced )
106
+ . filter ( s => s . referenced && ! s . reexport )
102
107
. reduce < { [ name : string ] : string } > ( ( p , s ) => {
103
108
p [ s . isPrivate ? s . privateName : s . name ] = s . declaration . module ;
104
109
return p ;
105
110
} , { } ) ;
111
+ const exports = this . getReExports ( exportedSymbols ) ;
106
112
return {
107
- metadata :
108
- { __symbolic : 'module' , version : VERSION , metadata, origins, importAs : this . importAs } ,
113
+ metadata : {
114
+ __symbolic : 'module' ,
115
+ version : VERSION ,
116
+ exports : exports . length ? exports : undefined , metadata, origins,
117
+ importAs : this . importAs
118
+ } ,
109
119
privates
110
120
} ;
111
121
}
@@ -165,12 +175,19 @@ export class MetadataBundler {
165
175
for ( const exportDeclaration of module . exports ) {
166
176
const exportFrom = resolveModule ( exportDeclaration . from , moduleName ) ;
167
177
// Record all the exports from the module even if we don't use it directly.
168
- this . exportAll ( exportFrom ) ;
178
+ const exportedSymbols = this . exportAll ( exportFrom ) ;
169
179
if ( exportDeclaration . export ) {
170
180
// Re-export all the named exports from a module.
171
181
for ( const exportItem of exportDeclaration . export ) {
172
182
const name = typeof exportItem == 'string' ? exportItem : exportItem . name ;
173
183
const exportAs = typeof exportItem == 'string' ? exportItem : exportItem . as ;
184
+ const symbol = this . symbolOf ( exportFrom , name ) ;
185
+ if ( exportedSymbols && exportedSymbols . length == 1 && exportedSymbols [ 0 ] . reexport &&
186
+ exportedSymbols [ 0 ] . name == '*' ) {
187
+ // This is a named export from a module we have no metadata about. Record the named
188
+ // export as a re-export.
189
+ symbol . reexport = true ;
190
+ }
174
191
exportSymbol ( this . symbolOf ( exportFrom , name ) , exportAs ) ;
175
192
}
176
193
} else {
@@ -183,6 +200,15 @@ export class MetadataBundler {
183
200
}
184
201
}
185
202
}
203
+
204
+ if ( ! module ) {
205
+ // If no metadata is found for this import then it is considered external to the
206
+ // library and should be recorded as a re-export in the final metadata if it is
207
+ // eventually re-exported.
208
+ const symbol = this . symbolOf ( moduleName , '*' ) ;
209
+ symbol . reexport = true ;
210
+ result . push ( symbol ) ;
211
+ }
186
212
this . exports . set ( moduleName , result ) ;
187
213
188
214
return result ;
@@ -207,6 +233,7 @@ export class MetadataBundler {
207
233
symbol . isPrivate = isPrivate ;
208
234
symbol . declaration = declaration ;
209
235
symbol . canonicalSymbol = canonicalSymbol ;
236
+ symbol . reexport = declaration . reexport ;
210
237
}
211
238
212
239
private getEntries ( exportedSymbols : Symbol [ ] ) : BundleEntries {
@@ -233,7 +260,7 @@ export class MetadataBundler {
233
260
exportedSymbols . forEach ( symbol => this . convertSymbol ( symbol ) ) ;
234
261
235
262
Array . from ( this . symbolMap . values ( ) ) . forEach ( symbol => {
236
- if ( symbol . referenced ) {
263
+ if ( symbol . referenced && ! symbol . reexport ) {
237
264
let name = symbol . name ;
238
265
if ( symbol . isPrivate && ! symbol . privateName ) {
239
266
name = newPrivateName ( ) ;
@@ -246,6 +273,36 @@ export class MetadataBundler {
246
273
return result ;
247
274
}
248
275
276
+ private getReExports ( exportedSymbols : Symbol [ ] ) : ModuleExportMetadata [ ] {
277
+ type ExportClause = { name : string , as : string } [ ] ;
278
+ const modules = new Map < string , ExportClause > ( ) ;
279
+ const exportAlls = new Set < string > ( ) ;
280
+ for ( const symbol of exportedSymbols ) {
281
+ if ( symbol . reexport ) {
282
+ const declaration = symbol . declaration ;
283
+ const module = declaration . module ;
284
+ if ( declaration . name == '*' ) {
285
+ // Reexport all the symbols.
286
+ exportAlls . add ( declaration . module ) ;
287
+ } else {
288
+ // Re-export the symbol as the exported name.
289
+ let entry = modules . get ( module ) ;
290
+ if ( ! entry ) {
291
+ entry = [ ] ;
292
+ modules . set ( module , entry ) ;
293
+ }
294
+ const as = symbol . name ;
295
+ const name = declaration . name ;
296
+ entry . push ( { name, as} ) ;
297
+ }
298
+ }
299
+ }
300
+ return [
301
+ ...Array . from ( exportAlls . values ( ) ) . map ( from => ( { from} ) ) ,
302
+ ...Array . from ( modules . entries ( ) ) . map ( ( [ from , exports ] ) => ( { export : exports , from} ) )
303
+ ] ;
304
+ }
305
+
249
306
private convertSymbol ( symbol : Symbol ) {
250
307
const canonicalSymbol = symbol . canonicalSymbol ;
251
308
0 commit comments