From f45688fd48a9f0ebd099f5f94bf9828177cbea8f Mon Sep 17 00:00:00 2001 From: Martin van Dam Date: Wed, 8 Aug 2018 10:41:09 +0200 Subject: [PATCH 1/2] Validate tsconfig and report compiler errors --- index.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 2228e70..58c0d27 100644 --- a/index.js +++ b/index.js @@ -145,6 +145,24 @@ function composeSourceMaps(tsMap, babelMap, tsFileName, tsContent, babelCode) { return map.toJSON() } +// the typescript api needs some transformation of the JSON +// this will also validate the config file and reports errors about it +function convertCompilerOptionsFromJson(compilerOptionsJson) { + const { options, errors } = ts.convertCompilerOptionsFromJson( + compilerOptionsJson, + process.cwd() + ) + + if (errors.length) { + reportErrors(errors) + } + + return Object.assign(options, { + sourceMap: true, + inlineSources: true, + }) +} + const tsConfig = (() => { if (TSCONFIG_PATH) { const resolvedTsconfigPath = path.resolve(process.cwd(), TSCONFIG_PATH) @@ -167,7 +185,9 @@ const tsConfig = (() => { return fs.existsSync(path.join(dir, expectedTsConfigFileName)) }) } catch (error) { - console.error(`${chalk.bold(`***ERROR***`)} in react-native-typescript-transformer + console.error(`${chalk.bold( + `***ERROR***` + )} in react-native-typescript-transformer ${chalk.red(` Unable to find a "${expectedTsConfigFileName}" file.`)} @@ -184,10 +204,7 @@ const tsConfig = (() => { return loadJsonFile(tsConfigPath) })() -const compilerOptions = Object.assign(tsConfig.compilerOptions, { - sourceMap: true, - inlineSources: true, -}) +const compilerOptions = convertCompilerOptionsFromJson(tsConfig.compilerOptions) module.exports.getCacheKey = function() { const upstreamCacheKey = upstreamTransformer.getCacheKey From 59f1bad1188c4250c99cf87be35a639c1c02eca6 Mon Sep 17 00:00:00 2001 From: Martin van Dam Date: Mon, 24 Sep 2018 10:09:14 +0200 Subject: [PATCH 2/2] Listen to compilerOptions.typeRoots to support custom type definitions --- index.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 58c0d27..ff2177a 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ const semver = require('semver') const traverse = require('babel-traverse') const crypto = require('crypto') const chalk = require('chalk') +const glob = require('glob') const TSCONFIG_PATH = process.env.TSCONFIG_PATH @@ -163,6 +164,29 @@ function convertCompilerOptionsFromJson(compilerOptionsJson) { }) } +function findDefinitionFilesForTypeRoots(typeRoots) { + // when the typeRoots option is not defined return empty array + if (typeRoots === undefined || !typeRoots.length) { + return [] + } + + // normalize typeroots + const roots = compilerOptions.typeRoots.map(root => + path.normalize(`${root}/**/*.d.ts`) + ) + + // find all definition files in typeRoots + const definitionFiles = roots.map(root => glob.sync(root)) + + // flatten result + const flattenedDefinitionFiles = [].concat.apply([], definitionFiles) + + // remove duplicates + const typeDefinitions = Array.from(new Set(flattenedDefinitionFiles)) + + return typeDefinitions +} + const tsConfig = (() => { if (TSCONFIG_PATH) { const resolvedTsconfigPath = path.resolve(process.cwd(), TSCONFIG_PATH) @@ -225,7 +249,11 @@ module.exports.transform = function(src, filename, options) { if (filename.endsWith('.ts') || filename.endsWith('.tsx')) { if (compilerOptions.noEmitOnError) { - const program = ts.createProgram([filename], compilerOptions) + const definitions = findDefinitionFilesForTypeRoots( + compilerOptions.typeRoots + ) + const filenames = definitions.concat(filename) + const program = ts.createProgram(filenames, compilerOptions) const preErrors = ts .getPreEmitDiagnostics(program)