Skip to content

Commit fbfa9b3

Browse files
Timernate770
authored andcommitted
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 1d0fd21 commit fbfa9b3

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

packages/react-scripts/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'),
@@ -118,6 +116,8 @@ module.exports = {
118116
// These properties only exist before ejecting:
119117
ownPath: resolveOwn('.'),
120118
ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
119+
appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
120+
ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
121121
};
122122

123123
const ownPackageJson = require('../package.json');
@@ -141,7 +141,6 @@ if (
141141
appPackageJson: resolveOwn('package.json'),
142142
appSrc: resolveOwn('template/src'),
143143
appTsConfig: resolveOwn('template/tsconfig.json'),
144-
appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'),
145144
yarnLockFile: resolveOwn('template/yarn.lock'),
146145
testsSetup: resolveModule(resolveOwn, 'template/src/setupTests'),
147146
proxySetup: resolveOwn('template/src/setupProxy.js'),
@@ -151,6 +150,8 @@ if (
151150
// These properties only exist before ejecting:
152151
ownPath: resolveOwn('.'),
153152
ownNodeModules: resolveOwn('node_modules'),
153+
appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'),
154+
ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
154155
};
155156
}
156157
// @remove-on-eject-end

packages/react-scripts/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",

packages/react-scripts/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 {

tasks/e2e-installs.sh

+14
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,27 @@ exists node_modules/react-scripts
170170
exists node_modules/typescript
171171
exists src/index.tsx
172172
exists tsconfig.json
173+
exists src/react-app-env.d.ts
173174
checkTypeScriptDependencies
174175

175176
# Check that the TypeScript template passes smoke tests, build, and normal tests
176177
yarn start --smoke-test
177178
yarn build
178179
CI=true yarn test
179180

181+
# Check eject behaves and works
182+
183+
# Eject...
184+
echo yes | npm run eject
185+
186+
# Ensure env file still exists
187+
exists src/react-app-env.d.ts
188+
189+
# Check that the TypeScript template passes ejected smoke tests, build, and normal tests
190+
yarn start --smoke-test
191+
yarn build
192+
CI=true yarn test
193+
180194
# ******************************************************************************
181195
# Test --scripts-version with a tarball url
182196
# ******************************************************************************

0 commit comments

Comments
 (0)