1import { compiler, beautify } from 'flowgen';
2
3function flowTypings() {
4 return {
5 name: 'flow-typings',
6 async renderChunk(code, chunk) {
7 if (chunk.fileName.endsWith('d.ts')) {
8 // NOTE: Computed property names will be omitted
9 code = code.replace(/\[Symbol\.\w+\][?()]*:(?:.*);\n?/g, '');
10
11 let flowdef = compiler.compileDefinitionString(code, {
12 jsdoc: false,
13 });
14
15 flowdef = beautify(flowdef);
16 flowdef = flowdef.replace(/import/g, 'import type');
17 flowdef = `// @flow\n\n${flowdef}`;
18
19 this.emitFile({
20 type: 'asset',
21 name: chunk.name,
22 fileName: `${chunk.name}.js.flow`,
23 source: flowdef,
24 });
25 }
26
27 return null;
28 },
29 };
30}
31
32export default flowTypings;