Skip to content

Commit 6766d6b

Browse files
committed
init
0 parents  commit 6766d6b

27 files changed

+50841
-0
lines changed

.github/workflows/main.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
on: [push]
3+
jobs:
4+
build:
5+
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}
6+
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
node: ['10.x', '12.x', '14.x']
11+
os: [ubuntu-latest, windows-latest, macOS-latest]
12+
13+
steps:
14+
- name: Checkout repo
15+
uses: actions/checkout@v2
16+
17+
- name: Use Node ${{ matrix.node }}
18+
uses: actions/setup-node@v1
19+
with:
20+
node-version: ${{ matrix.node }}
21+
22+
- name: Install deps and build (with cache)
23+
uses: bahmutov/npm-install@v1
24+
25+
- name: Lint
26+
run: yarn lint
27+
28+
- name: Test
29+
run: yarn test --ci --coverage --maxWorkers=2
30+
31+
- name: Build
32+
run: yarn build

.github/workflows/size.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: size
2+
on: [pull_request]
3+
jobs:
4+
size:
5+
runs-on: ubuntu-latest
6+
env:
7+
CI_JOB_NUMBER: 1
8+
steps:
9+
- uses: actions/checkout@v1
10+
- uses: andresz1/size-limit-action@v1
11+
with:
12+
github_token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.log
2+
.DS_Store
3+
node_modules
4+
.cache
5+
dist

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 baixiaojian
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# TSDX React User Guide
2+
3+
Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it.
4+
5+
> This TSDX setup is meant for developing React component libraries (not apps!) that can be published to NPM. If you’re looking to build a React-based app, you should use `create-react-app`, `razzle`, `nextjs`, `gatsby`, or `react-static`.
6+
7+
> If you’re new to TypeScript and React, checkout [this handy cheatsheet](https://github.com/sw-yx/react-typescript-cheatsheet/)
8+
9+
## Commands
10+
11+
TSDX scaffolds your new library inside `/src`, and also sets up a [Parcel-based](https://parceljs.org) playground for it inside `/example`.
12+
13+
The recommended workflow is to run TSDX in one terminal:
14+
15+
```bash
16+
npm start # or yarn start
17+
```
18+
19+
This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`.
20+
21+
Then run the example inside another:
22+
23+
```bash
24+
cd example
25+
npm i # or yarn to install dependencies
26+
npm start # or yarn start
27+
```
28+
29+
The default example imports and live reloads whatever is in `/dist`, so if you are seeing an out of date component, make sure TSDX is running in watch mode like we recommend above. **No symlinking required**, we use [Parcel's aliasing](https://parceljs.org/module_resolution.html#aliases).
30+
31+
To do a one-off build, use `npm run build` or `yarn build`.
32+
33+
To run tests, use `npm test` or `yarn test`.
34+
35+
## Configuration
36+
37+
Code quality is set up for you with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly.
38+
39+
### Jest
40+
41+
Jest tests are set up to run with `npm test` or `yarn test`.
42+
43+
### Bundle analysis
44+
45+
Calculates the real cost of your library using [size-limit](https://github.com/ai/size-limit) with `npm run size` and visulize it with `npm run analyze`.
46+
47+
#### Setup Files
48+
49+
This is the folder structure we set up for you:
50+
51+
```txt
52+
/example
53+
index.html
54+
index.tsx # test your component here in a demo app
55+
package.json
56+
tsconfig.json
57+
/src
58+
index.tsx # EDIT THIS
59+
/test
60+
blah.test.tsx # EDIT THIS
61+
.gitignore
62+
package.json
63+
README.md # EDIT THIS
64+
tsconfig.json
65+
```
66+
67+
#### React Testing Library
68+
69+
We do not set up `react-testing-library` for you yet, we welcome contributions and documentation on this.
70+
71+
### Rollup
72+
73+
TSDX uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details.
74+
75+
### TypeScript
76+
77+
`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs.
78+
79+
## Continuous Integration
80+
81+
### GitHub Actions
82+
83+
Two actions are added by default:
84+
85+
- `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
86+
- `size` which comments cost comparison of your library on every pull request using [`size-limit`](https://github.com/ai/size-limit)
87+
88+
## Optimizations
89+
90+
Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know that you can take advantage of development-only optimizations:
91+
92+
```js
93+
// ./types/index.d.ts
94+
declare var __DEV__: boolean;
95+
96+
// inside your code...
97+
if (__DEV__) {
98+
console.log('foo');
99+
}
100+
```
101+
102+
You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions.
103+
104+
## Module Formats
105+
106+
CJS, ESModules, and UMD module formats are supported.
107+
108+
The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found.
109+
110+
## Deploying the Example Playground
111+
112+
The Playground is just a simple [Parcel](https://parceljs.org) app, you can deploy it anywhere you would normally deploy that. Here are some guidelines for **manually** deploying with the Netlify CLI (`npm i -g netlify-cli`):
113+
114+
```bash
115+
cd example # if not already in the example folder
116+
npm run build # builds to dist
117+
netlify deploy # deploy the dist folder
118+
```
119+
120+
Alternatively, if you already have a git repo connected, you can set up continuous deployment with Netlify:
121+
122+
```bash
123+
netlify init
124+
# build command: yarn build && cd example && yarn && yarn build
125+
# directory to deploy: example/dist
126+
# pick yes for netlify.toml
127+
```
128+
129+
## Named Exports
130+
131+
Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library.
132+
133+
## Including Styles
134+
135+
There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like.
136+
137+
For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so that it can be imported separately by your users and run through their bundler's loader.
138+
139+
## Publishing to NPM
140+
141+
We recommend using [np](https://github.com/sindresorhus/np).
142+
143+
## Usage with Lerna
144+
145+
When creating a new package with TSDX within a project set up with Lerna, you might encounter a `Cannot resolve dependency` error when trying to run the `example` project. To fix that you will need to make changes to the `package.json` file _inside the `example` directory_.
146+
147+
The problem is that due to the nature of how dependencies are installed in Lerna projects, the aliases in the example project's `package.json` might not point to the right place, as those dependencies might have been installed in the root of your Lerna project.
148+
149+
Change the `alias` to point to where those packages are actually installed. This depends on the directory structure of your Lerna project, so the actual path might be different from the diff below.
150+
151+
```diff
152+
"alias": {
153+
- "react": "../node_modules/react",
154+
- "react-dom": "../node_modules/react-dom"
155+
+ "react": "../../../node_modules/react",
156+
+ "react-dom": "../../../node_modules/react-dom"
157+
},
158+
```
159+
160+
An alternative to fixing this problem would be to remove aliases altogether and define the dependencies referenced as aliases as dev dependencies instead. [However, that might cause other problems.](https://github.com/palmerhq/tsdx/issues/64)

example/.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.cache
3+
dist

example/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<title>Playground</title>
8+
</head>
9+
10+
<body>
11+
<div id="root"></div>
12+
<script src="./index.tsx"></script>
13+
</body>
14+
</html>

example/index.tsx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import 'react-app-polyfill/ie11';
2+
import * as React from 'react';
3+
import * as ReactDOM from 'react-dom';
4+
import JsonEditor from '../.';
5+
// import './reset.css';
6+
const App = () => {
7+
const [editObject, setEditObject] = React.useState<any>({
8+
field: 'assignee',
9+
list: [1, 2, 3],
10+
from: {
11+
name: 'zhangsan',
12+
type: 'crazy person',
13+
},
14+
tmpFromAccountId: null,
15+
});
16+
17+
return (
18+
<div style={{ padding: '10px' }}>
19+
<div style={{ display: 'flex', justifyContent: 'center' }}>
20+
<div
21+
style={{
22+
width: '550px',
23+
border: '1px solid black',
24+
padding: '10px',
25+
}}
26+
>
27+
<JsonEditor
28+
data={editObject}
29+
onChange={data => {
30+
setEditObject(data);
31+
}}
32+
/>
33+
</div>
34+
<div
35+
style={{
36+
width: '600px',
37+
border: '1px solid black',
38+
padding: '10px',
39+
}}
40+
>
41+
<pre>{JSON.stringify(editObject, null, 2)}</pre>
42+
</div>
43+
</div>
44+
</div>
45+
);
46+
};
47+
48+
ReactDOM.render(<App />, document.getElementById('root'));
49+
// const root = ReactDOM.createRoot(
50+
// document.getElementById('root') as HTMLElement
51+
// );
52+
// root.render(
53+
// <React.StrictMode>
54+
// <App />
55+
// </React.StrictMode>
56+
// );

0 commit comments

Comments
 (0)