Skip to content

Support configuration inheritance #215

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

Merged
merged 4 commits into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ coverage/
tmp/
oldsrc/
oldtest/
package-lock.json
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@types/mocha": "^2.2.39",
"@types/node": "^8.0.20",
"@types/sinon": "^2.3.3",
"@types/systemjs": "^0.20.0",
"@types/systemjs": "^0.20.6",
"chai": "^4.1.1",
"chai-as-promised": "^7.1.1",
"chokidar-socket-emitter": "^0.6.0",
Expand Down
17 changes: 13 additions & 4 deletions src/resolve-options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ts from 'typescript'
import Logger from './logger'
import { formatErrors } from './format-errors'
import { __assign } from 'tslib'

const logger = new Logger({ debug: false })
export type CombinedOptions = PluginOptions & ts.CompilerOptions
Expand All @@ -14,8 +13,12 @@ export async function resolveOptions(

const globalTsconfigOptions = await loadTsconfigOptions(globalOptions, '', fetchJson)
const fileTsconfigOptions = await loadTsconfigOptions(fileOptions, fileAddress, fetchJson)
const mergedOptions = __assign({},
globalTsconfigOptions, globalOptions, fileTsconfigOptions, fileOptions)
const mergedOptions = {
...globalTsconfigOptions,
...globalOptions,
...fileTsconfigOptions,
...fileOptions
}

const finalOptions = parseOptions(mergedOptions)
validateOptions(finalOptions)
Expand All @@ -40,7 +43,13 @@ async function loadTsconfigOptions(
throw new Error(`failed to load tsconfig from ${tsconfigName}`)
}
else {
return result.config.compilerOptions
const extendedTsconfig = result.config.extends
? await loadTsconfigOptions(options, result.config.extends, fetchJson)
: undefined
return {
...extendedTsconfig,
...result.config.compilerOptions
}
}
}
else {
Expand Down
146 changes: 146 additions & 0 deletions test/resolve-options-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,152 @@ describe('Options', () => {
fetchSpy.firstCall.args[1].should.equal('')
})

it('fileConfig handles extends, fetching transitively', async () => {
const fileConfig = { tsconfig: true }

const fetchJson = (fileName, parentAddress) =>
parentAddress !== 'tsconfig.extended.json'
? Promise.resolve(JSON.stringify({
extends: 'tsconfig.extended.json',
compilerOptions: {
target: 'es2017'
}
}))
: Promise.resolve(JSON.stringify({
compilerOptions: {
downlevelIteration: true
}
}))

const finalOptions = await resolveOptions(undefined, fileConfig, 'file1.ts', fetchJson)
finalOptions.downlevelIteration.should.be.true
finalOptions.target.should.equal(ts.ScriptTarget.ES2017)
})

it('fileConfig handles extends, traversing multiple files', async () => {
const fileConfig = { tsconfig: true }

const fetchJson = (fileName, parentAddress) =>
parentAddress === 'tsconfig.extended.json'
? Promise.resolve(JSON.stringify({
extends: 'tsconfig.another-extended.json',
compilerOptions: {
target: 'es2017'
}
}))
: parentAddress === 'tsconfig.another-extended.json'
? Promise.resolve(JSON.stringify({
compilerOptions: {
downlevelIteration: true
}
}))
: Promise.resolve(JSON.stringify({
extends: 'tsconfig.extended.json',
compilerOptions: {
module: 'esnext'
}
}))

const finalOptions = await resolveOptions(undefined, fileConfig, 'file1.ts', fetchJson)
finalOptions.downlevelIteration.should.be.true
finalOptions.target.should.equal(ts.ScriptTarget.ES2017)
finalOptions.module.should.equal(ts.ModuleKind.ESNext)
})

it('fileConfig handles extends, giving precedence to extending file', async () => {
const fileConfig = { tsconfig: true }

const fetchJson = (fileName, parentAddress) =>
parentAddress !== 'tsconfig.extended.json'
? Promise.resolve(JSON.stringify({
extends: 'tsconfig.extended.json',
compilerOptions: {
target: 'es2017'
}
}))
: Promise.resolve(JSON.stringify({
compilerOptions: {
target: 'es5'
}
}))

const finalOptions = await resolveOptions(undefined, fileConfig, 'file1.ts', fetchJson)
finalOptions.target.should.equal(ts.ScriptTarget.ES2017)
})

it('globalConfig handles extends, fetching transitively', async () => {
const globalConfig = { tsconfig: true }

const fetchJson = (fileName, parentAddress) =>
parentAddress !== 'tsconfig.extended.json'
? Promise.resolve(JSON.stringify({
extends: 'tsconfig.extended.json',
compilerOptions: {
target: 'es2017'
}
}))
: Promise.resolve(JSON.stringify({
compilerOptions: {
downlevelIteration: true
}
}))

const finalOptions = await resolveOptions(globalConfig, undefined, 'file1.ts', fetchJson)
finalOptions.downlevelIteration.should.be.true
finalOptions.target.should.equal(ts.ScriptTarget.ES2017)
})

it('globalConfig handles tsconfig.extends, traversing multiple files', async () => {
const globalConfig = { tsconfig: true }

const fetchJson = (fileName, parentAddress) =>
parentAddress === 'tsconfig.extended.json'
? Promise.resolve(JSON.stringify({
extends: 'tsconfig.another-extended.json',
compilerOptions: {
target: 'es2017'
}
}))
: parentAddress === 'tsconfig.another-extended.json'
? Promise.resolve(JSON.stringify({
compilerOptions: {
downlevelIteration: true
}
}))
: Promise.resolve(JSON.stringify({
extends: 'tsconfig.extended.json',
compilerOptions: {
module: 'esnext'
}
}))

const finalOptions = await resolveOptions(globalConfig, undefined, 'file1.ts', fetchJson)
finalOptions.downlevelIteration.should.be.true
finalOptions.target.should.equal(ts.ScriptTarget.ES2017)
finalOptions.module.should.equal(ts.ModuleKind.ESNext)
})

it('globalConfig handles extends, giving precedence to extending file', async () => {
const globalConfig = { tsconfig: true }

const fetchJson = (fileName, parentAddress) =>
parentAddress !== 'tsconfig.extended.json'
? Promise.resolve(JSON.stringify({
extends: 'tsconfig.extended.json',
compilerOptions: {
target: 'es2017'
}
}))
: Promise.resolve(JSON.stringify({
compilerOptions: {
target: 'es5'
}
}))

const finalOptions = await resolveOptions(globalConfig, undefined, 'file1.ts', fetchJson)
finalOptions.target.should.equal(ts.ScriptTarget.ES2017)
})

it('specified configuration takes precedence over tsconfig configuration', async () => {
const globalConfig = { tsconfig: true, target: 'es2017' }
const finalOptions = await resolveOptions(globalConfig, undefined, 'file1.ts', fetchJson)
Expand Down