diff --git a/cli/source_maps.rs b/cli/source_maps.rs index b3b8d7e56f..b2943cddb7 100644 --- a/cli/source_maps.rs +++ b/cli/source_maps.rs @@ -407,7 +407,7 @@ mod tests { assert_eq!(actual.message, "TypeError: baz"); // Because this is accessing the live bundle, this test might be more fragile assert_eq!(actual.frames.len(), 1); - assert!(actual.frames[0].script_name.ends_with("js/window.ts")); + assert!(actual.frames[0].script_name.ends_with("/window.ts")); } #[test] diff --git a/deno_typescript/compiler_main.js b/deno_typescript/compiler_main.js index 0d51479d50..c83bbdec3c 100644 --- a/deno_typescript/compiler_main.js +++ b/deno_typescript/compiler_main.js @@ -109,6 +109,13 @@ const ops = { setEmitResult: 53 }; +/** + * @type {Map} + */ +const moduleMap = new Map(); + +const externalSpecifierRegEx = /^file:\/{3}\S+\/js(\/\S+\.ts)$/; + /** * This is a minimal implementation of a compiler host to be able to allow the * creation of runtime bundles. Some of the methods are implemented in a way @@ -176,18 +183,34 @@ class Host { .replace("/index.d.ts", ""); } + // This looks up any modules that have been mapped to internal names + if (moduleMap.has(fileName)) { + fileName = moduleMap.get(fileName); + } + const { sourceCode, moduleName } = dispatch("readFile", { fileName, languageVersion, shouldCreateNewSourceFile }); + // If we match the external specifier regex, we will then create an internal + // specifier and then use that when creating the source file + let internalModuleName = moduleName; + const result = externalSpecifierRegEx.exec(moduleName); + if (result) { + const [, specifier] = result; + const internalSpecifier = `$deno$${specifier}`; + moduleMap.set(internalSpecifier, moduleName); + internalModuleName = internalSpecifier; + } + const sourceFile = ts.createSourceFile( - fileName, + internalModuleName, sourceCode, languageVersion ); - sourceFile.moduleName = moduleName; + sourceFile.moduleName = internalModuleName; return sourceFile; } @@ -247,11 +270,17 @@ class Host { * @return {Array} */ resolveModuleNames(moduleNames, containingFile) { + // If the containing file is an internal specifier, map it back to the + // external specifier + containingFile = moduleMap.has(containingFile) + ? moduleMap.get(containingFile) + : containingFile; /** @type {string[]} */ const resolvedNames = dispatch("resolveModuleNames", { moduleNames, containingFile }); + /** @type {ts.ResolvedModule[]} */ const r = resolvedNames.map(resolvedFileName => { const extension = getExtension(resolvedFileName); return { resolvedFileName, extension };