diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2019-09-19 09:56:13 +1000 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-09-18 19:56:13 -0400 |
commit | 693a45c3fd700bb48f9dc93829416aebb4e4d343 (patch) | |
tree | f4f7aa30864126ca8a9af3a47254db2168e74a1a /deno_typescript/compiler_main.js | |
parent | de19598d1303d5961d845bdf2b093ce934cf1058 (diff) |
Utilise internal names for snapshot bundles (#2973)
Diffstat (limited to 'deno_typescript/compiler_main.js')
-rw-r--r-- | deno_typescript/compiler_main.js | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/deno_typescript/compiler_main.js b/deno_typescript/compiler_main.js index 0d51479d5..c83bbdec3 100644 --- a/deno_typescript/compiler_main.js +++ b/deno_typescript/compiler_main.js @@ -110,6 +110,13 @@ const ops = { }; /** + * @type {Map<string, string>} + */ +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 * to just appease the TypeScript compiler, not to necessarily be a general @@ -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<ts.ResolvedModule | undefined>} */ 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 }; |