Skip to content

Commit f33e0ca

Browse files
committed
[TypeScript] Support tsconfig.prod.json and tslint.prod.json
1 parent b343104 commit f33e0ca

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed

packages/react-scripts/config/paths.js

+40-20
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ function getServedPath(appPackageJson) {
4747
return ensureSlash(servedUrl, true);
4848
}
4949

50-
const isFlow = fs.existsSync(resolveApp('.flowconfig'));
51-
const isTypeScript = fs.existsSync(resolveApp('tsconfig.json'));
52-
const useTSLint = isTypeScript && fs.existsSync(resolveApp('tslint.json'));
50+
const hasTSConfig = fs.existsSync(resolveApp('tsconfig.json'));
51+
const hasTSConfigProd = fs.existsSync(resolveApp('tsconfig.prod.json'));
52+
53+
const hasTSLint = fs.existsSync(resolveApp('tslint.json'));
54+
const hasTSLintProd = fs.existsSync(resolveApp('tslint.prod.json'));
55+
56+
const isTypeScript = hasTSConfig;
5357

5458
// config after eject: we're in ./config/
5559
module.exports = {
@@ -58,13 +62,19 @@ module.exports = {
5862
appBuild: resolveApp('build'),
5963
appPublic: resolveApp('public'),
6064
appHtml: resolveApp('public/index.html'),
61-
appIndexJs: resolveApp(isTypeScript ? 'src/index.tsx' : 'src/index.js'),
65+
appIndexJs: isTypeScript
66+
? resolveApp('src/index.tsx')
67+
: resolveApp('src/index.js'),
6268
appPackageJson: resolveApp('package.json'),
6369
appSrc: resolveApp('src'),
64-
testsSetup: resolveApp(
65-
isTypeScript ? 'src/setupTests.ts' : 'src/setupTests.js'
66-
),
70+
testsSetup: isTypeScript
71+
? resolveApp('src/setupTests.ts')
72+
: resolveApp('src/setupTests.js'),
6773
appNodeModules: resolveApp('node_modules'),
74+
appTSConfig: resolveApp('tsconfig.json'),
75+
appTSConfigProd: resolveApp('tsconfig.prod.json'),
76+
appTSLint: resolveApp('tslint.json'),
77+
appTSLintProd: resolveApp('tslint.prod.json'),
6878
publicUrl: getPublicUrl(resolveApp('package.json')),
6979
servedPath: getServedPath(resolveApp('package.json')),
7080
};
@@ -81,13 +91,19 @@ module.exports = {
8191
appBuild: resolveApp('build'),
8292
appPublic: resolveApp('public'),
8393
appHtml: resolveApp('public/index.html'),
84-
appIndexJs: resolveApp(isTypeScript ? 'src/index.tsx' : 'src/index.js'),
94+
appIndexJs: isTypeScript
95+
? resolveApp('src/index.tsx')
96+
: resolveApp('src/index.js'),
8597
appPackageJson: resolveApp('package.json'),
8698
appSrc: resolveApp('src'),
87-
testsSetup: resolveApp(
88-
isTypeScript ? 'src/setupTests.ts' : 'src/setupTests.js'
89-
),
99+
testsSetup: isTypeScript
100+
? resolveApp('src/setupTests.ts')
101+
: resolveApp('src/setupTests.js'),
90102
appNodeModules: resolveApp('node_modules'),
103+
appTSConfig: resolveApp('tsconfig.json'),
104+
appTSConfigProd: resolveApp('tsconfig.prod.json'),
105+
appTSLint: resolveApp('tslint.json'),
106+
appTSLintProd: resolveApp('tslint.prod.json'),
91107
publicUrl: getPublicUrl(resolveApp('package.json')),
92108
servedPath: getServedPath(resolveApp('package.json')),
93109
// These properties only exist before ejecting:
@@ -108,15 +124,19 @@ if (useTemplate) {
108124
appBuild: resolveOwn('../../build'),
109125
appPublic: resolveOwn('template/public'),
110126
appHtml: resolveOwn('template/public/index.html'),
111-
appIndexJs: resolveOwn(
112-
isTypeScript ? 'template/src/index.tsx' : 'template/src/index.js'
113-
),
127+
appIndexJs: isTypeScript
128+
? resolveOwn('template/src/index.tsx')
129+
: resolveOwn('template/src/index.js'),
114130
appPackageJson: resolveOwn('package.json'),
115131
appSrc: resolveOwn('template/src'),
116-
testsSetup: resolveOwn(
117-
isTypeScript ? 'template/src/setupTests.ts' : 'template/src/setupTests.js'
118-
),
132+
testsSetup: isTypeScript
133+
? resolveOwn('template/src/setupTests.ts')
134+
: resolveOwn('template/src/setupTests.js'),
119135
appNodeModules: resolveOwn('node_modules'),
136+
appTSConfig: resolveOwn('template/tsconfig.json'),
137+
appTSConfigProd: resolveOwn('template/tsconfig.prod.json'),
138+
appTSLint: resolveOwn('template/tslint.json'),
139+
appTSLintProd: resolveOwn('template/tslint.prod.json'),
120140
publicUrl: getPublicUrl(resolveOwn('package.json')),
121141
servedPath: getServedPath(resolveOwn('package.json')),
122142
// These properties only exist before ejecting:
@@ -128,11 +148,11 @@ if (useTemplate) {
128148

129149
module.exports.srcPaths = [module.exports.appSrc];
130150

131-
module.exports.isFlow = isFlow;
132-
133151
module.exports.isTypeScript = isTypeScript;
152+
module.exports.useTSConfigProd = isTypeScript && hasTSConfigProd;
134153

135-
module.exports.useTSLint = useTSLint;
154+
module.exports.useTSLint = isTypeScript && hasTSLint;
155+
module.exports.useTSLintProd = isTypeScript && hasTSLintProd;
136156

137157
module.exports.useYarn = fs.existsSync(
138158
path.join(module.exports.appPath, 'yarn.lock')

packages/react-scripts/config/webpack.config.dev.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,9 @@ module.exports = {
410410
paths.isTypeScript &&
411411
new ForkTsCheckerWebpackPlugin({
412412
async: false,
413+
tsconfig: paths.appTSConfig,
414+
tslint: paths.useTSLint ? paths.appTSLint : undefined,
413415
watch: paths.appSrc,
414-
tslint: paths.useTSLint,
415416
}),
416417
].filter(Boolean),
417418

packages/react-scripts/config/webpack.config.prod.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,15 @@ module.exports = {
482482
paths.isTypeScript &&
483483
new ForkTsCheckerWebpackPlugin({
484484
async: false,
485+
tsconfig: paths.useTSConfigProd
486+
? paths.appTSConfigProd
487+
: paths.appTSConfig,
488+
tslint: paths.useTSLintProd
489+
? paths.appTSLintProd
490+
: paths.useTSLint
491+
? paths.appTSLint
492+
: undefined,
485493
watch: paths.appSrc,
486-
tslint: paths.useTSLint,
487494
}),
488495
].filter(Boolean),
489496
// Some libraries import Node modules but don't use them in the browser.

packages/react-scripts/template/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,8 @@ To add TypeScript to a Create React App project, follow these steps:
949949
}
950950
```
951951

952+
_Note: `tsconfig.prod.json` is also supported._
953+
952954
4. [optional] Setup TSLint
953955

954956
1. Create a `tslint.json` file with the following content:
@@ -976,6 +978,8 @@ To add TypeScript to a Create React App project, follow these steps:
976978
}
977979
```
978980
981+
_Note: `tslint.prod.json` is also supported._
982+
979983
2. Run `npm install --dev tslint-react tslint-config-prettier` (or `yarn add --dev tslint-react tslint-config-prettier`).
980984
981985
Type errors will show up in the console.

0 commit comments

Comments
 (0)