2020-01-21 10:01:55 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-08 09:17:44 -05:00
|
|
|
|
2020-03-11 05:53:06 -04:00
|
|
|
import { MediaType, SourceFile, SourceFileJson } from "./sourcefile.ts";
|
|
|
|
import { normalizeString, CHAR_FORWARD_SLASH } from "./util.ts";
|
|
|
|
import { cwd } from "../ops/fs/dir.ts";
|
|
|
|
import { assert } from "../util.ts";
|
|
|
|
import * as util from "../util.ts";
|
|
|
|
import * as compilerOps from "../ops/compiler.ts";
|
2020-01-08 09:17:44 -05:00
|
|
|
|
|
|
|
function resolvePath(...pathSegments: string[]): string {
|
|
|
|
let resolvedPath = "";
|
|
|
|
let resolvedAbsolute = false;
|
|
|
|
|
|
|
|
for (let i = pathSegments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
|
|
|
let path: string;
|
|
|
|
|
|
|
|
if (i >= 0) path = pathSegments[i];
|
|
|
|
else path = cwd();
|
|
|
|
|
|
|
|
// Skip empty entries
|
|
|
|
if (path.length === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolvedPath = `${path}/${resolvedPath}`;
|
2020-03-05 07:05:41 -05:00
|
|
|
resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point the path should be resolved to a full absolute path, but
|
|
|
|
// handle relative paths to be safe (might happen when cwd() fails)
|
|
|
|
|
|
|
|
// Normalize the path
|
2020-03-05 07:05:41 -05:00
|
|
|
resolvedPath = normalizeString(
|
2020-01-08 09:17:44 -05:00
|
|
|
resolvedPath,
|
|
|
|
!resolvedAbsolute,
|
|
|
|
"/",
|
2020-03-28 13:03:49 -04:00
|
|
|
(code) => code === CHAR_FORWARD_SLASH
|
2020-01-08 09:17:44 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
if (resolvedAbsolute) {
|
|
|
|
if (resolvedPath.length > 0) return `/${resolvedPath}`;
|
|
|
|
else return "/";
|
|
|
|
} else if (resolvedPath.length > 0) return resolvedPath;
|
|
|
|
else return ".";
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveSpecifier(specifier: string, referrer: string): string {
|
|
|
|
if (!specifier.startsWith(".")) {
|
|
|
|
return specifier;
|
|
|
|
}
|
|
|
|
const pathParts = referrer.split("/");
|
|
|
|
pathParts.pop();
|
|
|
|
let path = pathParts.join("/");
|
|
|
|
path = path.endsWith("/") ? path : `${path}/`;
|
|
|
|
return resolvePath(path, specifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resolveModules(
|
|
|
|
specifiers: string[],
|
|
|
|
referrer?: string
|
|
|
|
): string[] {
|
|
|
|
util.log("compiler_imports::resolveModules", { specifiers, referrer });
|
2020-03-08 08:09:22 -04:00
|
|
|
return compilerOps.resolveModules(specifiers, referrer);
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function fetchSourceFiles(
|
|
|
|
specifiers: string[],
|
|
|
|
referrer?: string
|
|
|
|
): Promise<SourceFileJson[]> {
|
|
|
|
util.log("compiler_imports::fetchSourceFiles", { specifiers, referrer });
|
2020-03-08 08:09:22 -04:00
|
|
|
return compilerOps.fetchSourceFiles(specifiers, referrer);
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function getMediaType(filename: string): MediaType {
|
|
|
|
const maybeExtension = /\.([a-zA-Z]+)$/.exec(filename);
|
|
|
|
if (!maybeExtension) {
|
|
|
|
util.log(`!!! Could not identify valid extension: "${filename}"`);
|
|
|
|
return MediaType.Unknown;
|
|
|
|
}
|
|
|
|
const [, extension] = maybeExtension;
|
|
|
|
switch (extension.toLowerCase()) {
|
|
|
|
case "js":
|
|
|
|
return MediaType.JavaScript;
|
|
|
|
case "jsx":
|
|
|
|
return MediaType.JSX;
|
|
|
|
case "json":
|
|
|
|
return MediaType.Json;
|
|
|
|
case "ts":
|
|
|
|
return MediaType.TypeScript;
|
|
|
|
case "tsx":
|
|
|
|
return MediaType.TSX;
|
|
|
|
case "wasm":
|
|
|
|
return MediaType.Wasm;
|
|
|
|
default:
|
|
|
|
util.log(`!!! Unknown extension: "${extension}"`);
|
|
|
|
return MediaType.Unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function processLocalImports(
|
|
|
|
sources: Record<string, string>,
|
|
|
|
specifiers: Array<[string, string]>,
|
2020-02-19 22:58:05 -05:00
|
|
|
referrer?: string,
|
2020-03-02 16:18:27 -05:00
|
|
|
processJsImports = false
|
2020-01-08 09:17:44 -05:00
|
|
|
): string[] {
|
|
|
|
if (!specifiers.length) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const moduleNames = specifiers.map(
|
|
|
|
referrer
|
|
|
|
? ([, specifier]): string => resolveSpecifier(specifier, referrer)
|
|
|
|
: ([, specifier]): string => specifier
|
|
|
|
);
|
|
|
|
for (let i = 0; i < moduleNames.length; i++) {
|
|
|
|
const moduleName = moduleNames[i];
|
|
|
|
assert(moduleName in sources, `Missing module in sources: "${moduleName}"`);
|
|
|
|
const sourceFile =
|
|
|
|
SourceFile.get(moduleName) ||
|
|
|
|
new SourceFile({
|
|
|
|
url: moduleName,
|
|
|
|
filename: moduleName,
|
|
|
|
sourceCode: sources[moduleName],
|
2020-03-28 13:03:49 -04:00
|
|
|
mediaType: getMediaType(moduleName),
|
2020-01-08 09:17:44 -05:00
|
|
|
});
|
|
|
|
sourceFile.cache(specifiers[i][0], referrer);
|
|
|
|
if (!sourceFile.processed) {
|
2020-02-19 22:58:05 -05:00
|
|
|
processLocalImports(
|
|
|
|
sources,
|
2020-03-02 16:18:27 -05:00
|
|
|
sourceFile.imports(processJsImports),
|
2020-02-19 22:58:05 -05:00
|
|
|
sourceFile.url,
|
2020-03-02 16:18:27 -05:00
|
|
|
processJsImports
|
2020-02-19 22:58:05 -05:00
|
|
|
);
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return moduleNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function processImports(
|
|
|
|
specifiers: Array<[string, string]>,
|
2020-02-19 22:58:05 -05:00
|
|
|
referrer?: string,
|
2020-03-02 16:18:27 -05:00
|
|
|
processJsImports = false
|
2020-01-08 09:17:44 -05:00
|
|
|
): Promise<string[]> {
|
|
|
|
if (!specifiers.length) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const sources = specifiers.map(([, moduleSpecifier]) => moduleSpecifier);
|
|
|
|
const resolvedSources = resolveModules(sources, referrer);
|
|
|
|
const sourceFiles = await fetchSourceFiles(resolvedSources, referrer);
|
|
|
|
assert(sourceFiles.length === specifiers.length);
|
|
|
|
for (let i = 0; i < sourceFiles.length; i++) {
|
|
|
|
const sourceFileJson = sourceFiles[i];
|
|
|
|
const sourceFile =
|
|
|
|
SourceFile.get(sourceFileJson.url) || new SourceFile(sourceFileJson);
|
|
|
|
sourceFile.cache(specifiers[i][0], referrer);
|
|
|
|
if (!sourceFile.processed) {
|
2020-02-19 22:58:05 -05:00
|
|
|
await processImports(
|
2020-03-02 16:18:27 -05:00
|
|
|
sourceFile.imports(processJsImports),
|
2020-02-19 22:58:05 -05:00
|
|
|
sourceFile.url,
|
2020-03-02 16:18:27 -05:00
|
|
|
processJsImports
|
2020-02-19 22:58:05 -05:00
|
|
|
);
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return resolvedSources;
|
|
|
|
}
|