Skip to content

Commit

Permalink
fix(tsc-wrapped): deduplicate metadata for re-exported modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe authored and matsko committed Sep 7, 2017
1 parent 3c480e4 commit c056b8c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/compiler/test/aot/compiler_spec.ts
Expand Up @@ -863,6 +863,7 @@ const LIBRARY: MockDirectory = {
'public-api.ts': `
export * from './src/bolder.component';
export * from './src/bolder.module';
export {BolderModule as ReExportedModule} from './src/bolder.module';
`,
src: {
'bolder.component.ts': `
Expand Down
26 changes: 26 additions & 0 deletions packages/tsc-wrapped/src/bundler.ts
Expand Up @@ -261,17 +261,43 @@ export class MetadataBundler {

exportedSymbols.forEach(symbol => this.convertSymbol(symbol));

const symbolsMap = new Map<string, string[]>();
Array.from(this.symbolMap.values()).forEach(symbol => {
if (symbol.referenced && !symbol.reexport) {
let name = symbol.name;
const declaredName = symbol.declaration !.name;
if (symbol.isPrivate && !symbol.privateName) {
name = newPrivateName();
symbol.privateName = name;
}
if (symbolsMap.has(declaredName)) {
const names = symbolsMap.get(declaredName);
names !.push(name);
} else {
symbolsMap.set(declaredName, [name]);
}
result[name] = symbol.value !;
}
});

// check for duplicated entries
symbolsMap.forEach((names: string[], declaredName: string) => {
if (names.length > 1) {
// prefer the export that uses the declared name (if any)
let reference = names.indexOf(declaredName);
if (reference === -1) {
reference = 0;
}

// keep one entry and replace the others by references
names.forEach((name: string, i: number) => {
if (i !== reference) {
result[name] = {__symbolic: 'reference', name: declaredName};
}
});
}
});

return result;
}

Expand Down
33 changes: 31 additions & 2 deletions packages/tsc-wrapped/test/bundler_spec.ts
Expand Up @@ -11,7 +11,7 @@ import * as ts from 'typescript';

import {MetadataBundler, MetadataBundlerHost} from '../src/bundler';
import {MetadataCollector} from '../src/collector';
import {ModuleMetadata} from '../src/schema';
import {ClassMetadata, MetadataGlobalReferenceExpression, ModuleMetadata} from '../src/schema';

import {Directory, open} from './typescript.mocks';

Expand Down Expand Up @@ -192,6 +192,35 @@ describe('metadata bundler', () => {
]);
expect(result.metadata.origins !['E']).toBeUndefined();
});

it('should be able to de-duplicate symbols of re-exported modules', () => {
const host = new MockStringBundlerHost('/', {
'public-api.ts': `
export {A as A2, A, B as B1, B as B2} from './src/core';
`,
'src': {
'core.ts': `
export class A {}
export class B {}
`,
}
});

const bundler = new MetadataBundler('/public-api', undefined, host);
const result = bundler.getMetadataBundle();
const {A, A2, B1, B2} = result.metadata.metadata as{
A: ClassMetadata,
A2: MetadataGlobalReferenceExpression,
B1: ClassMetadata,
B2: MetadataGlobalReferenceExpression
};
expect(A.__symbolic).toEqual('class');
expect(A2.__symbolic).toEqual('reference');
expect(A2.name).toEqual('A');
expect(B1.__symbolic).toEqual('class');
expect(B2.__symbolic).toEqual('reference');
expect(B2.name).toEqual('B');
});
});

export class MockStringBundlerHost implements MetadataBundlerHost {
Expand Down Expand Up @@ -242,4 +271,4 @@ export const SIMPLE_LIBRARY = {
}
}
}
};
};

0 comments on commit c056b8c

Please sign in to comment.