forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compile cache and relative imports working.
- Loading branch information
Showing
10 changed files
with
233 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as path from "path"; | ||
import { assert, log } from "./util"; | ||
|
||
namespace ModuleExportsCache { | ||
const cache = new Map<string, object>(); | ||
export function set(fileName: string, moduleExports: object) { | ||
fileName = normalizeModuleName(fileName); | ||
assert( | ||
fileName.startsWith("/"), | ||
`Normalized modules should start with /\n${fileName}` | ||
); | ||
log("ModuleExportsCache set", fileName); | ||
cache.set(fileName, moduleExports); | ||
} | ||
export function get(fileName: string): object { | ||
fileName = normalizeModuleName(fileName); | ||
log("ModuleExportsCache get", fileName); | ||
let moduleExports = cache.get(fileName); | ||
if (moduleExports == null) { | ||
moduleExports = {}; | ||
set(fileName, moduleExports); | ||
} | ||
return moduleExports; | ||
} | ||
} | ||
|
||
function normalizeModuleName(fileName: string): string { | ||
// Remove the extension. | ||
return fileName.replace(/\.\w+$/, ""); | ||
} | ||
|
||
function normalizeRelativeModuleName(contextFn: string, depFn: string): string { | ||
if (depFn.startsWith("/")) { | ||
return depFn; | ||
} else { | ||
return path.resolve(path.dirname(contextFn), depFn); | ||
} | ||
} | ||
|
||
const executeQueue: Array<() => void> = []; | ||
|
||
export function executeQueueDrain(): void { | ||
let fn; | ||
while ((fn = executeQueue.shift())) { | ||
fn(); | ||
} | ||
} | ||
|
||
// tslint:disable-next-line:no-any | ||
type AmdFactory = (...args: any[]) => undefined | object; | ||
type AmdDefine = (deps: string[], factory: AmdFactory) => void; | ||
|
||
export function makeDefine(fileName: string): AmdDefine { | ||
const localDefine = (deps: string[], factory: AmdFactory): void => { | ||
const localRequire = (x: string) => { | ||
log("localRequire", x); | ||
}; | ||
const localExports = ModuleExportsCache.get(fileName); | ||
log("localDefine", fileName, deps, localExports); | ||
const args = deps.map(dep => { | ||
if (dep === "require") { | ||
return localRequire; | ||
} else if (dep === "exports") { | ||
return localExports; | ||
} else { | ||
dep = normalizeRelativeModuleName(fileName, dep); | ||
return ModuleExportsCache.get(dep); | ||
} | ||
}); | ||
executeQueue.push(() => { | ||
log("execute", fileName); | ||
const r = factory(...args); | ||
if (r != null) { | ||
ModuleExportsCache.set(fileName, r); | ||
throw Error("x"); | ||
} | ||
}); | ||
}; | ||
return localDefine; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.