-
-
Notifications
You must be signed in to change notification settings - Fork 627
Expand file tree
/
Copy pathdeclarations.js
More file actions
210 lines (206 loc) · 6.07 KB
/
declarations.js
File metadata and controls
210 lines (206 loc) · 6.07 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { rollup } from 'rollup';
import typescript from '..';
import { getCode, onwarn } from '../../../util/test';
beforeEach(() => process.chdir(__dirname));
const captureThrownError = async (valueOrFactory) => {
try {
await (typeof valueOrFactory === 'function' ? valueOrFactory() : valueOrFactory);
} catch (error) {
return error;
}
return expect.unreachable('Expected call to throw');
};
test.sequential('supports creating declaration files', async () => {
const bundle = await rollup({
input: 'fixtures/basic/main.ts',
plugins: [
typescript({
tsconfig: 'fixtures/basic/tsconfig.json',
outDir: 'fixtures/basic/dist',
declaration: true
})
],
onwarn
});
const output = await getCode(
bundle,
{
format: 'es',
dir: 'fixtures/basic/dist'
},
true
);
const declaration = output[1].source;
expect(output.map((out) => out.fileName)).toEqual(['main.js', 'main.d.ts']);
expect(declaration.includes('declare const answer = 42;'), declaration).toBe(true);
});
test.sequential('supports creating declaration files in subfolder', async () => {
const bundle = await rollup({
input: 'fixtures/basic/main.ts',
plugins: [
typescript({
tsconfig: 'fixtures/basic/tsconfig.json',
outDir: 'fixtures/basic/dist/types',
declaration: true,
declarationMap: true
})
],
onwarn
});
const output = await getCode(
bundle,
{
format: 'es',
dir: 'fixtures/basic/dist'
},
true
);
const declaration = output[2].source;
expect(output.map((out) => out.fileName)).toEqual([
'main.js',
'types/main.d.ts.map',
'types/main.d.ts'
]);
expect(declaration.includes('declare const answer = 42;'), declaration).toBe(true);
expect(declaration.includes('//# sourceMappingURL=main.d.ts.map'), declaration).toBe(true);
});
test.sequential('supports creating declarations with non-default rootDir', async () => {
const bundle = await rollup({
input: 'fixtures/declaration-root-dir/src/main.ts',
plugins: [
typescript({
tsconfig: 'fixtures/declaration-root-dir/tsconfig.json'
})
],
onwarn
});
const output = await getCode(
bundle,
{
format: 'es',
dir: 'fixtures/declaration-root-dir/lib'
},
true
);
expect(output.map((out) => out.fileName)).toEqual(['main.js', 'main.d.ts']);
});
test.sequential('supports creating declaration files for interface only source file', async () => {
const bundle = await rollup({
input: 'fixtures/export-interface-only/main.ts',
plugins: [
typescript({
tsconfig: 'fixtures/export-interface-only/tsconfig.json',
declarationDir: 'fixtures/export-interface-only/dist/types',
declaration: true,
declarationMap: true
})
],
onwarn
});
const output = await getCode(
bundle,
{
format: 'es',
dir: 'fixtures/export-interface-only/dist'
},
true
);
const declaration = output[2].source;
expect(output.map((out) => out.fileName)).toEqual([
'main.js',
'types/interface.d.ts.map',
'types/interface.d.ts',
'types/main.d.ts.map',
'types/main.d.ts'
]);
expect(declaration.includes('export interface ITest'), declaration).toBe(true);
expect(declaration.includes('//# sourceMappingURL=interface.d.ts.map'), declaration).toBe(true);
});
test.sequential(
'supports creating declaration files for type-only source files that are implicitly included',
async () => {
const bundle = await rollup({
input: 'fixtures/implicitly-included-type-only-file/main.ts',
plugins: [
typescript({
tsconfig: 'fixtures/implicitly-included-type-only-file/tsconfig.json',
declarationDir: 'fixtures/implicitly-included-type-only-file/dist/types',
declaration: true
}),
onwarn
]
});
const output = await getCode(
bundle,
{
format: 'es',
dir: 'fixtures/implicitly-included-type-only-file/dist'
},
true
);
const declaration = output[1].source;
expect(output.map((out) => out.fileName)).toEqual(
// 'types/should-not-be-emitted-types.d.ts' should not be emitted because 'main.ts' does not import/export from it.
['main.js', 'types/should-be-emitted-types.d.ts', 'types/main.d.ts']
);
expect(declaration.includes('export type MyNumber = number;'), declaration).toBe(true);
}
);
test.sequential('supports creating declaration files in declarationDir', async () => {
const bundle = await rollup({
input: 'fixtures/basic/main.ts',
plugins: [
typescript({
tsconfig: 'fixtures/basic/tsconfig.json',
declarationDir: 'fixtures/basic/dist/types',
declaration: true
})
],
onwarn
});
const output = await getCode(
bundle,
{
format: 'es',
dir: 'fixtures/basic/dist'
},
true
);
const declaration = output[1].source;
expect(output.map((out) => out.fileName)).toEqual(['main.js', 'types/main.d.ts']);
expect(declaration.includes('declare const answer = 42;'), declaration).toBe(true);
});
async function ensureOutDirWhenCreatingDeclarationFiles(compilerOptionName) {
const bundle = await rollup({
input: 'fixtures/basic/main.ts',
plugins: [
typescript({
tsconfig: 'fixtures/basic/tsconfig.json',
[compilerOptionName]: true
})
],
onwarn
});
const caughtError = await captureThrownError(() =>
getCode(
bundle,
{
format: 'es',
dir: 'fixtures/basic/dist'
},
true
)
);
expect(
caughtError.message.includes(
`'outDir' or 'declarationDir' must be specified to generate declaration files`
),
`Unexpected error message: ${caughtError.message}`
).toBe(true);
}
test.sequential('ensures outDir is set when creating declaration files (declaration)', async () => {
await ensureOutDirWhenCreatingDeclarationFiles('declaration');
});
test.sequential('ensures outDir is set when creating declaration files (composite)', async () => {
await ensureOutDirWhenCreatingDeclarationFiles('composite');
});