2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-11-20 11:02:08 -05:00
|
|
|
|
2020-03-11 05:53:06 -04:00
|
|
|
import { SYSTEM_LOADER } from "./bootstrap.ts";
|
|
|
|
import { commonPath, normalizeString, CHAR_FORWARD_SLASH } from "./util.ts";
|
|
|
|
import { assert } from "../util.ts";
|
2019-11-20 11:02:08 -05:00
|
|
|
|
|
|
|
let rootExports: string[] | undefined;
|
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
function normalizeUrl(rootName: string): string {
|
|
|
|
const match = /^(\S+:\/{2,3})(.+)$/.exec(rootName);
|
|
|
|
if (match) {
|
|
|
|
const [, protocol, path] = match;
|
|
|
|
return `${protocol}${normalizeString(
|
|
|
|
path,
|
|
|
|
false,
|
|
|
|
"/",
|
2020-03-28 13:03:49 -04:00
|
|
|
(code) => code === CHAR_FORWARD_SLASH
|
2020-01-08 09:17:44 -05:00
|
|
|
)}`;
|
|
|
|
} else {
|
|
|
|
return rootName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildBundle(
|
|
|
|
rootName: string,
|
2019-11-20 11:02:08 -05:00
|
|
|
data: string,
|
|
|
|
sourceFiles: readonly ts.SourceFile[]
|
2020-01-08 09:17:44 -05:00
|
|
|
): string {
|
2019-11-20 11:02:08 -05:00
|
|
|
// when outputting to AMD and a single outfile, TypeScript makes up the module
|
|
|
|
// specifiers which are used to define the modules, and doesn't expose them
|
|
|
|
// publicly, so we have to try to replicate
|
2020-03-28 13:03:49 -04:00
|
|
|
const sources = sourceFiles.map((sf) => sf.fileName);
|
2019-11-20 11:02:08 -05:00
|
|
|
const sharedPath = commonPath(sources);
|
2020-01-08 09:17:44 -05:00
|
|
|
rootName = normalizeUrl(rootName)
|
|
|
|
.replace(sharedPath, "")
|
|
|
|
.replace(/\.\w+$/i, "");
|
2020-02-26 11:59:33 -05:00
|
|
|
// If one of the modules requires support for top-level-await, TypeScript will
|
|
|
|
// emit the execute function as an async function. When this is the case we
|
|
|
|
// need to bubble up the TLA to the instantiation, otherwise we instantiate
|
|
|
|
// synchronously.
|
|
|
|
const hasTla = data.match(/execute:\sasync\sfunction\s/);
|
2019-11-20 11:02:08 -05:00
|
|
|
let instantiate: string;
|
|
|
|
if (rootExports && rootExports.length) {
|
2020-02-26 11:59:33 -05:00
|
|
|
instantiate = hasTla
|
2020-03-04 08:26:00 -05:00
|
|
|
? `const __exp = await __instantiateAsync("${rootName}");\n`
|
|
|
|
: `const __exp = __instantiate("${rootName}");\n`;
|
2019-11-20 11:02:08 -05:00
|
|
|
for (const rootExport of rootExports) {
|
|
|
|
if (rootExport === "default") {
|
2020-02-12 16:41:51 -05:00
|
|
|
instantiate += `export default __exp["${rootExport}"];\n`;
|
2019-11-20 11:02:08 -05:00
|
|
|
} else {
|
2020-02-12 16:41:51 -05:00
|
|
|
instantiate += `export const ${rootExport} = __exp["${rootExport}"];\n`;
|
2019-11-20 11:02:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-26 11:59:33 -05:00
|
|
|
instantiate = hasTla
|
2020-03-04 08:26:00 -05:00
|
|
|
? `await __instantiateAsync("${rootName}");\n`
|
|
|
|
: `__instantiate("${rootName}");\n`;
|
2019-11-20 11:02:08 -05:00
|
|
|
}
|
2020-02-12 16:41:51 -05:00
|
|
|
return `${SYSTEM_LOADER}\n${data}\n${instantiate}`;
|
2019-11-20 11:02:08 -05:00
|
|
|
}
|
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
export function setRootExports(program: ts.Program, rootModule: string): void {
|
2019-11-20 11:02:08 -05:00
|
|
|
// get a reference to the type checker, this will let us find symbols from
|
|
|
|
// the AST.
|
|
|
|
const checker = program.getTypeChecker();
|
|
|
|
// get a reference to the main source file for the bundle
|
2020-01-08 09:17:44 -05:00
|
|
|
const mainSourceFile = program.getSourceFile(rootModule);
|
2019-11-20 11:02:08 -05:00
|
|
|
assert(mainSourceFile);
|
|
|
|
// retrieve the internal TypeScript symbol for this AST node
|
|
|
|
const mainSymbol = checker.getSymbolAtLocation(mainSourceFile);
|
|
|
|
if (!mainSymbol) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rootExports = checker
|
|
|
|
.getExportsOfModule(mainSymbol)
|
|
|
|
// .getExportsOfModule includes type only symbols which are exported from
|
|
|
|
// the module, so we need to try to filter those out. While not critical
|
|
|
|
// someone looking at the bundle would think there is runtime code behind
|
|
|
|
// that when there isn't. There appears to be no clean way of figuring that
|
|
|
|
// out, so inspecting SymbolFlags that might be present that are type only
|
|
|
|
.filter(
|
2020-03-28 13:03:49 -04:00
|
|
|
(sym) =>
|
2020-02-12 16:41:51 -05:00
|
|
|
sym.flags & ts.SymbolFlags.Class ||
|
2019-11-20 11:02:08 -05:00
|
|
|
!(
|
|
|
|
sym.flags & ts.SymbolFlags.Interface ||
|
|
|
|
sym.flags & ts.SymbolFlags.TypeLiteral ||
|
|
|
|
sym.flags & ts.SymbolFlags.Signature ||
|
|
|
|
sym.flags & ts.SymbolFlags.TypeParameter ||
|
|
|
|
sym.flags & ts.SymbolFlags.TypeAlias ||
|
|
|
|
sym.flags & ts.SymbolFlags.Type ||
|
|
|
|
sym.flags & ts.SymbolFlags.Namespace ||
|
|
|
|
sym.flags & ts.SymbolFlags.InterfaceExcludes ||
|
|
|
|
sym.flags & ts.SymbolFlags.TypeParameterExcludes ||
|
|
|
|
sym.flags & ts.SymbolFlags.TypeAliasExcludes
|
|
|
|
)
|
|
|
|
)
|
2020-03-28 13:03:49 -04:00
|
|
|
.map((sym) => sym.getName());
|
2019-11-20 11:02:08 -05:00
|
|
|
}
|