diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2020-03-19 03:39:53 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-18 12:39:53 -0400 |
commit | da8cb408c878aa6e90542e26173f1f14b5254d29 (patch) | |
tree | fda19fdecabb75cae8b2b5d3c72cdb9012a0af26 /cli/js/compiler/host.ts | |
parent | 83f49161953c0c79056a56a19754fbf298f53f21 (diff) |
Provide compiled JSON to TypeScript compiler. (#4404)
Fixes #4101
Previously, we would just provide the raw JSON to the TypeScript
compiler worker, but TypeScript does not transform JSON. This caused
a problem when emitting a bundle, that the JSON would just be "inlined"
into the output, instead of being transformed into a module.
This fixes this problem by providing the compiled JSON to the TypeScript
compiler, so TypeScript just sees JSON as a "normal" TypeScript module.
Diffstat (limited to 'cli/js/compiler/host.ts')
-rw-r--r-- | cli/js/compiler/host.ts | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/cli/js/compiler/host.ts b/cli/js/compiler/host.ts index 627c52970..457388bd9 100644 --- a/cli/js/compiler/host.ts +++ b/cli/js/compiler/host.ts @@ -242,8 +242,16 @@ export class Host implements ts.CompilerHost { assert(sourceFile != null); if (!sourceFile.tsSourceFile) { assert(sourceFile.sourceCode != null); + // even though we assert the extension for JSON modules to the compiler + // is TypeScript, TypeScript internally analyses the filename for its + // extension and tries to parse it as JSON instead of TS. We have to + // change the filename to the TypeScript file. sourceFile.tsSourceFile = ts.createSourceFile( - fileName.startsWith(ASSETS) ? sourceFile.filename : fileName, + fileName.startsWith(ASSETS) + ? sourceFile.filename + : fileName.toLowerCase().endsWith(".json") + ? `${fileName}.ts` + : fileName, sourceFile.sourceCode, languageVersion ); |