Skip to content

Commit 1d5a74b

Browse files
authored
Add TypeScript app creation (facebook#5550)
* Add TypeScript app creation * Actually specify new extension * Check for ts and tsx * Fix types in default service worker file * Mirror changes in JS version of SW * Separate templates * Use separate template directory * Remove unused function * Add a tsconfig file to the template * Test install of typescript * Add e2e for TypeScript * Check for index.tsx to make sure we're not in the JS template * Ensure TypeScript doesn't leak into normal installs * Ignore some files * Print version of CRA * Fix script * Add new template * Move test to correct location * Use `verifyTypeScriptSetup` routine to populate files * Ensure tsconfig is created
1 parent 4b26235 commit 1d5a74b

15 files changed

+382
-3
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"config",
1616
"scripts",
1717
"template",
18+
"template-typescript",
1819
"utils"
1920
],
2021
"bin": {

scripts/init.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const execSync = require('child_process').execSync;
2121
const spawn = require('react-dev-utils/crossSpawn');
2222
const { defaultBrowsers } = require('react-dev-utils/browsersHelper');
2323
const os = require('os');
24+
const verifyTypeScriptSetup = require('./utils/verifyTypeScriptSetup');
2425

2526
function isInGitRepository() {
2627
try {
@@ -90,6 +91,8 @@ module.exports = function(
9091
// Copy over some of the devDependencies
9192
appPackage.dependencies = appPackage.dependencies || {};
9293

94+
const useTypeScript = appPackage.dependencies['typescript'] != null;
95+
9396
// Setup the script rules
9497
appPackage.scripts = {
9598
start: 'react-scripts start',
@@ -122,7 +125,7 @@ module.exports = function(
122125
// Copy the files for the user
123126
const templatePath = template
124127
? path.resolve(originalDirectory, template)
125-
: path.join(ownPath, 'template');
128+
: path.join(ownPath, useTypeScript ? 'template-typescript' : 'template');
126129
if (fs.existsSync(templatePath)) {
127130
fs.copySync(templatePath, appPath);
128131
} else {
@@ -192,6 +195,10 @@ module.exports = function(
192195
}
193196
}
194197

198+
if (useTypeScript) {
199+
verifyTypeScriptSetup();
200+
}
201+
195202
if (tryGitInit(appPath)) {
196203
console.log();
197204
console.log('Initialized a git repository.');

template-typescript/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2+
3+
## Available Scripts
4+
5+
In the project directory, you can run:
6+
7+
### `npm start`
8+
9+
Runs the app in the development mode.<br>
10+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11+
12+
The page will reload if you make edits.<br>
13+
You will also see any lint errors in the console.
14+
15+
### `npm test`
16+
17+
Launches the test runner in the interactive watch mode.<br>
18+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19+
20+
### `npm run build`
21+
22+
Builds the app for production to the `build` folder.<br>
23+
It correctly bundles React in production mode and optimizes the build for the best performance.
24+
25+
The build is minified and the filenames include the hashes.<br>
26+
Your app is ready to be deployed!
27+
28+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29+
30+
### `npm run eject`
31+
32+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33+
34+
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.
35+
36+
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.
37+
38+
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.
39+
40+
## Learn More
41+
42+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43+
44+
To learn React, check out the [React documentation](https://reactjs.org/).

template-typescript/gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
3.78 KB
Binary file not shown.

template-typescript/public/index.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7+
<meta name="theme-color" content="#000000">
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/web-app-manifest/
11+
-->
12+
<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.
17+
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+
-->
22+
<title>React App</title>
23+
</head>
24+
<body>
25+
<noscript>
26+
You need to enable JavaScript to run this app.
27+
</noscript>
28+
<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+
-->
39+
</body>
40+
</html>
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
}
10+
],
11+
"start_url": ".",
12+
"display": "standalone",
13+
"theme_color": "#000000",
14+
"background_color": "#ffffff"
15+
}

template-typescript/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-typescript/src/App.test.tsx

+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+
});

template-typescript/src/App.tsx

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { Component } from 'react';
2+
import logo from './logo.svg';
3+
import './App.css';
4+
5+
class App extends Component {
6+
render() {
7+
return (
8+
<div className="App">
9+
<header className="App-header">
10+
<img src={logo} className="App-logo" alt="logo" />
11+
<p>
12+
Edit <code>src/App.js</code> and save to reload.
13+
</p>
14+
<a
15+
className="App-link"
16+
href="https://reactjs.org"
17+
target="_blank"
18+
rel="noopener noreferrer"
19+
>
20+
Learn React
21+
</a>
22+
</header>
23+
</div>
24+
);
25+
}
26+
}
27+
28+
export default App;

template-typescript/src/index.css

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
body {
2+
margin: 0;
3+
padding: 0;
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;
14+
}

template-typescript/src/index.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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';
6+
7+
ReactDOM.render(<App />, document.getElementById('root'));
8+
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();

template-typescript/src/logo.svg

+7
Loading

0 commit comments

Comments
 (0)