diff --git a/packages/react-scripts/config/paths.js b/packages/react-scripts/config/paths.js index d7a794243a4..7cd46d6eb36 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'), @@ -118,6 +116,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'); @@ -141,7 +141,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'), @@ -151,6 +150,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 f97f113474a..d6956e65aeb 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 # ******************************************************************************