Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNM] - Feature/add typescript types for sage #1796

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/sage-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"homepage": "https://github.com/Kajabi/sage-lib#readme",
"license": "MIT",
"main": "dist/main.js",
"types": "./dist/types/main.d.ts",
"type": "module",
"directories": {
"lib": "lib"
},
Expand All @@ -33,9 +35,10 @@
"yarn": "1.x"
},
"scripts": {
"build": "build-storybook -o build && webpack --config webpack/webpack.prod.js",
"build": "build-storybook -o build && webpack --config webpack/webpack.prod.js && yarn run generate:types",
"build:watch": "build-storybook -o build && webpack --config webpack/webpack.prod.js --watch",
"heroku-prebuild": "npm install react@16.13.1 react-dom@16.13.1 react-router-dom@5.1.0 --no-package-lock",
"generate:types": "node scripts/generateTypes.js",
"lint": "yarn run eslint \"lib/**/*.{js,jsx}\"",
"storybook": "start-storybook -p 4100 --ci",
"preversion": "yarn install && yarn run build",
Expand Down Expand Up @@ -77,6 +80,7 @@
"npm-run-all": "^4.1.5",
"react-is": "16.13.1",
"react-test-renderer": "16.13.1",
"react-to-typescript-definitions": "^3.1.1",
"sass-loader": "^10.0.2",
"typescript": "4.0.3",
"url-loader": "^4.1.0",
Expand Down
22 changes: 22 additions & 0 deletions packages/sage-react/scripts/generateTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { generateFromFile } from 'react-to-typescript-definitions';
import * as componentLibrary from '../lib/index.js';

const fs = require('fs');

let definitionsContent = '';
const components = Object.keys(componentLibrary);

components.forEach((component) => {
if (typeof componentLibrary[component] === 'function' && componentLibrary[compoennt].filename !== undefined) {
const definitions = generateFromFile(
null,
componentLibrary[component].filename,
{},
'react'
);

definitionsContent = `${definitionsContent}${definitions}`;
}
});

fs.writeFileSync('../dist/sage-lib.d.ts', definitionsContent);
25 changes: 25 additions & 0 deletions packages/sage-react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
// Tells TypeScript to read JS files, as
// normally they are ignored as source files
"allowJs": true,
// Generate d.ts files
"declaration": true,
// This compiler run should
// only output d.ts files
"emitDeclarationOnly": true,
// Types should go into this directory.
// Removing this would place the .d.ts files
// next to the .js files
"outDir": "dist/types",
// go to js file when using IDE functions like
// "Go to Definition" in VSCode
"declarationMap": true
},
// Change this to match your project
"include": ["lib/**/*"],
"exclude": [
"lib/**/*.story.jsx",
"lib/test"
],
}
Loading