From 2e1ab8232156a23afd22834c1e707fb3403c0db6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 31 Jul 2019 19:16:03 +0200 Subject: refactor: cleanup compiler pipeline (#2686) * remove fetch_source_file_and_maybe_compile_async and replace it with State.fetch_compiled_module * remove SourceFile.js_source() * introduce CompiledModule which is basically the same as deno::SourceInfo and represents arbitrary file that has been compiled to JS module * introduce //cli/compilers module containing all compilers * introduce JsCompiler which is a no-op compiler - output is the same as input, no compilation takes place - it is used for MediaType::JavaScript and MediaType::Unknown * introduce JsonCompiler that wraps JSON in default export * support JS-to-JS compilation using checkJs --- js/compiler.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'js/compiler.ts') diff --git a/js/compiler.ts b/js/compiler.ts index 34ac2f482..4203f753b 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -219,6 +219,8 @@ function getExtension( } class Host implements ts.CompilerHost { + extensionCache: Record = {}; + private readonly _options: ts.CompilerOptions = { allowJs: true, allowNonTsExtensions: true, @@ -370,10 +372,16 @@ class Host implements ts.CompilerHost { // This flags to the compiler to not go looking to transpile functional // code, anything that is in `/$asset$/` is just library code const isExternalLibraryImport = moduleName.startsWith(ASSETS); + const extension = getExtension( + resolvedFileName, + SourceFile.mediaType + ); + this.extensionCache[resolvedFileName] = extension; + const r = { resolvedFileName, isExternalLibraryImport, - extension: getExtension(resolvedFileName, SourceFile.mediaType) + extension }; return r; } else { @@ -401,6 +409,21 @@ class Host implements ts.CompilerHost { } else { assert(sourceFiles != null && sourceFiles.length == 1); const sourceFileName = sourceFiles![0].fileName; + const maybeExtension = this.extensionCache[sourceFileName]; + + if (maybeExtension) { + // NOTE: If it's a `.json` file we don't want to write it to disk. + // JSON files are loaded and used by TS compiler to check types, but we don't want + // to emit them to disk because output file is the same as input file. + if (maybeExtension === ts.Extension.Json) { + return; + } + + // NOTE: JavaScript files are only emitted to disk if `checkJs` option in on + if (maybeExtension === ts.Extension.Js && !this._options.checkJs) { + return; + } + } if (fileName.endsWith(".map")) { // Source Map -- cgit v1.2.3