Skip to content

Commit 8a162cb

Browse files
Timernate770
authored andcommitted
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 d7d72e9 commit 8a162cb

File tree

6 files changed

+59
-14
lines changed

6 files changed

+59
-14
lines changed

packages/react-dev-utils/globby.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
var globby = require('globby');
11+
12+
module.exports = globby;

packages/react-dev-utils/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"getCacheIdentifier.js",
2424
"getCSSModuleLocalIdent.js",
2525
"getProcessForPort.js",
26+
"globby.js",
2627
"ignoredFiles.js",
2728
"immer.js",
2829
"InlineChunkHtmlPlugin.js",
@@ -53,6 +54,7 @@
5354
"filesize": "3.6.1",
5455
"find-up": "3.0.0",
5556
"global-modules": "1.0.0",
57+
"globby": "8.0.1",
5658
"gzip-size": "5.0.0",
5759
"immer": "1.7.2",
5860
"inquirer": "6.2.0",

packages/react-scripts/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$/;
@@ -144,7 +147,9 @@ module.exports = {
144147
// https://github.com/facebook/create-react-app/issues/290
145148
// `web` extension prefixes have been added for better support
146149
// for React Native Web.
147-
extensions: paths.moduleFileExtensions.map(ext => `.${ext}`),
150+
extensions: paths.moduleFileExtensions
151+
.map(ext => `.${ext}`)
152+
.filter(ext => useTypeScript || !ext.includes('ts')),
148153
alias: {
149154
// Support React Native Web
150155
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
@@ -405,7 +410,7 @@ module.exports = {
405410
publicPath: publicPath,
406411
}),
407412
// TypeScript type checking
408-
fs.existsSync(paths.appTsConfig) &&
413+
useTypeScript &&
409414
new ForkTsCheckerWebpackPlugin({
410415
typescript: resolve.sync('typescript', {
411416
basedir: paths.appNodeModules,

packages/react-scripts/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$/;
@@ -218,7 +221,9 @@ module.exports = {
218221
// https://github.com/facebook/create-react-app/issues/290
219222
// `web` extension prefixes have been added for better support
220223
// for React Native Web.
221-
extensions: paths.moduleFileExtensions.map(ext => `.${ext}`),
224+
extensions: paths.moduleFileExtensions
225+
.map(ext => `.${ext}`)
226+
.filter(ext => useTypeScript || !ext.includes('ts')),
222227
alias: {
223228
// Support React Native Web
224229
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/

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

test/fixtures/typescript/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
{}
1+
{
2+
"dependencies": {
3+
"typescript": "3.1.3"
4+
}
5+
}

0 commit comments

Comments
 (0)