Skip to content
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

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

Closed
IgorMinar opened this issue Oct 15, 2015 · 14 comments · Fixed by #5310
Closed

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

IgorMinar opened this issue Oct 15, 2015 · 14 comments · Fixed by #5310
Labels
API Relates to the public API for TypeScript Question An issue which isn't directly actionable in code

Comments

@IgorMinar
Copy link

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

@IgorMinar IgorMinar changed the title Provide an api to convert tsconfig.json to CompilerOptions Provide an API to convert tsconfig.json to CompilerOptions Oct 15, 2015
@mhegazy mhegazy added Question An issue which isn't directly actionable in code API Relates to the public API for TypeScript labels Oct 16, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Oct 16, 2015

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
Copy link
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
Copy link
Contributor

mhegazy commented Oct 16, 2015

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
Copy link
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
Copy link
Contributor

vladima commented Oct 17, 2015

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
Copy link
Author

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

On Sat, Oct 17, 2015 at 4:49 PM Vladimir Matveev [email protected]
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
Copy link
Contributor

vladima commented Oct 19, 2015

@IgorMinar convertCompilerOptionsFromJson will be available in todays nightly build

@wycats
Copy link

wycats commented Mar 20, 2016

@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
Copy link

wycats commented Mar 20, 2016

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
Copy link
Contributor

vladima commented Mar 20, 2016

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

@wycats
Copy link

wycats commented Mar 21, 2016

@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
Copy link
Contributor

vladima commented Mar 21, 2016

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

@MartinJohns
Copy link
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
Copy link
Contributor

alexeagle commented Dec 12, 2017 via email

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API Relates to the public API for TypeScript Question An issue which isn't directly actionable in code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants