From d670c4b2bd823fc85d7f2efcba5d70edb5019987 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Mon, 3 Feb 2020 23:24:53 -0500 Subject: [PATCH] (fix): parse tsconfig extends, trailing commas, and comments - use ts.readConfigFile to properly parse a tsconfig instead of just trying to readJSON - add tests for all of the above use cases NOTE: this is only necessary for internal, TSDX-specific parsing of tsconfig.json. rollup-plugin-typescript2 already handles these options, but internal options like esModuleInterop etc are also used & parsed independently of rpts2 --- src/createRollupConfig.ts | 12 ++++---- .../fixtures/build-default/tsconfig.base.json | 28 +++++++++++++++++++ test/fixtures/build-default/tsconfig.json | 28 ++----------------- 3 files changed, 36 insertions(+), 32 deletions(-) create mode 100644 test/fixtures/build-default/tsconfig.base.json diff --git a/src/createRollupConfig.ts b/src/createRollupConfig.ts index 0092baf28..1a61e6593 100644 --- a/src/createRollupConfig.ts +++ b/src/createRollupConfig.ts @@ -10,10 +10,11 @@ import replace from '@rollup/plugin-replace'; import resolve from '@rollup/plugin-node-resolve'; import sourceMaps from 'rollup-plugin-sourcemaps'; import typescript from 'rollup-plugin-typescript2'; +import ts from 'typescript'; + import { extractErrors } from './errors/extractErrors'; import { babelPluginTsdx } from './babelPluginTsdx'; import { TsdxOptions } from './types'; -import * as fs from 'fs-extra'; const errorCodeOpts = { errorMapFilePath: paths.appErrorsJson, @@ -43,10 +44,9 @@ export async function createRollupConfig( .filter(Boolean) .join('.'); - let tsconfigJSON; - try { - tsconfigJSON = await fs.readJSON(opts.tsconfig || paths.tsconfigJson); - } catch (e) {} + const tsconfigPath = opts.tsconfig || paths.tsconfigJson; + // borrowed from https://github.com/facebook/create-react-app/pull/7248 + const tsconfigJSON = ts.readConfigFile(tsconfigPath, ts.sys.readFile).config; return { // Tell Rollup the entry point to the package @@ -136,7 +136,7 @@ export async function createRollupConfig( }, }, typescript({ - typescript: require('typescript'), + typescript: ts, cacheRoot: `./node_modules/.cache/tsdx/${opts.format}/`, tsconfig: opts.tsconfig, tsconfigDefaults: { diff --git a/test/fixtures/build-default/tsconfig.base.json b/test/fixtures/build-default/tsconfig.base.json new file mode 100644 index 000000000..fe05973f4 --- /dev/null +++ b/test/fixtures/build-default/tsconfig.base.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + "module": "ESNext", + "lib": ["dom", "esnext"], + "declaration": true, + "sourceMap": true, + "rootDir": "./src", + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "strictPropertyInitialization": true, + "noImplicitThis": true, + "alwaysStrict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "moduleResolution": "node", + "baseUrl": "./", + "paths": { + "*": ["src/*", "node_modules/*"] + }, + "jsx": "react", + "esModuleInterop": true + }, + "include": ["src", "types"], // test parsing of trailing comma & comment +} diff --git a/test/fixtures/build-default/tsconfig.json b/test/fixtures/build-default/tsconfig.json index 7f2bd50c5..120f170a5 100644 --- a/test/fixtures/build-default/tsconfig.json +++ b/test/fixtures/build-default/tsconfig.json @@ -1,28 +1,4 @@ { - "compilerOptions": { - "module": "ESNext", - "lib": ["dom", "esnext"], - "declaration": true, - "sourceMap": true, - "rootDir": "./src", - "strict": true, - "noImplicitAny": true, - "strictNullChecks": true, - "strictFunctionTypes": true, - "strictPropertyInitialization": true, - "noImplicitThis": true, - "alwaysStrict": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true, - "moduleResolution": "node", - "baseUrl": "./", - "paths": { - "*": ["src/*", "node_modules/*"] - }, - "jsx": "react", - "esModuleInterop": true - }, - "include": ["src", "types"] + // ensure that extends works (trailing comma too) + "extends": "./tsconfig.base.json", }