Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Commit 4786bec

Browse files
authored
Merge pull request #220 from wmonk/2.9.0
2.9.0
2 parents 2c25d89 + cc52d21 commit 4786bec

File tree

6 files changed

+67
-30
lines changed

6 files changed

+67
-30
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Create React apps (with Typescript) with no build configuration.
44

5+
* [Getting Started](#tldr) – How to create a new app.
6+
* [User Guide](https://github.com/wmonk/create-react-app-typescript/blob/master/packages/react-scripts/template/README.md) – How to develop apps bootstrapped with react scripts ts.
7+
58
_Do you know react and want to try out typescript? Or do you know typescript and want to try out react?_ Get all the benefits from `create-react-app` but you use typescript! 🚀
69

710
## tl;dr

Diff for: packages/react-scripts/config/paths.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ const getPublicUrl = appPackageJson =>
4343
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
4444
function getServedPath(appPackageJson) {
4545
const publicUrl = getPublicUrl(appPackageJson);
46-
const servedUrl =
47-
envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
46+
const servedUrl = envPublicUrl ||
47+
(publicUrl ? url.parse(publicUrl).pathname : '/');
4848
return ensureSlash(servedUrl, true);
4949
}
5050

@@ -61,6 +61,7 @@ module.exports = {
6161
testsSetup: resolveApp('src/setupTests.ts'),
6262
appNodeModules: resolveApp('node_modules'),
6363
appTsConfig: resolveApp('tsconfig.json'),
64+
appTsLint: resolveApp('tslint.json'),
6465
publicUrl: getPublicUrl(resolveApp('package.json')),
6566
servedPath: getServedPath(resolveApp('package.json')),
6667
};
@@ -83,6 +84,7 @@ module.exports = {
8384
appNodeModules: resolveApp('node_modules'),
8485
appTsConfig: resolveApp('tsconfig.json'),
8586
appTsTestConfig: resolveApp('tsconfig.test.json'),
87+
appTsLint: resolveApp('tslint.json'),
8688
publicUrl: getPublicUrl(resolveApp('package.json')),
8789
servedPath: getServedPath(resolveApp('package.json')),
8890
// These properties only exist before ejecting:
@@ -92,8 +94,7 @@ module.exports = {
9294

9395
const ownPackageJson = require('../package.json');
9496
const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
95-
const reactScriptsLinked =
96-
fs.existsSync(reactScriptsPath) &&
97+
const reactScriptsLinked = fs.existsSync(reactScriptsPath) &&
9798
fs.lstatSync(reactScriptsPath).isSymbolicLink();
9899

99100
// config before publish: we're in ./packages/react-scripts/config/
@@ -114,6 +115,7 @@ if (
114115
testsSetup: resolveOwn('template/src/setupTests.ts'),
115116
appNodeModules: resolveOwn('node_modules'),
116117
appTsConfig: resolveOwn('template/tsconfig.json'),
118+
appTsLint: resolveOwn('template/tslint.json'),
117119
appTsTestConfig: resolveOwn('template/tsconfig.test.json'),
118120
publicUrl: getPublicUrl(resolveOwn('package.json')),
119121
servedPath: getServedPath(resolveOwn('package.json')),

Diff for: packages/react-scripts/config/webpack.config.dev.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
1818
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
1919
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
2020
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
21+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
2122
const getClientEnvironment = require('./env');
2223
const paths = require('./paths');
2324

@@ -134,14 +135,6 @@ module.exports = {
134135
// We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
135136
// { parser: { requireEnsure: false } },
136137

137-
// First, run the linter.
138-
// It's important to do this before Babel processes the JS.
139-
{
140-
test: /\.(ts|tsx)$/,
141-
loader: require.resolve('tslint-loader'),
142-
enforce: 'pre',
143-
include: paths.appSrc,
144-
},
145138
{
146139
test: /\.js$/,
147140
loader: require.resolve('source-map-loader'),
@@ -168,7 +161,15 @@ module.exports = {
168161
{
169162
test: /\.(ts|tsx)$/,
170163
include: paths.appSrc,
171-
loader: require.resolve('ts-loader'),
164+
use: [
165+
{
166+
loader: require.resolve('ts-loader'),
167+
options: {
168+
// disable type checker - we will use it in fork plugin
169+
transpileOnly: true,
170+
},
171+
},
172+
],
172173
},
173174
// "postcss" loader applies autoprefixer to our CSS.
174175
// "css" loader resolves paths in CSS and adds assets as dependencies.
@@ -262,6 +263,13 @@ module.exports = {
262263
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
263264
// You can remove this if you don't use Moment.js:
264265
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
266+
// Perform type checking and linting in a separate process to speed up compilation
267+
new ForkTsCheckerWebpackPlugin({
268+
async: false,
269+
watch: paths.appSrc,
270+
tsconfig: paths.appTsConfig,
271+
tslint: paths.appTsLint,
272+
}),
265273
],
266274
// Some libraries import Node modules but don't use them in the browser.
267275
// Tell Webpack to provide empty mocks for them so importing them works.

Diff for: packages/react-scripts/config/webpack.config.prod.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const ManifestPlugin = require('webpack-manifest-plugin');
1919
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
2020
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
2121
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
22+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
2223
const paths = require('./paths');
2324
const getClientEnvironment = require('./env');
2425

@@ -137,7 +138,6 @@ module.exports = {
137138
// TODO: Disable require.ensure as it's not a standard language feature.
138139
// We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
139140
// { parser: { requireEnsure: false } },
140-
141141
// First, run the linter.
142142
// It's important to do this before Typescript runs.
143143
{
@@ -167,11 +167,19 @@ module.exports = {
167167
name: 'static/media/[name].[hash:8].[ext]',
168168
},
169169
},
170-
//Compile .tsx?
170+
// Compile .tsx?
171171
{
172172
test: /\.(ts|tsx)$/,
173173
include: paths.appSrc,
174-
loader: require.resolve('ts-loader')
174+
use: [
175+
{
176+
loader: require.resolve('ts-loader'),
177+
options: {
178+
// disable type checker - we will use it in fork plugin
179+
transpileOnly: true,
180+
},
181+
},
182+
],
175183
},
176184
// The notation here is somewhat confusing.
177185
// "postcss" loader applies autoprefixer to our CSS.
@@ -341,6 +349,12 @@ module.exports = {
341349
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
342350
// You can remove this if you don't use Moment.js:
343351
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
352+
// Perform type checking and linting in a separate process to speed up compilation
353+
new ForkTsCheckerWebpackPlugin({
354+
async: false,
355+
tsconfig: paths.appTsConfig,
356+
tslint: paths.appTsLint,
357+
}),
344358
],
345359
// Some libraries import Node modules but don't use them in the browser.
346360
// Tell Webpack to provide empty mocks for them so importing them works.

Diff for: packages/react-scripts/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"dotenv": "4.0.0",
2929
"extract-text-webpack-plugin": "3.0.0",
3030
"file-loader": "0.11.2",
31+
"fork-ts-checker-webpack-plugin": "^0.2.8",
3132
"fs-extra": "3.0.1",
3233
"html-webpack-plugin": "2.29.0",
3334
"jest": "20.0.4",
@@ -59,3 +60,4 @@
5960
"fsevents": "1.1.2"
6061
}
6162
}
63+

Diff for: packages/react-scripts/template/README.md

+22-14
Original file line numberDiff line numberDiff line change
@@ -1244,9 +1244,9 @@ There is a broad spectrum of component testing techniques. They range from a “
12441244

12451245
Different projects choose different testing tradeoffs based on how often components change, and how much logic they contain. If you haven’t decided on a testing strategy yet, we recommend that you start with creating simple smoke tests for your components:
12461246

1247-
```js
1248-
import React from 'react';
1249-
import ReactDOM from 'react-dom';
1247+
```ts
1248+
import * as React from 'react';
1249+
import * asReactDOM from 'react-dom';
12501250
import App from './App';
12511251

12521252
it('renders without crashing', () => {
@@ -1255,26 +1255,34 @@ it('renders without crashing', () => {
12551255
});
12561256
```
12571257

1258-
This test mounts a component and makes sure that it didn’t throw during rendering. Tests like this provide a lot value with very little effort so they are great as a starting point, and this is the test you will find in `src/App.test.js`.
1258+
This test mounts a component and makes sure that it didn’t throw during rendering. Tests like this provide a lot value with very little effort so they are great as a starting point, and this is the test you will find in `src/App.test.tsx`.
12591259

12601260
When you encounter bugs caused by changing components, you will gain a deeper insight into which parts of them are worth testing in your application. This might be a good time to introduce more specific tests asserting specific expected output or behavior.
12611261

12621262
If you’d like to test components in isolation from the child components they render, we recommend using [`shallow()` rendering API](http://airbnb.io/enzyme/docs/api/shallow.html) from [Enzyme](http://airbnb.io/enzyme/). To install it, run:
12631263

12641264
```sh
1265-
npm install --save enzyme react-test-renderer
1265+
npm install --save-dev enzyme @types/enzyme enzyme-adapter-react-16 @types/enzyme-adapter-react-16 react-test-renderer @types/react-test-renderer
12661266
```
12671267

12681268
Alternatively you may use `yarn`:
12691269

12701270
```sh
1271-
yarn add enzyme react-test-renderer
1271+
yarn add --dev enzyme @types/enzyme enzyme-adapter-react-16 @types/enzyme-adapter-react-16 react-test-renderer @types/react-test-renderer
1272+
```
1273+
1274+
#### `src/setupTests.ts`
1275+
```ts
1276+
import * as Enzyme from 'enzyme';
1277+
import * as Adapter from 'enzyme-adapter-react-16';
1278+
1279+
Enzyme.configure({ adapter: new Adapter() });
12721280
```
12731281

12741282
You can write a smoke test with it too:
12751283

1276-
```js
1277-
import React from 'react';
1284+
```ts
1285+
import * as React from 'react';
12781286
import { shallow } from 'enzyme';
12791287
import App from './App';
12801288

@@ -1289,8 +1297,8 @@ You can read the [Enzyme documentation](http://airbnb.io/enzyme/) for more testi
12891297

12901298
Here is an example from Enzyme documentation that asserts specific output, rewritten to use Jest matchers:
12911299

1292-
```js
1293-
import React from 'react';
1300+
```ts
1301+
import * as React from 'react';
12941302
import { shallow } from 'enzyme';
12951303
import App from './App';
12961304

@@ -1323,7 +1331,7 @@ Alternatively you may use `yarn`:
13231331
yarn add jest-enzyme
13241332
```
13251333

1326-
Import it in [`src/setupTests.js`](#initializing-test-environment) to make its matchers available in every test:
1334+
Import it in [`src/setupTests.ts`](#initializing-test-environment) to make its matchers available in every test:
13271335

13281336
```js
13291337
import 'jest-enzyme';
@@ -1346,12 +1354,12 @@ and then use them in your tests like you normally do.
13461354

13471355
>Note: this feature is available with `[email protected]` and higher.
13481356
1349-
If your app uses a browser API that you need to mock in your tests or if you just need a global setup before running your tests, add a `src/setupTests.js` to your project. It will be automatically executed before running your tests.
1357+
If your app uses a browser API that you need to mock in your tests or if you just need a global setup before running your tests, add a `src/setupTests.ts` to your project. It will be automatically executed before running your tests.
13501358

13511359
For example:
13521360

1353-
#### `src/setupTests.js`
1354-
```js
1361+
#### `src/setupTests.ts`
1362+
```ts
13551363
const localStorageMock = {
13561364
getItem: jest.fn(),
13571365
setItem: jest.fn(),

0 commit comments

Comments
 (0)