From 26a0f88e83b417109b79b69bea82d4f0f5525f4e Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Sat, 27 Oct 2018 22:11:47 -0700 Subject: [PATCH] Refine how TypeScript env types are handled (#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 --- config/paths.js | 3 +++ config/react-app.d.ts | 6 +++--- package.json | 1 + scripts/utils/verifyTypeScriptSetup.js | 22 +++++++--------------- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/config/paths.js b/config/paths.js index f556b4b196f..70fc76a0140 100644 --- a/config/paths.js +++ b/config/paths.js @@ -84,6 +84,7 @@ module.exports = { appPackageJson: resolveApp('package.json'), appSrc: resolveApp('src'), appTsConfig: resolveApp('tsconfig.json'), + appTypeDeclarations: resolveApp('src/react-app-env.d.ts'), yarnLockFile: resolveApp('yarn.lock'), testsSetup: resolveModule(resolveApp, 'src/setupTests'), proxySetup: resolveApp('src/setupProxy.js'), @@ -106,6 +107,7 @@ module.exports = { appPackageJson: resolveApp('package.json'), appSrc: resolveApp('src'), appTsConfig: resolveApp('tsconfig.json'), + appTypeDeclarations: resolveApp('src/react-app-env.d.ts'), yarnLockFile: resolveApp('yarn.lock'), testsSetup: resolveModule(resolveApp, 'src/setupTests'), proxySetup: resolveApp('src/setupProxy.js'), @@ -138,6 +140,7 @@ if ( appPackageJson: resolveOwn('package.json'), appSrc: resolveOwn('template/src'), appTsConfig: resolveOwn('template/tsconfig.json'), + appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'), yarnLockFile: resolveOwn('template/yarn.lock'), testsSetup: resolveModule(resolveOwn, 'template/src/setupTests'), proxySetup: resolveOwn('template/src/setupProxy.js'), diff --git a/config/react-app.d.ts b/config/react-app.d.ts index 8d91473dc5a..31e990dedaf 100644 --- a/config/react-app.d.ts +++ b/config/react-app.d.ts @@ -1,6 +1,6 @@ -// @remove-file-on-eject -// Do not edit this file. It's replaced every time you launch a toolbox action. -// If you need to add additional declarations, please do so in a new file. +/// +/// +/// declare namespace NodeJS { interface ProcessEnv { diff --git a/package.json b/package.json index dd91e4372fa..69c2ee4b5c4 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "bin": { "react-scripts": "./bin/react-scripts.js" }, + "types": "./config/react-app.d.ts", "dependencies": { "@babel/core": "7.1.0", "@svgr/webpack": "2.4.1", diff --git a/scripts/utils/verifyTypeScriptSetup.js b/scripts/utils/verifyTypeScriptSetup.js index 068fde69591..eb592deced0 100644 --- a/scripts/utils/verifyTypeScriptSetup.js +++ b/scripts/utils/verifyTypeScriptSetup.js @@ -238,21 +238,13 @@ function verifyTypeScriptSetup() { writeJson(paths.appTsConfig, appTsConfig); } - // Copy type declarations associated with this version of `react-scripts` - const declaredTypes = path.resolve( - __dirname, - '..', - '..', - 'config', - 'react-app.d.ts' - ); - const declaredTypesContent = fs - .readFileSync(declaredTypes, 'utf8') - .replace(/\/\/ @remove-file-on-eject\r?\n/, ''); - fs.writeFileSync( - path.resolve(paths.appSrc, 'react-app.d.ts'), - declaredTypesContent - ); + // Reference `react-scripts` types + if (!fs.existsSync(paths.appTypeDeclarations)) { + fs.writeFileSync( + paths.appTypeDeclarations, + `/// ${os.EOL}` + ); + } } module.exports = verifyTypeScriptSetup;