forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundler_spec.ts
192 lines (176 loc) · 6 KB
/
bundler_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import {MetadataBundler, MetadataBundlerHost} from '../src/bundler';
import {MetadataCollector} from '../src/collector';
import {ModuleMetadata} from '../src/schema';
import {Directory, open} from './typescript.mocks';
describe('metadata bundler', () => {
it('should be able to bundle a simple library', () => {
const host = new MockStringBundlerHost('/', SIMPLE_LIBRARY);
const bundler = new MetadataBundler('/lib/index', undefined, host);
const result = bundler.getMetadataBundle();
expect(Object.keys(result.metadata.metadata).sort()).toEqual([
'ONE_CLASSES', 'One', 'OneMore', 'TWO_CLASSES', 'Two', 'TwoMore', 'ɵa', 'ɵb'
]);
expect(result.privates).toEqual([
{privateName: 'ɵa', name: 'PrivateOne', module: './src/one'},
{privateName: 'ɵb', name: 'PrivateTwo', module: './src/two/index'}
]);
});
it('should be able to bundle an oddly constructed library', () => {
const host = new MockStringBundlerHost('/', {
'lib': {
'index.ts': `
export * from './src/index';
`,
'src': {
'index.ts': `
export {One, OneMore, ONE_CLASSES} from './one';
export {Two, TwoMore, TWO_CLASSES} from './two/index';
`,
'one.ts': `
class One {}
class OneMore extends One {}
class PrivateOne {}
const ONE_CLASSES = [One, OneMore, PrivateOne];
export {One, OneMore, PrivateOne, ONE_CLASSES};
`,
'two': {
'index.ts': `
class Two {}
class TwoMore extends Two {}
class PrivateTwo {}
const TWO_CLASSES = [Two, TwoMore, PrivateTwo];
export {Two, TwoMore, PrivateTwo, TWO_CLASSES};
`
}
}
}
});
const bundler = new MetadataBundler('/lib/index', undefined, host);
const result = bundler.getMetadataBundle();
expect(Object.keys(result.metadata.metadata).sort()).toEqual([
'ONE_CLASSES', 'One', 'OneMore', 'TWO_CLASSES', 'Two', 'TwoMore', 'ɵa', 'ɵb'
]);
expect(result.privates).toEqual([
{privateName: 'ɵa', name: 'PrivateOne', module: './src/one'},
{privateName: 'ɵb', name: 'PrivateTwo', module: './src/two/index'}
]);
});
it('should convert re-exported to the export', () => {
const host = new MockStringBundlerHost('/', {
'index.ts': `
export * from './bar';
export * from './foo';
`,
'bar.ts': `
import {Foo} from './foo';
export class Bar extends Foo {
}
`,
'foo.ts': `
export {Foo} from 'foo';
`
});
const bundler = new MetadataBundler('/index', undefined, host);
const result = bundler.getMetadataBundle();
// Expect the extends reference to refer to the imported module
expect((result.metadata.metadata as any).Bar.extends.module).toEqual('foo');
expect(result.privates).toEqual([]);
});
it('should treat import then export as a simple export', () => {
const host = new MockStringBundlerHost('/', {
'index.ts': `
export * from './a';
export * from './c';
`,
'a.ts': `
import { B } from './b';
export { B };
`,
'b.ts': `
export class B { }
`,
'c.ts': `
import { B } from './b';
export class C extends B { }
`
});
const bundler = new MetadataBundler('/index', undefined, host);
const result = bundler.getMetadataBundle();
expect(Object.keys(result.metadata.metadata).sort()).toEqual(['B', 'C']);
expect(result.privates).toEqual([]);
});
it('should be able to bundle a private from a un-exported module', () => {
const host = new MockStringBundlerHost('/', {
'index.ts': `
export * from './foo';
`,
'foo.ts': `
import {Bar} from './bar';
export class Foo extends Bar {
}
`,
'bar.ts': `
export class Bar {}
`
});
const bundler = new MetadataBundler('/index', undefined, host);
const result = bundler.getMetadataBundle();
expect(Object.keys(result.metadata.metadata).sort()).toEqual(['Foo', 'ɵa']);
expect(result.privates).toEqual([{privateName: 'ɵa', name: 'Bar', module: './bar'}]);
});
});
export class MockStringBundlerHost implements MetadataBundlerHost {
collector = new MetadataCollector();
constructor(private dirName: string, private directory: Directory) {}
getMetadataFor(moduleName: string): ModuleMetadata {
const fileName = path.join(this.dirName, moduleName) + '.ts';
const text = open(this.directory, fileName);
if (typeof text == 'string') {
const sourceFile = ts.createSourceFile(
fileName, text, ts.ScriptTarget.Latest, /* setParent */ true, ts.ScriptKind.TS);
const diagnostics: ts.Diagnostic[] = (sourceFile as any).parseDiagnostics;
if (diagnostics && diagnostics.length) {
throw Error('Unexpected syntax error in test');
}
const result = this.collector.getMetadata(sourceFile);
return result;
}
}
}
export const SIMPLE_LIBRARY = {
'lib': {
'index.ts': `
export * from './src/index';
`,
'src': {
'index.ts': `
export {One, OneMore, ONE_CLASSES} from './one';
export {Two, TwoMore, TWO_CLASSES} from './two/index';
`,
'one.ts': `
export class One {}
export class OneMore extends One {}
export class PrivateOne {}
export const ONE_CLASSES = [One, OneMore, PrivateOne];
`,
'two': {
'index.ts': `
export class Two {}
export class TwoMore extends Two {}
export class PrivateTwo {}
export const TWO_CLASSES = [Two, TwoMore, PrivateTwo];
`
}
}
}
};