From 6364bbf6dc8244508398f934d0882f05e0cb5dcc Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Mon, 29 Oct 2018 23:15:52 -0400 Subject: [PATCH] Remove react-scripts type reference on eject (#5611) * Remove react-scripts type reference on eject * Check for env file * Check eject for typescript * Shuffle appTypeDeclarations * Append internal types on eject * Ensure lib is published for types * Adjust comment * Don't add a bunch of new lines * File should exist and not be deleted * Add debug * Set file explicitly * Revert "Set file explicitly" This reverts commit bcd58a36cbd08a71af50b037d8f1fae6c595fb4e. * Copy file before destroying ourselves * Revert "Add debug" This reverts commit 0068ba81c6d79d99788877c9e1b618acd7412dce. --- packages/react-scripts/config/paths.js | 7 ++--- .../{config => lib}/react-app.d.ts | 0 packages/react-scripts/package.json | 3 ++- packages/react-scripts/scripts/eject.js | 27 +++++++++++++++++++ tasks/e2e-installs.sh | 14 ++++++++++ 5 files changed, 47 insertions(+), 4 deletions(-) rename packages/react-scripts/{config => lib}/react-app.d.ts (100%) diff --git a/packages/react-scripts/config/paths.js b/packages/react-scripts/config/paths.js index 70fc76a0140..b719054583b 100644 --- a/packages/react-scripts/config/paths.js +++ b/packages/react-scripts/config/paths.js @@ -84,7 +84,6 @@ 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'), @@ -107,7 +106,6 @@ 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'), @@ -117,6 +115,8 @@ module.exports = { // These properties only exist before ejecting: ownPath: resolveOwn('.'), ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3 + appTypeDeclarations: resolveApp('src/react-app-env.d.ts'), + ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'), }; const ownPackageJson = require('../package.json'); @@ -140,7 +140,6 @@ 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'), @@ -150,6 +149,8 @@ if ( // These properties only exist before ejecting: ownPath: resolveOwn('.'), ownNodeModules: resolveOwn('node_modules'), + appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'), + ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'), }; } // @remove-on-eject-end diff --git a/packages/react-scripts/config/react-app.d.ts b/packages/react-scripts/lib/react-app.d.ts similarity index 100% rename from packages/react-scripts/config/react-app.d.ts rename to packages/react-scripts/lib/react-app.d.ts diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index d6cea812012..15b8801b121 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -13,6 +13,7 @@ "files": [ "bin", "config", + "lib", "scripts", "template", "template-typescript", @@ -21,7 +22,7 @@ "bin": { "react-scripts": "./bin/react-scripts.js" }, - "types": "./config/react-app.d.ts", + "types": "./lib/react-app.d.ts", "dependencies": { "@babel/core": "7.1.0", "@svgr/webpack": "2.4.1", diff --git a/packages/react-scripts/scripts/eject.js b/packages/react-scripts/scripts/eject.js index 84353fc3c81..f4dba4d0b0f 100644 --- a/packages/react-scripts/scripts/eject.js +++ b/packages/react-scripts/scripts/eject.js @@ -223,6 +223,33 @@ inquirer ); console.log(); + if (fs.existsSync(paths.appTypeDeclarations)) { + try { + // Read app declarations file + let content = fs.readFileSync(paths.appTypeDeclarations, 'utf8'); + const ownContent = + fs.readFileSync(paths.ownTypeDeclarations, 'utf8').trim() + os.EOL; + + // Remove react-scripts reference since they're getting a copy of the types in their project + content = + content + // Remove react-scripts types + .replace( + /^\s*\/\/\/\s*.*(?:\n|$)/gm, + '' + ) + .trim() + os.EOL; + + fs.writeFileSync( + paths.appTypeDeclarations, + (ownContent + os.EOL + content).trim() + os.EOL + ); + } catch (e) { + // It's not essential that this succeeds, the TypeScript user should + // be able to re-create these types with ease. + } + } + // "Don't destroy what isn't ours" if (ownPath.indexOf(appPath) === 0) { try { diff --git a/tasks/e2e-installs.sh b/tasks/e2e-installs.sh index 690a9cd04fd..1be1c18468b 100755 --- a/tasks/e2e-installs.sh +++ b/tasks/e2e-installs.sh @@ -170,6 +170,7 @@ exists node_modules/react-scripts exists node_modules/typescript exists src/index.tsx exists tsconfig.json +exists src/react-app-env.d.ts checkTypeScriptDependencies # Check that the TypeScript template passes smoke tests, build, and normal tests @@ -177,6 +178,19 @@ yarn start --smoke-test yarn build CI=true yarn test +# Check eject behaves and works + +# Eject... +echo yes | npm run eject + +# Ensure env file still exists +exists src/react-app-env.d.ts + +# Check that the TypeScript template passes ejected smoke tests, build, and normal tests +yarn start --smoke-test +yarn build +CI=true yarn test + # ****************************************************************************** # Test --scripts-version with a tarball url # ******************************************************************************