Skip to content

Provide an API to convert tsconfig.json to CompilerOptions #5276

Closed
@IgorMinar

Description

@IgorMinar

Since IDEs like VS Code use tsconfig.json to figure out how to parse a project, it makes sense for all the other build tools (gulp, broccoli, webpack, etc) to pull the tsc config from tsconfig.json as well, instead of having their own custom way to configure tsc.

In order for this to be seamless, Typescript needs to provide a way to parse tsconfig.json to CompilerOptions object. tsc already does this internally, but all of the apis it uses are private.

I've put together my own converter for Angular/broccoli-typescript, but it will get stale with the next version of typescript until I update my option mappings. See ts-config-converter.ts added in angular/angular#4779

I believe that this task is so common that it should be part of typescripts public api.

cc: @alexeagle

Activity

changed the title [-]Provide an api to convert tsconfig.json to CompilerOptions[/-] [+]Provide an API to convert tsconfig.json to CompilerOptions[/+] on Oct 15, 2015
added
QuestionAn issue which isn't directly actionable in code
APIRelates to the public API for TypeScript
on Oct 16, 2015
mhegazy

mhegazy commented on Oct 16, 2015

@mhegazy
Contributor

here you go: https://github.com/Microsoft/TypeScript/blob/master/lib/typescript.d.ts#L1536-L1555

use ts.parseConfigFile to parse the text. if you want to parse the JSON the same way as the compiler does as well (currently using JSON.parse, but possibly in the future do something more interesting allowing comments for instance) use ts.parseConfigFileText

IgorMinar

IgorMinar commented on Oct 16, 2015

@IgorMinar
Author

Awesome. That's what I need.

Is the filename option significant? We also have a use case where the configuration is an object already in the tsconfig format, so I would just serialize it and pass it in as text with bogus filename. Will there be any problems with that?

mhegazy

mhegazy commented on Oct 16, 2015

@mhegazy
Contributor

in parseConfigFileTextToJson the name is used only for the error message.
https://github.com/Microsoft/TypeScript/blob/master/src/compiler/commandLineParser.ts#L401

in parseJsonConfigFileContent basePath is expected to be the location of the tscongfig, as all files path in the options are considered relative to the file location.
https://github.com/Microsoft/TypeScript/blob/master/src/compiler/commandLineParser.ts#L416

IgorMinar

IgorMinar commented on Oct 17, 2015

@IgorMinar
Author

I tried using parseJsonConfigFileContent but it feels too heavyweight since it requires me to implement ParseConfigHost.

All I want is:

ts.convertConfigToCompilerOptions({
    emitDecoratorMetadata: true,
    experimentalDecorators: true,
    declaration: true,
    module: 'commonjs',
    moduleResolution: 'classic',
    noEmitOnError: true,
    rootDir: '.',
    sourceMap: true,
    sourceRoot: '.',
    target: 'es5'
})
=> CompilerOptions

Could that be a thing?

vladima

vladima commented on Oct 17, 2015

@vladima
Contributor

I don't see why not, we already have this code as nested function of parseJsonConfigFileContent.
Also as a temporary workaround for now you can use something like this

function convertConfigToCompilerOptions(opts) {
    var parsed = ts.parseJsonConfigFileContent({
        compilerOptions: opts,
        // if files are not specified then parseJsonConfigFileContent 
        // will use ParseConfigHost to collect files in containing folder
        files: []  
        },
        // we don't do any file lookups - host and base folders should not be used
        undefined, 
        undefined);
    return parsed.options;
}
IgorMinar

IgorMinar commented on Oct 17, 2015

@IgorMinar
Author

That worked! It's a bit awkward, but it works. Thanks Vladimir.

On Sat, Oct 17, 2015 at 4:49 PM Vladimir Matveev notifications@github.com
wrote:

I don't see why not, we already have this code as nested function of
parseJsonConfigFileContent.
Also as a temporary workaround for now you can use something like this

function convertConfigToCompilerOptions(opts) {
var parsed = ts.parseJsonConfigFileContent({
compilerOptions: opts,
// if files are not specified then parseJsonConfigFileContent
// will use ParseConfigHost to collect files in containing folder
files: []
},
// we don't do any file lookups - host and base folders should not be used
undefined,
undefined);
return parsed.options;
}


Reply to this email directly or view it on GitHub
#5276 (comment)
.

vladima

vladima commented on Oct 19, 2015

@vladima
Contributor

@IgorMinar convertCompilerOptionsFromJson will be available in todays nightly build

wycats

wycats commented on Mar 20, 2016

@wycats

@mhegazy @vladima This is getting pretty frustrating; it just broke again.

Here's what we had to do to get it working on typescript@next: tildeio/broccoli-typescript-compiler@ff874d9?w=1

Can you recommend a stable API we can use?

wycats

wycats commented on Mar 20, 2016

@wycats

See also tildeio/broccoli-typescript-compiler#7.

We're very eager to get on a supported, stable path here, but can't seem to find one.

vladima

vladima commented on Mar 20, 2016

@vladima
Contributor

@wycats apologies, this breaking change was not intentional. I'll revert convertCompilerOptionsFromJson to the shape it used to be in 1.8

wycats

wycats commented on Mar 21, 2016

@wycats

@vladima is there any way we can get the tests of this functionality to be considered regressions, so people don't just change them at the same time as the functionality?

Maybe a note in the tests?

vladima

vladima commented on Mar 21, 2016

@vladima
Contributor

I'll add tests on our side to make sure that public surface of the API is not changed in an unexpected way

MartinJohns

MartinJohns commented on Dec 10, 2017

@MartinJohns
Contributor

@vladima Unfortunately the host can't be set to undefined anymore, as the type is not ts.ParseConfigHost | undefined. I don't see a way to use this function anymore.

alexeagle

alexeagle commented on Dec 12, 2017

@alexeagle
Contributor
locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    APIRelates to the public API for TypeScriptQuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @wycats@alexeagle@IgorMinar@MartinJohns@mhegazy

      Issue actions

        Provide an API to convert tsconfig.json to CompilerOptions · Issue #5276 · microsoft/TypeScript