放弃 TypeScript. 不适用react
This commit is contained in:
parent
2a6595c9b1
commit
f563c46e19
23
.gitignore
vendored
23
.gitignore
vendored
|
@ -1,26 +1,15 @@
|
|||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
node_modules
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# my
|
||||
screenlog.*
|
||||
.env
|
||||
npm-debug.log
|
||||
|
|
76
README.md
76
README.md
|
@ -1,4 +1,34 @@
|
|||
# Getting Started with Create React App
|
||||
# Use antd in create-react-app ✨
|
||||
|
||||
[Create React App](https://facebook.github.io/create-react-app/) + [Ant Design](https://ant.design).
|
||||
|
||||
## Step by Step Documentation
|
||||
|
||||
- 🇺🇸 English: https://ant.design/docs/react/use-with-create-react-app
|
||||
- 🇨🇳 中文:https://ant.design/docs/react/use-with-create-react-app-cn
|
||||
|
||||
## Preview
|
||||
|
||||
```bash
|
||||
$ npm install
|
||||
$ npm start
|
||||
```
|
||||
|
||||
or:
|
||||
|
||||
```bash
|
||||
$ yarn
|
||||
$ yarn start
|
||||
```
|
||||
|
||||
## See more
|
||||
|
||||
- [antd](http://github.com/ant-design/ant-design/)
|
||||
- [create-react-app](https://github.com/facebookincubator/create-react-app)
|
||||
- [craco](https://github.com/gsoft-inc/craco)
|
||||
- [craco-less](https://github.com/DocSpring/craco-less)
|
||||
|
||||
---
|
||||
|
||||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
||||
|
||||
|
@ -6,36 +36,36 @@ This project was bootstrapped with [Create React App](https://github.com/faceboo
|
|||
|
||||
In the project directory, you can run:
|
||||
|
||||
### `yarn start`
|
||||
### `npm start`
|
||||
|
||||
Runs the app in the development mode.\
|
||||
Runs the app in the development mode.<br>
|
||||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
|
||||
|
||||
The page will reload if you make edits.\
|
||||
The page will reload if you make edits.<br>
|
||||
You will also see any lint errors in the console.
|
||||
|
||||
### `yarn test`
|
||||
### `npm test`
|
||||
|
||||
Launches the test runner in the interactive watch mode.\
|
||||
Launches the test runner in the interactive watch mode.<br>
|
||||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
||||
|
||||
### `yarn build`
|
||||
### `npm run build`
|
||||
|
||||
Builds the app for production to the `build` folder.\
|
||||
Builds the app for production to the `build` folder.<br>
|
||||
It correctly bundles React in production mode and optimizes the build for the best performance.
|
||||
|
||||
The build is minified and the filenames include the hashes.\
|
||||
The build is minified and the filenames include the hashes.<br>
|
||||
Your app is ready to be deployed!
|
||||
|
||||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
||||
|
||||
### `yarn eject`
|
||||
### `npm run eject`
|
||||
|
||||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
|
||||
|
||||
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
||||
|
||||
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
|
||||
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
|
||||
|
||||
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
|
||||
|
||||
|
@ -44,3 +74,27 @@ You don’t have to ever use `eject`. The curated feature set is suitable for sm
|
|||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
||||
|
||||
To learn React, check out the [React documentation](https://reactjs.org/).
|
||||
|
||||
### Code Splitting
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
|
||||
|
||||
### Analyzing the Bundle Size
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
|
||||
|
||||
### Making a Progressive Web App
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
|
||||
|
||||
### Advanced Configuration
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
|
||||
|
||||
### Deployment
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
|
||||
|
||||
### `npm run build` fails to minify
|
||||
|
||||
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
|
||||
|
|
17
craco.config.js
Normal file
17
craco.config.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
const CracoLessPlugin = require('craco-less');
|
||||
|
||||
module.exports = {
|
||||
plugins: [
|
||||
{
|
||||
plugin: CracoLessPlugin,
|
||||
options: {
|
||||
lessLoaderOptions: {
|
||||
lessOptions: {
|
||||
modifyVars: { '@primary-color': '#1DA57A' },
|
||||
javascriptEnabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
16776
package-lock.json
generated
16776
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
40
package.json
40
package.json
|
@ -1,37 +1,25 @@
|
|||
{
|
||||
"name": "datagod",
|
||||
"name": "create-react-app-antd",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@testing-library/jest-dom": "^5.11.4",
|
||||
"@testing-library/react": "^11.1.0",
|
||||
"@testing-library/user-event": "^12.1.10",
|
||||
"@types/jest": "^26.0.15",
|
||||
"@types/node": "^12.0.0",
|
||||
"@types/react": "^16.9.53",
|
||||
"@types/react-dom": "^16.9.8",
|
||||
"@types/react-router": "^5.1.8",
|
||||
"@types/react-router-dom": "^5.1.6",
|
||||
"antd": "^4.8.0",
|
||||
"react": "^17.0.1",
|
||||
"react-dom": "^17.0.1",
|
||||
"react-router": "^5.2.0",
|
||||
"react-router-dom": "^5.2.0",
|
||||
"react-scripts": "4.0.0",
|
||||
"typescript": "^4.0.3",
|
||||
"web-vitals": "^0.2.4"
|
||||
"@craco/craco": "^5.6.4",
|
||||
"@testing-library/jest-dom": "^5.9.0",
|
||||
"@testing-library/react": "^11.0.0",
|
||||
"@testing-library/user-event": "^11.1.0",
|
||||
"antd": "^4.5.2",
|
||||
"craco-less": "^1.17.0",
|
||||
"react": "^16.13.1",
|
||||
"react-dom": "^16.13.1",
|
||||
"react-scripts": "3.4.3"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start",
|
||||
"build": "react-scripts build",
|
||||
"test": "react-scripts test",
|
||||
"eject": "react-scripts eject"
|
||||
"start": "craco start",
|
||||
"build": "craco build",
|
||||
"test": "craco test"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": [
|
||||
"react-app",
|
||||
"react-app/jest"
|
||||
]
|
||||
"extends": "react-app"
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.1 KiB |
18
screenlog.0
Normal file
18
screenlog.0
Normal file
|
@ -0,0 +1,18 @@
|
|||
[2K[1G[1myarn run v1.22.4[22m
|
||||
[2K[1G[2m$ craco start[22m
|
||||
[34mℹ[39m [90m「wds」[39m: Project is running at http://192.168.17.208/
|
||||
[34mℹ[39m [90m「wds」[39m: webpack output is served from
|
||||
[34mℹ[39m [90m「wds」[39m: Content not from webpack is served from /home/eson/workspace/datagod/public
|
||||
[34mℹ[39m [90m「wds」[39m: 404s will fallback to /
|
||||
[2J[3J[H[36mStarting the development server...[39m
|
||||
[36m[39m
|
||||
[2J[3J[H[32mCompiled successfully![39m
|
||||
|
||||
You can now view [1mcreate-react-app-antd[22m in the browser.
|
||||
|
||||
[1mLocal:[22m http://localhost:[1m4440[22m
|
||||
[1mOn Your Network:[22m http://192.168.17.208:[1m4440[22m
|
||||
|
||||
Note that the development build is not optimized.
|
||||
To create a production build, use [36myarn build[39m.
|
||||
|
78
src/App.js
Normal file
78
src/App.js
Normal file
|
@ -0,0 +1,78 @@
|
|||
import React from 'react';
|
||||
import {
|
||||
Form,
|
||||
Select,
|
||||
InputNumber,
|
||||
DatePicker,
|
||||
Switch,
|
||||
Slider,
|
||||
Button,
|
||||
Rate,
|
||||
Typography,
|
||||
Space,
|
||||
Divider,
|
||||
} from 'antd';
|
||||
import './App.less';
|
||||
|
||||
const { Option } = Select;
|
||||
const { Title } = Typography;
|
||||
|
||||
const App = () => (
|
||||
<>
|
||||
<section style={{ textAlign: 'center', marginTop: 48, marginBottom: 40 }}>
|
||||
<Space align="start">
|
||||
<img
|
||||
style={{width: 40, height: 40 }}
|
||||
src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg"
|
||||
alt="Ant Design"
|
||||
/>
|
||||
<Title level={2} style={{ marginBottom: 0 }}>
|
||||
Ant Design
|
||||
</Title>
|
||||
</Space>
|
||||
</section>
|
||||
<Divider style={{ marginBottom: 60 }}>Form</Divider>
|
||||
<Form labelCol={{ span: 8 }} wrapperCol={{ span: 8 }}>
|
||||
<Form.Item label="数字输入框">
|
||||
<InputNumber min={1} max={10} defaultValue={3} />
|
||||
<span className="ant-form-text"> 台机器</span>
|
||||
<a href="https://ant.design">链接文字</a>
|
||||
</Form.Item>
|
||||
<Form.Item label="开关">
|
||||
<Switch defaultChecked />
|
||||
</Form.Item>
|
||||
<Form.Item label="滑动输入条">
|
||||
<Slider defaultValue={70} />
|
||||
</Form.Item>
|
||||
<Form.Item label="选择器">
|
||||
<Select defaultValue="lucy" style={{ width: 192 }}>
|
||||
<Option value="jack">jack</Option>
|
||||
<Option value="lucy">lucy</Option>
|
||||
<Option value="disabled" disabled>disabled</Option>
|
||||
<Option value="yiminghe">yiminghe</Option>
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<Form.Item label="日期选择框">
|
||||
<DatePicker />
|
||||
</Form.Item>
|
||||
<Form.Item label="日期范围选择框">
|
||||
<DatePicker.RangePicker />
|
||||
</Form.Item>
|
||||
<Form.Item label="评分">
|
||||
<Rate defaultValue={5} />
|
||||
</Form.Item>
|
||||
<Form.Item wrapperCol={{ span: 8, offset: 8 }}>
|
||||
<Space>
|
||||
<Button type="primary" htmlType="submit">
|
||||
Submit
|
||||
</Button>
|
||||
<Button>
|
||||
Cancel
|
||||
</Button>
|
||||
</Space>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</>
|
||||
);
|
||||
|
||||
export default App;
|
|
@ -1,4 +1,4 @@
|
|||
@import '~antd/dist/antd.css';
|
||||
@import '~antd/dist/antd.less';
|
||||
|
||||
.App {
|
||||
text-align: center;
|
|
@ -1,9 +1,9 @@
|
|||
import React from 'react';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { render } from '@testing-library/react';
|
||||
import App from './App';
|
||||
|
||||
test('renders learn react link', () => {
|
||||
render(<App />);
|
||||
const linkElement = screen.getByText(/learn react/i);
|
||||
const { getByText } = render(<App />);
|
||||
const linkElement = getByText(/Ant Design/i);
|
||||
expect(linkElement).toBeInTheDocument();
|
||||
});
|
14
src/App.tsx
14
src/App.tsx
|
@ -1,14 +0,0 @@
|
|||
import React from 'react';
|
||||
import './App.css';
|
||||
|
||||
import HomeRouter from './router/router';
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<div className="App">
|
||||
<HomeRouter></HomeRouter>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
15
src/index.js
Normal file
15
src/index.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import './index.css';
|
||||
import App from './App';
|
||||
import * as serviceWorker from './serviceWorker';
|
||||
|
||||
ReactDOM.render(
|
||||
<App />,
|
||||
document.getElementById('root')
|
||||
);
|
||||
|
||||
// If you want your app to work offline and load faster, you can change
|
||||
// unregister() to register() below. Note this comes with some pitfalls.
|
||||
// Learn more about service workers: https://bit.ly/CRA-PWA
|
||||
serviceWorker.unregister();
|
|
@ -1,18 +0,0 @@
|
|||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import './index.css';
|
||||
import App from './App';
|
||||
import reportWebVitals from './reportWebVitals';
|
||||
|
||||
|
||||
ReactDOM.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>,
|
||||
document.getElementById('root')
|
||||
);
|
||||
|
||||
// If you want to start measuring performance in your app, pass a function
|
||||
// to log results (for example: reportWebVitals(console.log))
|
||||
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
|
||||
reportWebVitals();
|
|
@ -1,33 +0,0 @@
|
|||
#components-layout-custom-trigger .trigger {
|
||||
font-size: 18px;
|
||||
line-height: 128px;
|
||||
padding: 0 6px;
|
||||
cursor: pointer;
|
||||
transition: color 0.3s;
|
||||
}
|
||||
|
||||
#components-layout-custom-trigger .trigger:hover {
|
||||
color: #1890ff;
|
||||
}
|
||||
|
||||
#components-layout-custom-trigger .logo {
|
||||
|
||||
background: rgba(255, 255, 255, 0.3);
|
||||
|
||||
}
|
||||
|
||||
.header-layout {
|
||||
padding: 0px;
|
||||
/* height: 36px; */
|
||||
}
|
||||
|
||||
.site-layout {
|
||||
background: rgb(208, 214, 216);
|
||||
}
|
||||
|
||||
.content-layout {
|
||||
/* background: rgb(208, 214, 216); */
|
||||
margin: 0px;
|
||||
padding: 12;
|
||||
min-height: 900px;
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
import React from 'react';
|
||||
import { Button, Layout, Menu } from 'antd';
|
||||
import {
|
||||
MenuUnfoldOutlined,
|
||||
MenuFoldOutlined,
|
||||
UserOutlined,
|
||||
VideoCameraOutlined,
|
||||
UploadOutlined,
|
||||
LeftCircleFilled,
|
||||
} from '@ant-design/icons';
|
||||
import "./home.css";
|
||||
import ContentTabs from './tabs';
|
||||
import { HashRouter, Link, Redirect, Route, Switch} from 'react-router-dom';
|
||||
import { any } from 'prop-types';
|
||||
|
||||
|
||||
const { Header, Sider, Content } = Layout;
|
||||
|
||||
class Home extends React.Component {
|
||||
state = {
|
||||
collapsed: false,
|
||||
};
|
||||
|
||||
toggle = () => {
|
||||
this.setState({
|
||||
collapsed: !this.state.collapsed,
|
||||
});
|
||||
};
|
||||
|
||||
render() {
|
||||
var props:any = this.props;
|
||||
const { match } = props;
|
||||
console.log();
|
||||
return (
|
||||
// var props:any = this.props;
|
||||
|
||||
|
||||
|
||||
<Layout className="site-layout">
|
||||
|
||||
<Header className="header-layout" style={{ padding: 0 }}>
|
||||
|
||||
<Menu theme="dark" mode="horizontal" defaultSelectedKeys={['1']}>
|
||||
<Menu.Item key="1" icon={<UserOutlined />} >
|
||||
<HashRouter>
|
||||
<Link to="/tab1">tab 1</Link>
|
||||
</HashRouter>
|
||||
</Menu.Item>
|
||||
<Menu.Item key="2" icon={<VideoCameraOutlined />}>
|
||||
<HashRouter>
|
||||
<Link to="/tab2">tab 2</Link>
|
||||
</HashRouter>
|
||||
</Menu.Item>
|
||||
<Menu.Item key="3" icon={<UploadOutlined />}>
|
||||
<HashRouter>
|
||||
<Link to="/info">nav 3</Link>
|
||||
</HashRouter>
|
||||
</Menu.Item>
|
||||
</Menu>
|
||||
</Header>
|
||||
|
||||
<Content
|
||||
className="content-layout"
|
||||
>
|
||||
<HashRouter>
|
||||
<Switch>
|
||||
<Route path={`/:id`} component={ContentTabs}></Route>
|
||||
<Route path="/" >
|
||||
<Redirect to="/tab1">...{console.log("tab1 redirect")}</Redirect>
|
||||
</Route>
|
||||
|
||||
{/* <Route path={`/info/:id`} ></Route> */}
|
||||
</Switch>
|
||||
</HashRouter>
|
||||
|
||||
</Content>
|
||||
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default Home;
|
|
@ -1,37 +0,0 @@
|
|||
import { AndroidOutlined, AppleOutlined } from '@ant-design/icons';
|
||||
import { Tabs } from 'antd';
|
||||
import React from 'react';
|
||||
|
||||
const { TabPane } = Tabs;
|
||||
|
||||
class ContentTabs extends React.Component {
|
||||
|
||||
|
||||
|
||||
render() {
|
||||
var props:any = this.props;
|
||||
const {location, match} = props;
|
||||
const {state} = location;
|
||||
console.log(props);
|
||||
var key = match.params.id;
|
||||
console.log(key);
|
||||
|
||||
return (
|
||||
<Tabs defaultActiveKey={key} >
|
||||
<TabPane
|
||||
tab={<span><AppleOutlined />Tab 1</span>}
|
||||
key="tab1"
|
||||
>
|
||||
Tab 1
|
||||
</TabPane>
|
||||
<TabPane tab={<span><AndroidOutlined />Tab 2</span>}
|
||||
key="tab2"
|
||||
>
|
||||
Tab 2
|
||||
</TabPane>
|
||||
</Tabs>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default ContentTabs;
|
1
src/react-app-env.d.ts
vendored
1
src/react-app-env.d.ts
vendored
|
@ -1 +0,0 @@
|
|||
/// <reference types="react-scripts" />
|
|
@ -1,15 +0,0 @@
|
|||
import { ReportHandler } from 'web-vitals';
|
||||
|
||||
const reportWebVitals = (onPerfEntry?: ReportHandler) => {
|
||||
if (onPerfEntry && onPerfEntry instanceof Function) {
|
||||
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
|
||||
getCLS(onPerfEntry);
|
||||
getFID(onPerfEntry);
|
||||
getFCP(onPerfEntry);
|
||||
getLCP(onPerfEntry);
|
||||
getTTFB(onPerfEntry);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default reportWebVitals;
|
|
@ -1,35 +0,0 @@
|
|||
|
||||
import React from 'react';
|
||||
import { BrowserRouter, Route, Switch, HashRouter, Redirect, Link } from 'react-router-dom';
|
||||
|
||||
|
||||
import Home from '../pages/home';
|
||||
|
||||
class HomeRouter extends React.Component {
|
||||
|
||||
render() {
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<Switch>
|
||||
<Route exact path="/" >
|
||||
|
||||
<Redirect to={{pathname: "/content"}} >
|
||||
...{console.log("redirect")}
|
||||
</Redirect>
|
||||
|
||||
</Route>
|
||||
<Route path="/content" component={Home}>
|
||||
|
||||
</Route>
|
||||
|
||||
|
||||
|
||||
{/* <Route exact path="/home" component={Home} ></Route> */}
|
||||
</Switch>
|
||||
</BrowserRouter>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default HomeRouter;
|
||||
|
141
src/serviceWorker.js
Normal file
141
src/serviceWorker.js
Normal file
|
@ -0,0 +1,141 @@
|
|||
// This optional code is used to register a service worker.
|
||||
// register() is not called by default.
|
||||
|
||||
// This lets the app load faster on subsequent visits in production, and gives
|
||||
// it offline capabilities. However, it also means that developers (and users)
|
||||
// will only see deployed updates on subsequent visits to a page, after all the
|
||||
// existing tabs open on the page have been closed, since previously cached
|
||||
// resources are updated in the background.
|
||||
|
||||
// To learn more about the benefits of this model and instructions on how to
|
||||
// opt-in, read https://bit.ly/CRA-PWA
|
||||
|
||||
const isLocalhost = Boolean(
|
||||
window.location.hostname === 'localhost' ||
|
||||
// [::1] is the IPv6 localhost address.
|
||||
window.location.hostname === '[::1]' ||
|
||||
// 127.0.0.0/8 are considered localhost for IPv4.
|
||||
window.location.hostname.match(
|
||||
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
|
||||
)
|
||||
);
|
||||
|
||||
export function register(config) {
|
||||
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
|
||||
// The URL constructor is available in all browsers that support SW.
|
||||
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
|
||||
if (publicUrl.origin !== window.location.origin) {
|
||||
// Our service worker won't work if PUBLIC_URL is on a different origin
|
||||
// from what our page is served on. This might happen if a CDN is used to
|
||||
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
|
||||
return;
|
||||
}
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
|
||||
|
||||
if (isLocalhost) {
|
||||
// This is running on localhost. Let's check if a service worker still exists or not.
|
||||
checkValidServiceWorker(swUrl, config);
|
||||
|
||||
// Add some additional logging to localhost, pointing developers to the
|
||||
// service worker/PWA documentation.
|
||||
navigator.serviceWorker.ready.then(() => {
|
||||
console.log(
|
||||
'This web app is being served cache-first by a service ' +
|
||||
'worker. To learn more, visit https://bit.ly/CRA-PWA'
|
||||
);
|
||||
});
|
||||
} else {
|
||||
// Is not localhost. Just register service worker
|
||||
registerValidSW(swUrl, config);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function registerValidSW(swUrl, config) {
|
||||
navigator.serviceWorker
|
||||
.register(swUrl)
|
||||
.then(registration => {
|
||||
registration.onupdatefound = () => {
|
||||
const installingWorker = registration.installing;
|
||||
if (installingWorker == null) {
|
||||
return;
|
||||
}
|
||||
installingWorker.onstatechange = () => {
|
||||
if (installingWorker.state === 'installed') {
|
||||
if (navigator.serviceWorker.controller) {
|
||||
// At this point, the updated precached content has been fetched,
|
||||
// but the previous service worker will still serve the older
|
||||
// content until all client tabs are closed.
|
||||
console.log(
|
||||
'New content is available and will be used when all ' +
|
||||
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
|
||||
);
|
||||
|
||||
// Execute callback
|
||||
if (config && config.onUpdate) {
|
||||
config.onUpdate(registration);
|
||||
}
|
||||
} else {
|
||||
// At this point, everything has been precached.
|
||||
// It's the perfect time to display a
|
||||
// "Content is cached for offline use." message.
|
||||
console.log('Content is cached for offline use.');
|
||||
|
||||
// Execute callback
|
||||
if (config && config.onSuccess) {
|
||||
config.onSuccess(registration);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error during service worker registration:', error);
|
||||
});
|
||||
}
|
||||
|
||||
function checkValidServiceWorker(swUrl, config) {
|
||||
// Check if the service worker can be found. If it can't reload the page.
|
||||
fetch(swUrl, {
|
||||
headers: { 'Service-Worker': 'script' },
|
||||
})
|
||||
.then(response => {
|
||||
// Ensure service worker exists, and that we really are getting a JS file.
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (
|
||||
response.status === 404 ||
|
||||
(contentType != null && contentType.indexOf('javascript') === -1)
|
||||
) {
|
||||
// No service worker found. Probably a different app. Reload the page.
|
||||
navigator.serviceWorker.ready.then(registration => {
|
||||
registration.unregister().then(() => {
|
||||
window.location.reload();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// Service worker found. Proceed as normal.
|
||||
registerValidSW(swUrl, config);
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
console.log(
|
||||
'No internet connection found. App is running in offline mode.'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export function unregister() {
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.ready
|
||||
.then(registration => {
|
||||
registration.unregister();
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error.message);
|
||||
});
|
||||
}
|
||||
}
|
19
src/setupTests.js
Normal file
19
src/setupTests.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
// jest-dom adds custom jest matchers for asserting on DOM nodes.
|
||||
// allows you to do things like:
|
||||
// expect(element).toHaveTextContent(/react/i)
|
||||
// learn more: https://github.com/testing-library/jest-dom
|
||||
import '@testing-library/jest-dom/extend-expect';
|
||||
|
||||
Object.defineProperty(window, 'matchMedia', {
|
||||
writable: true,
|
||||
value: jest.fn().mockImplementation(query => ({
|
||||
matches: false,
|
||||
media: query,
|
||||
onchange: null,
|
||||
addListener: jest.fn(), // deprecated
|
||||
removeListener: jest.fn(), // deprecated
|
||||
addEventListener: jest.fn(),
|
||||
removeEventListener: jest.fn(),
|
||||
dispatchEvent: jest.fn(),
|
||||
})),
|
||||
});
|
|
@ -1,5 +0,0 @@
|
|||
// jest-dom adds custom jest matchers for asserting on DOM nodes.
|
||||
// allows you to do things like:
|
||||
// expect(element).toHaveTextContent(/react/i)
|
||||
// learn more: https://github.com/testing-library/jest-dom
|
||||
import '@testing-library/jest-dom';
|
|
@ -1,26 +0,0 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2015",
|
||||
"lib": [
|
||||
"dom",
|
||||
"dom.iterable",
|
||||
"esnext"
|
||||
],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"jsx": "react"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user