From dfb348447a70618c2324414141c3c92e7c63d889 Mon Sep 17 00:00:00 2001 From: Jan Florian Dietrich Date: Wed, 29 Mar 2017 19:09:23 +0200 Subject: [PATCH] Use "compilerOptions" from projects tsconfig for jest * .. if file is present * .. if file is valid json * .. if compilerOptions are set in tsconfig.json * .. fall back to old value in other cases --- .../config/jest/typescriptTransform.js | 26 ++++++++++++++----- packages/react-scripts/package.json | 1 + 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/react-scripts/config/jest/typescriptTransform.js b/packages/react-scripts/config/jest/typescriptTransform.js index f503b65b..b6ac285e 100644 --- a/packages/react-scripts/config/jest/typescriptTransform.js +++ b/packages/react-scripts/config/jest/typescriptTransform.js @@ -1,21 +1,33 @@ // Copyright 2004-present Facebook. All Rights Reserved. +const fs = require('fs'); const tsc = require('typescript'); +const tsconfigPath = require('app-root-path').resolve('/tsconfig.json'); + +let compilerConfig = { + module: tsc.ModuleKind.CommonJS, + jsx: tsc.JsxEmit.React, +} + +if (fs.existsSync(tsconfigPath)) { + try { + const tsconfig = require(tsconfigPath); + + if (tsconfig && tsconfig.compilerOptions) { + compilerConfig = tsconfig.compilerOptions; + } + } catch (e) { /* Do nothing - default is set */ } +} module.exports = { process(src, path) { if (path.endsWith('.ts') || path.endsWith('.tsx')) { return tsc.transpile( src, - { - module: tsc.ModuleKind.CommonJS, - jsx: tsc.JsxEmit.React, - }, - path, - [] + compilerConfig, + path, [] ); } return src; }, }; - diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index 33e4f867..3a42086c 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -24,6 +24,7 @@ "react-scripts-ts": "./bin/react-scripts-ts.js" }, "dependencies": { + "app-root-path": "^2.0.1", "autoprefixer": "6.7.2", "case-sensitive-paths-webpack-plugin": "1.1.4", "chalk": "1.1.3",