Skip to content

Commit 4dcb5c4

Browse files
authored
feat: updated bundling & ts (module augmentation); lerna monorepo (#57)
- set up [lernajs](https://github.com/lerna/lerna) monorepo - replaced `awesome-typescript-loader` with `ts-loader` - added `paths` to `ts-config.js` allowing IDEs to resolve paths to proper packages/* dir. - set proper `composite` setting in `packages/react-component-catalog` - updated `corejs` to `3.6` in example app (see `babel.config.js`) - updated example app (tsconfig, module augmentation of `CatalogComponents`) - update generics in `react-catalog-component` in packages - ensured lerna works with standard-version and commitizen
1 parent b6019ce commit 4dcb5c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+6091
-7493
lines changed

.eslintignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
src/**/*.d.ts
1+
packages/**/*.d.ts
2+
packages/**/dist
3+
packages/**/es
4+
packages/**/esm
5+
packages/**/lib

.eslintrc.js

+9-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const path = require('path')
33
module.exports = {
44
extends: [
55
'eslint-config-ns',
6-
// add typescript specific linting rules and add prettier typescript support
76
'plugin:@typescript-eslint/recommended',
87
'prettier/@typescript-eslint',
98
],
@@ -13,19 +12,8 @@ module.exports = {
1312
__DEV__: true,
1413
},
1514
rules: {
15+
// universal rules
1616
'import/extensions': 0,
17-
'import/order': [
18-
'error',
19-
{
20-
pathGroups: [
21-
{
22-
pattern: 'react-component-catalog',
23-
group: 'external',
24-
position: 'after',
25-
},
26-
],
27-
},
28-
],
2917
'no-underscore-dangle': 0,
3018
'sort-keys': 0,
3119

@@ -65,16 +53,17 @@ module.exports = {
6553
'@typescript-eslint/no-explicit-any': 0,
6654
},
6755
},
56+
{
57+
files: ['webpack.*.js'],
58+
rules: {
59+
'import/no-extraneous-dependencies': 0,
60+
'no-console': 0,
61+
'@typescript-eslint/no-var-requires': 0,
62+
},
63+
},
6864
],
69-
// required in the example-app
7065
settings: {
7166
'import/resolver': {
72-
alias: {
73-
map: [
74-
['react-component-catalog', path.resolve(__dirname, 'dist')],
75-
['Base', path.resolve(__dirname, 'example/client/base')],
76-
],
77-
},
7867
node: {
7968
extensions: ['.js', '.jsx', '.ts', '.tsx', '.d.ts'],
8069
},

.github/PULL_REQUEST_TEMPLATE.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--
2-
Please submit all PRs to the `master` branch unless they are specific to current
3-
release.
2+
Please submit all PRs to the `master` branch unless they are specific to the
3+
current current release.
44
-->
55

66
# Issue
@@ -21,10 +21,8 @@ Please explain what's new after this PR.
2121
## How to test
2222

2323
<!--
24-
2524
- Is this testable with Jest?
2625
- Does this need an update to the documentation?
2726
2827
If your answer is yes to any of these, please make sure to include it in your PR.
29-
3028
-->

.gitignore

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ coverage
88
# Development
99
node_modules
1010
tmp
11+
*.log
1112

1213
# Linting
1314
.eslintcache
@@ -20,6 +21,11 @@ es
2021
esm
2122
lib
2223
*.tgz
24+
*.tsbuildinfo
2325

24-
# typescript declaration files generated by tsc in src
25-
src/**/*.d.ts
26+
# LICENCE, CHANGELOG.md and README.md in the packages/react-component-catalog
27+
# are ignored, because they are copied into the folder, when the package is
28+
# released.
29+
packages/react-component-catalog/LICENCE
30+
packages/react-component-catalog/CHANGELOG.md
31+
packages/react-component-catalog/README.md

.npmignore

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
.vscode
44

55
# tests and example files
6-
coverage
7-
example
8-
__test__
9-
__tests__
10-
*.test.js
6+
packages/example
7+
packages/**/coverage
8+
packages/**/__test__
9+
packages/**/__tests__
10+
packages/**/*.test.js
11+
packages/**/*.test.ts
12+
packages/**/*.test.tsx
1113

1214
# logs and development
1315
*.log

.prettierrc

-20
This file was deleted.

LICENCE

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
same "printed page" as the copyright notice for easier
188188
identification within third-party archives.
189189

190-
Copyright 2019 Stefan Natter
190+
Copyright 2020 Stefan Natter
191191

192192
Licensed under the Apache License, Version 2.0 (the "License");
193193
you may not use this file except in compliance with the License.

README.md

+39-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[![Dependencies](https://img.shields.io/david/natterstefan/react-component-catalog.svg)](https://github.com/natterstefan/react-component-catalog/blob/master/package.json)
88
[![Known Vulnerabilities](https://snyk.io/test/github/natterstefan/react-component-catalog/badge.svg)](https://snyk.io/test/github/natterstefan/react-component-catalog)
99
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
10+
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lerna.js.org/)
1011

1112
[React-Component-Catalog](https://github.com/natterstefan/react-component-catalog)
1213
is a library for individually registering, retrieving, and rendering React
@@ -111,6 +112,40 @@ const App = () => {
111112
- import { Catalog } from 'react-catalog-component'
112113
```
113114

115+
### `CatalogComponent` and Module Augmentation
116+
117+
The `CatalogComponents` interface can be augmented to add more typing support.
118+
119+
```tsx
120+
// react-component-catalog.d.ts
121+
declare module 'react-component-catalog' {
122+
export interface CatalogComponents {
123+
Title: React.FunctionComponent<{}>
124+
}
125+
}
126+
```
127+
128+
Whenever you use the `CatalogComponent` now you can do the following to get full
129+
typing support (_opt-in feature_). When you do not provide the interface, any
130+
`string`, `string[]` or `Record<string, any>` value for `component` is allowed.
131+
132+
```tsx
133+
const App = () => (
134+
<CatalogComponent<CatalogComponents> component="Title">
135+
Hello World
136+
</CatalogComponent>
137+
)
138+
139+
// this works too, but `component` has no typing support
140+
const App = () => (
141+
<CatalogComponent component="Title">Hello Base</CatalogComponent>
142+
)
143+
```
144+
145+
_Attention:_ it is recommended to use `CatalogComponents` only when it was
146+
augmented. Because it represents an empty interface and without adding your own
147+
custom properties it will [match everything](https://stackoverflow.com/a/58512513/1238150).
148+
114149
## Basic Usage
115150

116151
### Create a Catalog
@@ -322,27 +357,16 @@ yarn build
322357
```
323358

324359
```sh
325-
# -- test the package in a dedicated example setup --
326-
# prepare the example
327-
cd example
328-
rm -rf node_modules # this needs to be optimised by eg. using lernajs
329-
yarn
330-
360+
# -- test the package in an example app --
331361
# run the example in watch-mode
332362
yarn watch
333363

334-
# or if you want to run them individually
335-
yarn watch-client
336-
yarn watch-server
337-
338364
# or run the example in production mode
365+
cd packages/example
339366
yarn build
340367
yarn start
341368
```
342369

343-
Then open the [example](./example) folder and follow the setup instructions.
344-
Afterwards, you can see the package in action.
345-
346370
## How to release and publish the package
347371

348372
This package uses [standard-version](https://github.com/conventional-changelog/standard-version)
@@ -367,6 +391,8 @@ When you're ready to release, execute the following commands in the given order:
367391
(standard-version alternative, with extended CI support)
368392
- [commitlint](https://github.com/conventional-changelog/commitlint)
369393
- [npm-dedupe when eg. multiple @types/\* versions are installed](https://docs.npmjs.com/cli/dedupe.html)
394+
- [React Type Reference](https://flow.org/en/docs/react/types/)
395+
- [Generics while using React.forwardRef](https://stackoverflow.com/a/58473012/1238150)
370396

371397
## Credits
372398

example/.env

-2
This file was deleted.

example/.gitignore

-1
This file was deleted.

example/client/base/components/app/index.tsx

-17
This file was deleted.

example/client/base/components/title/index.tsx

-7
This file was deleted.

example/client/base/index.tsx

-6
This file was deleted.

example/client/client1/components/app/index.tsx

-37
This file was deleted.

example/package.json

-61
This file was deleted.

0 commit comments

Comments
 (0)