Skip to content

Commit 26a0f88

Browse files
authored
Refine how TypeScript env types are handled (facebook#5593)
* Specify types in package * Do not remove types file on eject * Stop copying types into generated project * Reference react and react-dom * Reference node types * Install node types as well * Restore copying * Add Node to the list of installed types * Reference Jest types * Remove jest types from install * Remove jest from CRA install * Remove Jest reference and let user do this themselves * Stop copying types file * Add types key to package.json * Add appTypeDeclarations and create when missing * Rename declarations file * Add Jest back to install instructions * Minimize diff
1 parent a566dd0 commit 26a0f88

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

Diff for: config/paths.js

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ module.exports = {
8484
appPackageJson: resolveApp('package.json'),
8585
appSrc: resolveApp('src'),
8686
appTsConfig: resolveApp('tsconfig.json'),
87+
appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
8788
yarnLockFile: resolveApp('yarn.lock'),
8889
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
8990
proxySetup: resolveApp('src/setupProxy.js'),
@@ -106,6 +107,7 @@ module.exports = {
106107
appPackageJson: resolveApp('package.json'),
107108
appSrc: resolveApp('src'),
108109
appTsConfig: resolveApp('tsconfig.json'),
110+
appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
109111
yarnLockFile: resolveApp('yarn.lock'),
110112
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
111113
proxySetup: resolveApp('src/setupProxy.js'),
@@ -138,6 +140,7 @@ if (
138140
appPackageJson: resolveOwn('package.json'),
139141
appSrc: resolveOwn('template/src'),
140142
appTsConfig: resolveOwn('template/tsconfig.json'),
143+
appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'),
141144
yarnLockFile: resolveOwn('template/yarn.lock'),
142145
testsSetup: resolveModule(resolveOwn, 'template/src/setupTests'),
143146
proxySetup: resolveOwn('template/src/setupProxy.js'),

Diff for: config/react-app.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// @remove-file-on-eject
2-
// Do not edit this file. It's replaced every time you launch a toolbox action.
3-
// If you need to add additional declarations, please do so in a new file.
1+
/// <reference types="node" />
2+
/// <reference types="react" />
3+
/// <reference types="react-dom" />
44

55
declare namespace NodeJS {
66
interface ProcessEnv {

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"bin": {
2222
"react-scripts": "./bin/react-scripts.js"
2323
},
24+
"types": "./config/react-app.d.ts",
2425
"dependencies": {
2526
"@babel/core": "7.1.0",
2627
"@svgr/webpack": "2.4.1",

Diff for: scripts/utils/verifyTypeScriptSetup.js

+7-15
Original file line numberDiff line numberDiff line change
@@ -238,21 +238,13 @@ function verifyTypeScriptSetup() {
238238
writeJson(paths.appTsConfig, appTsConfig);
239239
}
240240

241-
// Copy type declarations associated with this version of `react-scripts`
242-
const declaredTypes = path.resolve(
243-
__dirname,
244-
'..',
245-
'..',
246-
'config',
247-
'react-app.d.ts'
248-
);
249-
const declaredTypesContent = fs
250-
.readFileSync(declaredTypes, 'utf8')
251-
.replace(/\/\/ @remove-file-on-eject\r?\n/, '');
252-
fs.writeFileSync(
253-
path.resolve(paths.appSrc, 'react-app.d.ts'),
254-
declaredTypesContent
255-
);
241+
// Reference `react-scripts` types
242+
if (!fs.existsSync(paths.appTypeDeclarations)) {
243+
fs.writeFileSync(
244+
paths.appTypeDeclarations,
245+
`/// <reference types="react-scripts" />${os.EOL}`
246+
);
247+
}
256248
}
257249

258250
module.exports = verifyTypeScriptSetup;

0 commit comments

Comments
 (0)