Skip to content

Commit 795e737

Browse files
Merge pull request transitive-bullshit#80 from wuweiweiwu/cra-v2
Use CRA v2
2 parents f84519c + bebfdff commit 795e737

22 files changed

+2090
-968
lines changed

template/default/example/README.md

+803-457
Large diffs are not rendered by default.

template/default/example/package.json

+14-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@
22
"name": "{{name}}-example",
33
"homepage": "https://{{author}}.github.io/{{name}}",
44
"version": "0.0.0",
5-
"license": "MIT",
65
"private": true,
76
"dependencies": {
8-
"prop-types": "^15.6.2",
9-
"react": "^16.4.1",
10-
"react-dom": "^16.4.1",
11-
"react-scripts": "^1.1.4",
7+
"react": "^16.5.2",
8+
"react-dom": "^16.5.2",
9+
"react-scripts": "2.0.4",
1210
"{{name}}": "{{#if yarn}}link:..{{else}}file:..{{/if}}"
1311
},
1412
"scripts": {
1513
"start": "react-scripts start",
1614
"build": "react-scripts build",
17-
"test": "react-scripts test --env=jsdom",
15+
"test": "react-scripts test",
1816
"eject": "react-scripts eject"
19-
}
17+
},
18+
"eslintConfig": {
19+
"extends": "react-app"
20+
},
21+
"browserslist": [
22+
">0.2%",
23+
"not dead",
24+
"not ie <= 11",
25+
"not op_mini all"
26+
]
2027
}
3.78 KB
Binary file not shown.
+24-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
1-
<!doctype html>
1+
<!DOCTYPE html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8">
5+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
56
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
67
<meta name="theme-color" content="#000000">
7-
8+
<!--
9+
manifest.json provides metadata used when your web app is added to the
10+
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
11+
-->
812
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
13+
<!--
14+
Notice the use of %PUBLIC_URL% in the tags above.
15+
It will be replaced with the URL of the `public` folder during the build.
16+
Only files inside the `public` folder can be referenced from the HTML.
917
18+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
19+
work correctly both with client-side routing and a non-root public URL.
20+
Learn how to configure a non-root public URL by running `npm run build`.
21+
-->
1022
<title>{{name}}</title>
1123
</head>
12-
1324
<body>
1425
<noscript>
1526
You need to enable JavaScript to run this app.
1627
</noscript>
17-
1828
<div id="root"></div>
29+
<!--
30+
This HTML file is a template.
31+
If you open it directly in the browser, you will see an empty page.
32+
33+
You can add webfonts, meta tags, or analytics to this file.
34+
The build step will place the bundled scripts into the <body> tag.
35+
36+
To begin the development, run `npm start` or `yarn start`.
37+
To create a production bundle, use `npm run build` or `yarn build`.
38+
-->
1939
</body>
2040
</html>

template/default/example/public/manifest.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
{
22
"short_name": "{{name}}",
33
"name": "{{name}}",
4-
"start_url": "./index.html",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
}
10+
],
11+
"start_url": ".",
512
"display": "standalone",
613
"theme_color": "#000000",
714
"background_color": "#ffffff"

template/default/example/src/App.css

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.App {
2+
text-align: center;
3+
}
4+
5+
.App-logo {
6+
animation: App-logo-spin infinite 20s linear;
7+
height: 40vmin;
8+
}
9+
10+
.App-header {
11+
background-color: #282c34;
12+
min-height: 100vh;
13+
display: flex;
14+
flex-direction: column;
15+
align-items: center;
16+
justify-content: center;
17+
font-size: calc(10px + 2vmin);
18+
color: white;
19+
}
20+
21+
.App-link {
22+
color: #61dafb;
23+
}
24+
25+
@keyframes App-logo-spin {
26+
from {
27+
transform: rotate(0deg);
28+
}
29+
to {
30+
transform: rotate(360deg);
31+
}
32+
}

template/default/example/src/App.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import React, { Component } from 'react'
1+
import React, { Component } from 'react';
2+
import './App.css';
23

3-
import ExampleComponent from '{{name}}'
4+
import ExampleComponent from '{{name}}';
45

5-
export default class App extends Component {
6-
render () {
7-
return (
8-
<div>
9-
<ExampleComponent text='Modern React component module' />
10-
</div>
11-
)
6+
class App extends Component {
7+
render() {
8+
return <ExampleComponent text='Modern React component module' />;
129
}
1310
}
11+
12+
export default App;
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
5+
it('renders without crashing', () => {
6+
const div = document.createElement('div');
7+
ReactDOM.render(<App />, div);
8+
ReactDOM.unmountComponentAtNode(div);
9+
});
+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
body {
22
margin: 0;
33
padding: 0;
4-
font-family: sans-serif;
4+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
5+
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
6+
sans-serif;
7+
-webkit-font-smoothing: antialiased;
8+
-moz-osx-font-smoothing: grayscale;
9+
}
10+
11+
code {
12+
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
13+
monospace;
514
}

template/default/example/src/index.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import React from 'react'
2-
import ReactDOM from 'react-dom'
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import './index.css';
4+
import App from './App';
5+
import * as serviceWorker from './serviceWorker';
36

4-
import './index.css'
5-
import App from './App'
7+
ReactDOM.render(<App />, document.getElementById('root'));
68

7-
ReactDOM.render(<App />, document.getElementById('root'))
9+
// If you want your app to work offline and load faster, you can change
10+
// unregister() to register() below. Note this comes with some pitfalls.
11+
// Learn more about service workers: http://bit.ly/CRA-PWA
12+
serviceWorker.unregister();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// In production, we register a service worker to serve assets from local cache.
2+
3+
// This lets the app load faster on subsequent visits in production, and gives
4+
// it offline capabilities. However, it also means that developers (and users)
5+
// will only see deployed updates on the "N+1" visit to a page, since previously
6+
// cached resources are updated in the background.
7+
8+
// To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
9+
// This link also includes instructions on opting out of this behavior.
10+
11+
const isLocalhost = Boolean(
12+
window.location.hostname === 'localhost' ||
13+
// [::1] is the IPv6 localhost address.
14+
window.location.hostname === '[::1]' ||
15+
// 127.0.0.1/8 is considered localhost for IPv4.
16+
window.location.hostname.match(
17+
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
18+
)
19+
);
20+
21+
export function register(config) {
22+
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
23+
// The URL constructor is available in all browsers that support SW.
24+
const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
25+
if (publicUrl.origin !== window.location.origin) {
26+
// Our service worker won't work if PUBLIC_URL is on a different origin
27+
// from what our page is served on. This might happen if a CDN is used to
28+
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
29+
return;
30+
}
31+
32+
window.addEventListener('load', () => {
33+
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
34+
35+
if (isLocalhost) {
36+
// This is running on localhost. Let's check if a service worker still exists or not.
37+
checkValidServiceWorker(swUrl, config);
38+
39+
// Add some additional logging to localhost, pointing developers to the
40+
// service worker/PWA documentation.
41+
navigator.serviceWorker.ready.then(() => {
42+
console.log(
43+
'This web app is being served cache-first by a service ' +
44+
'worker. To learn more, visit https://goo.gl/SC7cgQ'
45+
);
46+
});
47+
} else {
48+
// Is not local host. Just register service worker
49+
registerValidSW(swUrl, config);
50+
}
51+
});
52+
}
53+
}
54+
55+
function registerValidSW(swUrl, config) {
56+
navigator.serviceWorker
57+
.register(swUrl)
58+
.then(registration => {
59+
registration.onupdatefound = () => {
60+
const installingWorker = registration.installing;
61+
installingWorker.onstatechange = () => {
62+
if (installingWorker.state === 'installed') {
63+
if (navigator.serviceWorker.controller) {
64+
// At this point, the old content will have been purged and
65+
// the fresh content will have been added to the cache.
66+
// It's the perfect time to display a "New content is
67+
// available; please refresh." message in your web app.
68+
console.log('New content is available; please refresh.');
69+
70+
// Execute callback
71+
if (config.onUpdate) {
72+
config.onUpdate(registration);
73+
}
74+
} else {
75+
// At this point, everything has been precached.
76+
// It's the perfect time to display a
77+
// "Content is cached for offline use." message.
78+
console.log('Content is cached for offline use.');
79+
80+
// Execute callback
81+
if (config.onSuccess) {
82+
config.onSuccess(registration);
83+
}
84+
}
85+
}
86+
};
87+
};
88+
})
89+
.catch(error => {
90+
console.error('Error during service worker registration:', error);
91+
});
92+
}
93+
94+
function checkValidServiceWorker(swUrl, config) {
95+
// Check if the service worker can be found. If it can't reload the page.
96+
fetch(swUrl)
97+
.then(response => {
98+
// Ensure service worker exists, and that we really are getting a JS file.
99+
if (
100+
response.status === 404 ||
101+
response.headers.get('content-type').indexOf('javascript') === -1
102+
) {
103+
// No service worker found. Probably a different app. Reload the page.
104+
navigator.serviceWorker.ready.then(registration => {
105+
registration.unregister().then(() => {
106+
window.location.reload();
107+
});
108+
});
109+
} else {
110+
// Service worker found. Proceed as normal.
111+
registerValidSW(swUrl, config);
112+
}
113+
})
114+
.catch(() => {
115+
console.log(
116+
'No internet connection found. App is running in offline mode.'
117+
);
118+
});
119+
}
120+
121+
export function unregister() {
122+
if ('serviceWorker' in navigator) {
123+
navigator.serviceWorker.ready.then(registration => {
124+
registration.unregister();
125+
});
126+
}
127+
}

0 commit comments

Comments
 (0)