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

Commit 923822e

Browse files
authored
Remove react-scripts type reference on eject (facebook#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 bcd58a3. * Copy file before destroying ourselves * Revert "Add debug" This reverts commit 0068ba8.
1 parent 54c107d commit 923822e

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

config/paths.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ 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'),
8887
yarnLockFile: resolveApp('yarn.lock'),
8988
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
9089
proxySetup: resolveApp('src/setupProxy.js'),
@@ -107,7 +106,6 @@ module.exports = {
107106
appPackageJson: resolveApp('package.json'),
108107
appSrc: resolveApp('src'),
109108
appTsConfig: resolveApp('tsconfig.json'),
110-
appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
111109
yarnLockFile: resolveApp('yarn.lock'),
112110
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
113111
proxySetup: resolveApp('src/setupProxy.js'),
@@ -117,6 +115,8 @@ module.exports = {
117115
// These properties only exist before ejecting:
118116
ownPath: resolveOwn('.'),
119117
ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
118+
appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
119+
ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
120120
};
121121

122122
const ownPackageJson = require('../package.json');
@@ -140,7 +140,6 @@ if (
140140
appPackageJson: resolveOwn('package.json'),
141141
appSrc: resolveOwn('template/src'),
142142
appTsConfig: resolveOwn('template/tsconfig.json'),
143-
appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'),
144143
yarnLockFile: resolveOwn('template/yarn.lock'),
145144
testsSetup: resolveModule(resolveOwn, 'template/src/setupTests'),
146145
proxySetup: resolveOwn('template/src/setupProxy.js'),
@@ -150,6 +149,8 @@ if (
150149
// These properties only exist before ejecting:
151150
ownPath: resolveOwn('.'),
152151
ownNodeModules: resolveOwn('node_modules'),
152+
appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'),
153+
ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
153154
};
154155
}
155156
// @remove-on-eject-end
File renamed without changes.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"files": [
1414
"bin",
1515
"config",
16+
"lib",
1617
"scripts",
1718
"template",
1819
"template-typescript",
@@ -21,7 +22,7 @@
2122
"bin": {
2223
"react-scripts": "./bin/react-scripts.js"
2324
},
24-
"types": "./config/react-app.d.ts",
25+
"types": "./lib/react-app.d.ts",
2526
"dependencies": {
2627
"@babel/core": "7.1.0",
2728
"@svgr/webpack": "2.4.1",

scripts/eject.js

+27
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,33 @@ inquirer
223223
);
224224
console.log();
225225

226+
if (fs.existsSync(paths.appTypeDeclarations)) {
227+
try {
228+
// Read app declarations file
229+
let content = fs.readFileSync(paths.appTypeDeclarations, 'utf8');
230+
const ownContent =
231+
fs.readFileSync(paths.ownTypeDeclarations, 'utf8').trim() + os.EOL;
232+
233+
// Remove react-scripts reference since they're getting a copy of the types in their project
234+
content =
235+
content
236+
// Remove react-scripts types
237+
.replace(
238+
/^\s*\/\/\/\s*<reference\s+types.+?"react-scripts".*\/>.*(?:\n|$)/gm,
239+
''
240+
)
241+
.trim() + os.EOL;
242+
243+
fs.writeFileSync(
244+
paths.appTypeDeclarations,
245+
(ownContent + os.EOL + content).trim() + os.EOL
246+
);
247+
} catch (e) {
248+
// It's not essential that this succeeds, the TypeScript user should
249+
// be able to re-create these types with ease.
250+
}
251+
}
252+
226253
// "Don't destroy what isn't ours"
227254
if (ownPath.indexOf(appPath) === 0) {
228255
try {

0 commit comments

Comments
 (0)