Skip to content

Convert and validate tsconfig compiler options #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -145,6 +146,47 @@ 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,
})
}

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)
Expand All @@ -167,7 +209,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.`)}

Expand All @@ -184,10 +228,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
Expand All @@ -208,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)
Expand Down