0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-08 07:16:56 -05:00

Utilise internal names for snapshot bundles (#2973)

This commit is contained in:
Kitson Kelly 2019-09-19 09:56:13 +10:00 committed by Ryan Dahl
parent de19598d13
commit 693a45c3fd
2 changed files with 32 additions and 3 deletions

View file

@ -407,7 +407,7 @@ mod tests {
assert_eq!(actual.message, "TypeError: baz"); assert_eq!(actual.message, "TypeError: baz");
// Because this is accessing the live bundle, this test might be more fragile // Because this is accessing the live bundle, this test might be more fragile
assert_eq!(actual.frames.len(), 1); 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] #[test]

View file

@ -109,6 +109,13 @@ const ops = {
setEmitResult: 53 setEmitResult: 53
}; };
/**
* @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 * 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 * creation of runtime bundles. Some of the methods are implemented in a way
@ -176,18 +183,34 @@ class Host {
.replace("/index.d.ts", ""); .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", { const { sourceCode, moduleName } = dispatch("readFile", {
fileName, fileName,
languageVersion, languageVersion,
shouldCreateNewSourceFile 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( const sourceFile = ts.createSourceFile(
fileName, internalModuleName,
sourceCode, sourceCode,
languageVersion languageVersion
); );
sourceFile.moduleName = moduleName; sourceFile.moduleName = internalModuleName;
return sourceFile; return sourceFile;
} }
@ -247,11 +270,17 @@ class Host {
* @return {Array<ts.ResolvedModule | undefined>} * @return {Array<ts.ResolvedModule | undefined>}
*/ */
resolveModuleNames(moduleNames, containingFile) { 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[]} */ /** @type {string[]} */
const resolvedNames = dispatch("resolveModuleNames", { const resolvedNames = dispatch("resolveModuleNames", {
moduleNames, moduleNames,
containingFile containingFile
}); });
/** @type {ts.ResolvedModule[]} */
const r = resolvedNames.map(resolvedFileName => { const r = resolvedNames.map(resolvedFileName => {
const extension = getExtension(resolvedFileName); const extension = getExtension(resolvedFileName);
return { resolvedFileName, extension }; return { resolvedFileName, extension };