Skip to content

Commit 06138db

Browse files
authored
Automatically setup TypeScript when detected (facebook#5549)
* Don't resolve TS files if it's not detected in the project * Automatically create tsconfig.json file for user * Remove always false check * Add missing file * Don't filter paths too early
1 parent 82089e3 commit 06138db

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

config/webpack.config.dev.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ const publicUrl = '';
3939
// Get environment variables to inject into our app.
4040
const env = getClientEnvironment(publicUrl);
4141

42+
// Check if TypeScript is setup
43+
const useTypeScript = fs.existsSync(paths.appTsConfig);
44+
4245
// style files regexes
4346
const cssRegex = /\.css$/;
4447
const cssModuleRegex = /\.module\.css$/;
@@ -150,7 +153,9 @@ module.exports = {
150153
// https://github.com/facebook/create-react-app/issues/290
151154
// `web` extension prefixes have been added for better support
152155
// for React Native Web.
153-
extensions: paths.moduleFileExtensions.map(ext => `.${ext}`),
156+
extensions: paths.moduleFileExtensions
157+
.map(ext => `.${ext}`)
158+
.filter(ext => useTypeScript || !ext.includes('ts')),
154159
alias: {
155160
// Support React Native Web
156161
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
@@ -411,7 +416,7 @@ module.exports = {
411416
publicPath: publicPath,
412417
}),
413418
// TypeScript type checking
414-
fs.existsSync(paths.appTsConfig) &&
419+
useTypeScript &&
415420
new ForkTsCheckerWebpackPlugin({
416421
typescript: resolve.sync('typescript', {
417422
basedir: paths.appNodeModules,

config/webpack.config.prod.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ if (env.stringified['process.env'].NODE_ENV !== '"production"') {
5757
throw new Error('Production builds must have NODE_ENV=production.');
5858
}
5959

60+
// Check if TypeScript is setup
61+
const useTypeScript = fs.existsSync(paths.appTsConfig);
62+
6063
// style files regexes
6164
const cssRegex = /\.css$/;
6265
const cssModuleRegex = /\.module\.css$/;
@@ -224,7 +227,9 @@ module.exports = {
224227
// https://github.com/facebook/create-react-app/issues/290
225228
// `web` extension prefixes have been added for better support
226229
// for React Native Web.
227-
extensions: paths.moduleFileExtensions.map(ext => `.${ext}`),
230+
extensions: paths.moduleFileExtensions
231+
.map(ext => `.${ext}`)
232+
.filter(ext => useTypeScript || !ext.includes('ts')),
228233
alias: {
229234
// Support React Native Web
230235
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/

scripts/utils/verifyTypeScriptSetup.js

+27-10
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,33 @@ const path = require('path');
1515
const paths = require('../../config/paths');
1616
const os = require('os');
1717
const immer = require('react-dev-utils/immer').produce;
18+
const globby = require('react-dev-utils/globby').sync;
1819

1920
function writeJson(fileName, object) {
2021
fs.writeFileSync(fileName, JSON.stringify(object, null, 2) + os.EOL);
2122
}
2223

24+
function verifyNoTypeScript() {
25+
const typescriptFiles = globby('**/*.(ts|tsx)', { cwd: paths.appSrc });
26+
if (typescriptFiles.length > 0) {
27+
console.warn(
28+
chalk.yellow(
29+
`We detected TypeScript in your project (${chalk.bold(
30+
`src${path.sep}${typescriptFiles[0]}`
31+
)}) and created a ${chalk.bold('tsconfig.json')} file for you.`
32+
)
33+
);
34+
console.warn();
35+
return false;
36+
}
37+
return true;
38+
}
39+
2340
function verifyTypeScriptSetup() {
2441
let firstTimeSetup = false;
2542

2643
if (!fs.existsSync(paths.appTsConfig)) {
27-
if (!paths.appIndexJs.match(/\.tsx?$/)) {
44+
if (verifyNoTypeScript()) {
2845
return;
2946
}
3047
writeJson(paths.appTsConfig, {});
@@ -41,14 +58,12 @@ function verifyTypeScriptSetup() {
4158
}));
4259
} catch (_) {
4360
console.error(
44-
chalk.red(
45-
'We detected a',
46-
chalk.bold('tsconfig.json'),
47-
"in your package root but couldn't find an installation of",
48-
chalk.bold('typescript') + '.'
61+
chalk.bold.red(
62+
`It looks like you're trying to use TypeScript but do not have ${chalk.bold(
63+
'typescript'
64+
)} installed.`
4965
)
5066
);
51-
console.error();
5267
console.error(
5368
chalk.bold(
5469
'Please install',
@@ -60,9 +75,11 @@ function verifyTypeScriptSetup() {
6075
)
6176
);
6277
console.error(
63-
'If you are not trying to use TypeScript, please remove the ' +
64-
chalk.cyan('tsconfig.json') +
65-
' file from your package root.'
78+
chalk.bold(
79+
'If you are not trying to use TypeScript, please remove the ' +
80+
chalk.cyan('tsconfig.json') +
81+
' file from your package root (and any TypeScript files).'
82+
)
6683
);
6784
console.error();
6885
process.exit(1);

0 commit comments

Comments
 (0)