Skip to content

Commit 03da353

Browse files
authored
Add typings to packaged components (#979)
1 parent c36c2fe commit 03da353

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

scripts/buildPackages.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,19 @@ const packages = [
2121
];
2222

2323
/* Write custom packages */
24+
function addTyping(typings, component) {
25+
if (_.isEmpty(typings)) {
26+
typings += `{${component}`;
27+
} else {
28+
typings += `, ${component}`;
29+
}
30+
31+
return typings;
32+
}
33+
2434
packages.forEach((package) => {
2535
let content = package.content || '';
36+
let typings = '';
2637

2738
if (package.components || package.styleComponents) {
2839
content += 'module.exports = {\n';
@@ -32,6 +43,8 @@ packages.forEach((package) => {
3243
component
3344
)}').default;`;
3445
content += `},\n`;
46+
47+
typings = addTyping(typings, component);
3548
});
3649

3750
_.forEach(package.styleComponents, (component) => {
@@ -40,11 +53,19 @@ packages.forEach((package) => {
4053
component
4154
)}').default;`;
4255
content += `},\n`;
56+
57+
typings = addTyping(typings, component);
4358
});
4459
content += '};\n';
60+
typings += '}';
61+
typings = `import ${typings} from './generatedTypes';\nexport ${typings};\n`;
4562
}
4663

4764
fs.writeFileSync(package.filename, content);
65+
if (!_.isEmpty(typings)) {
66+
const filename = `${package.filename.substring(0, package.filename.indexOf('.js'))}.d.ts`;
67+
fs.writeFileSync(filename, typings);
68+
}
4869
});
4970

5071
/* Write all components as separate packages */
@@ -58,6 +79,11 @@ fs.readdir(path, (err, files) => {
5879
`${file}.js`,
5980
`module.exports = require('${path}/${file}').default;\n`
6081
);
82+
const componentName = _.upperFirst(file);
83+
fs.writeFileSync(
84+
`${file}.d.ts`,
85+
`import {${componentName}} from './generatedTypes';\nexport default ${componentName};\n`
86+
);
6187
});
6288
}
6389
});

0 commit comments

Comments
 (0)