feat(tsc-wrapped): record original location of flattened symbols (#15367)

Added an "origins" section to the flat module `.metadata.json` files
that records where the original symbols was declared. This allows
correctly calculating relative path references recorded in metadata.
This commit is contained in:
Chuck Jazdzewski 2017-03-20 16:30:50 -07:00 committed by Miško Hevery
parent 5efc86069f
commit 7354949763
3 changed files with 23 additions and 5 deletions

View File

@ -92,7 +92,7 @@ export class MetadataBundler {
const exportedSymbols = this.exportAll(this.rootModule);
this.canonicalizeSymbols(exportedSymbols);
// TODO: exports? e.g. a module re-exports a symbol from another bundle
const entries = this.getEntries(exportedSymbols);
const metadata = this.getEntries(exportedSymbols);
const privates = Array.from(this.symbolMap.values())
.filter(s => s.referenced && s.isPrivate)
.map(s => ({
@ -100,9 +100,15 @@ export class MetadataBundler {
name: s.declaration.name,
module: s.declaration.module
}));
const origins = Array.from(this.symbolMap.values())
.filter(s => s.referenced)
.reduce<{[name: string]: string}>((p, s) => {
p[s.isPrivate ? s.privateName : s.name] = s.declaration.module;
return p;
}, {});
return {
metadata:
{__symbolic: 'module', version: VERSION, metadata: entries, importAs: this.importAs},
{__symbolic: 'module', version: VERSION, metadata, origins, importAs: this.importAs},
privates
};
}
@ -235,7 +241,6 @@ export class MetadataBundler {
let name = symbol.name;
if (symbol.isPrivate && !symbol.privateName) {
name = newPrivateName();
;
symbol.privateName = name;
}
result[name] = symbol.value;

View File

@ -25,6 +25,7 @@ export interface ModuleMetadata {
exports?: ModuleExportMetadata[];
importAs?: string;
metadata: {[name: string]: MetadataEntry};
origins?: {[name: string]: string};
}
export function isModuleMetadata(value: any): value is ModuleMetadata {
return value && value.__symbolic === 'module';

View File

@ -25,9 +25,21 @@ describe('metadata bundler', () => {
expect(Object.keys(result.metadata.metadata).sort()).toEqual([
'ONE_CLASSES', 'One', 'OneMore', 'TWO_CLASSES', 'Two', 'TwoMore', 'ɵa', 'ɵb'
]);
const originalOne = './src/one';
const originalTwo = './src/two/index';
expect(Object.keys(result.metadata.origins)
.sort()
.map(name => ({name, value: result.metadata.origins[name]})))
.toEqual([
{name: 'ONE_CLASSES', value: originalOne}, {name: 'One', value: originalOne},
{name: 'OneMore', value: originalOne}, {name: 'TWO_CLASSES', value: originalTwo},
{name: 'Two', value: originalTwo}, {name: 'TwoMore', value: originalTwo},
{name: 'ɵa', value: originalOne}, {name: 'ɵb', value: originalTwo}
]);
expect(result.privates).toEqual([
{privateName: 'ɵa', name: 'PrivateOne', module: './src/one'},
{privateName: 'ɵb', name: 'PrivateTwo', module: './src/two/index'}
{privateName: 'ɵa', name: 'PrivateOne', module: originalOne},
{privateName: 'ɵb', name: 'PrivateTwo', module: originalTwo}
]);
});