1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 04:52:26 -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");
// 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]

View file

@ -109,6 +109,13 @@ const ops = {
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
* 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<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 };