Skip to content

Commit

Permalink
fix: use compiler options from tsconfig.json schema, not ts module
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Jul 21, 2019
1 parent e0b80ac commit c651fcc
Show file tree
Hide file tree
Showing 4 changed files with 513 additions and 61 deletions.
3 changes: 2 additions & 1 deletion src/clean-addon/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EmitOptions, CompilerOptions } from '../interfaces'
import { CompilerOptions } from 'typescript'
import { EmitOptions } from '../interfaces'
import { ensureAbsolutePath, relativePath, normalizePath, parentPaths } from '../path.utils'
import rmrf from './rmrf'

Expand Down
18 changes: 11 additions & 7 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { existsSync } from 'fs'
import { join, normalize } from 'path'
import ts from 'typescript'
import { join } from 'path'
import { createProgramFromConfig, build } from '.'

jest.mock('./clean-addon/rmrf')
Expand All @@ -9,6 +8,8 @@ const basePath = join(__dirname, '__fixtures__')
const configFilePath = 'tsconfig.fixture.json'

test('Create program by overriding config file', async () => {
const consoleWarnSpy = spyOn(console, 'warn')

const program = createProgramFromConfig({
basePath,
configFilePath,
Expand All @@ -24,10 +25,12 @@ test('Create program by overriding config file', async () => {
expect(program.getCompilerOptions()).toMatchObject({
strict: true,
// `compilerOptions` properties returns unix separators in windows paths
rootDir: normalize(join(basePath, 'src')),
declaration: 'true',
rootDir: join(basePath, 'src').replace(/\\/g, '/'),
declaration: undefined,
})

expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining("'declaration' requires a value of type boolean"))

expect(program.getRootFileNames()).toHaveLength(1)
})

Expand All @@ -39,9 +42,10 @@ test('Build without errors with config from scratch', async () => {
// configFilePath,
clean: { outDir: true },
compilerOptions: {
module: ts.ModuleKind.ES2015,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
target: ts.ScriptTarget.ES5,
module: 'es2015',
moduleResolution: 'node',
target: 'es2019',
lib: ['es2019'],
rootDir: 'src',
outDir: 'dist',
declaration: false,
Expand Down
32 changes: 2 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function createProgramFromConfig({
config = readResult.config
}

// config.compilerOptions = Object.assign({}, config.compilerOptions, compilerOptions)
config.compilerOptions = Object.assign({}, config.compilerOptions, compilerOptions)
if (include) config.include = include
if (exclude) config.exclude = exclude
if (files) config.files = files
Expand All @@ -49,14 +49,12 @@ export function createProgramFromConfig({
config,
ts.sys,
basePath,
compilerOptions,
undefined,
configFilePath
)

logDiagnostics(errors, true)

normalizeOptionValues(options, basePath)

const program = ts.createProgram({
options,
rootNames: fileNames,
Expand Down Expand Up @@ -108,29 +106,3 @@ function logDiagnostics(diagnostics: ts.Diagnostic[], better = false) {

console.warn(message)
}

/**
* @see https://github.com/microsoft/TypeScript/blob/v3.5.3/src/compiler/commandLineParser.ts#L2528
* @internal
*/
function normalizeOptionValues(options: ts.CompilerOptions, basePath: string) {
// https://github.com/microsoft/TypeScript/blob/v3.5.3/src/compiler/commandLineParser.ts#L76
const pathOptions = [
'outFile',
'outDir',
'rootDir',
'tsBuildInfoFile',
'baseUrl',
'rootDirs',
'typeRoots',
'declarationDir',
]

for (const o of pathOptions) {
if (options[o] == null) continue

options[o] = Array.isArray(options[o])
? (options[o] as string[]).map((value) => ensureAbsolutePath(value, basePath))
: ensureAbsolutePath(options[o] as string, basePath)
}
}
Loading

0 comments on commit c651fcc

Please sign in to comment.