summaryrefslogtreecommitdiff
path: root/cli/js/compiler.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2020-06-18 09:06:48 -0400
committerGitHub <noreply@github.com>2020-06-18 09:06:48 -0400
commita2969ecd27645bafc7195baa7cfecbebfd8d2bf4 (patch)
treed4bfd2fa7473b9bbfb5fa25eee4dc7238f040ddf /cli/js/compiler.ts
parent940f8e8433ae5ec74b2642438849089a0433e512 (diff)
Deno.bundle supports targets < ES2017 (#6346)
This commit provides a "system_loader_es5.js" bundle loader which will be added to the bundle when the target is < ES2017, which is the minimum target syntax required for "system_loader.js". Supports #5913 (via Deno.bundle()) with a couple caveats: * Allowing "deno bundle" to take a different target is not supported, as we specifically ignore "target" when passed in a TypeScript config file. This is because deno bundle is really intended to generate bundles that work in Deno. It is an unintentional side effect that some bundles are loadable in browsers. * While a target of "es3" will be accepted, the module loader will still only be compatible with ES5 or later. Realistically no one should be expecting bundles generated by Deno to be used on IE8 and prior, and there is just too much "baggage" to support that at this point. This is a minor variation of 75bb9d, which exposed some sort of internal V8 bug. Ref #6358 This is 100% authored by Kitson Kelly. Github might change the author when landing so I'm leaving this in: Co-authored-by: Kitson Kelly <me@kitsonkelly.com>
Diffstat (limited to 'cli/js/compiler.ts')
-rw-r--r--cli/js/compiler.ts34
1 files changed, 27 insertions, 7 deletions
diff --git a/cli/js/compiler.ts b/cli/js/compiler.ts
index abe145da2..46dbfcaf9 100644
--- a/cli/js/compiler.ts
+++ b/cli/js/compiler.ts
@@ -307,6 +307,10 @@ class Host implements ts.CompilerHost {
}
}
+ get options(): ts.CompilerOptions {
+ return this.#options;
+ }
+
configure(
cwd: string,
path: string,
@@ -528,6 +532,7 @@ const _TS_SNAPSHOT_PROGRAM = ts.createProgram({
// This function is called only during snapshotting process
const SYSTEM_LOADER = getAsset("system_loader.js");
+const SYSTEM_LOADER_ES5 = getAsset("system_loader_es5.js");
function buildLocalSourceFileCache(
sourceFileMap: Record<string, SourceFileMapEntry>
@@ -683,7 +688,12 @@ function createBundleWriteFile(state: WriteFileState): WriteFileCallback {
assert(state.bundle);
// we only support single root names for bundles
assert(state.rootNames.length === 1);
- state.bundleOutput = buildBundle(state.rootNames[0], data, sourceFiles);
+ state.bundleOutput = buildBundle(
+ state.rootNames[0],
+ data,
+ sourceFiles,
+ state.host.options.target ?? ts.ScriptTarget.ESNext
+ );
};
}
@@ -949,7 +959,8 @@ function normalizeUrl(rootName: string): string {
function buildBundle(
rootName: string,
data: string,
- sourceFiles: readonly ts.SourceFile[]
+ sourceFiles: readonly ts.SourceFile[],
+ target: ts.ScriptTarget
): string {
// 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
@@ -967,8 +978,8 @@ function buildBundle(
let instantiate: string;
if (rootExports && rootExports.length) {
instantiate = hasTla
- ? `const __exp = await __instantiateAsync("${rootName}");\n`
- : `const __exp = __instantiate("${rootName}");\n`;
+ ? `const __exp = await __instantiate("${rootName}", true);\n`
+ : `const __exp = __instantiate("${rootName}", false);\n`;
for (const rootExport of rootExports) {
if (rootExport === "default") {
instantiate += `export default __exp["${rootExport}"];\n`;
@@ -978,10 +989,19 @@ function buildBundle(
}
} else {
instantiate = hasTla
- ? `await __instantiateAsync("${rootName}");\n`
- : `__instantiate("${rootName}");\n`;
+ ? `await __instantiate("${rootName}", true);\n`
+ : `__instantiate("${rootName}", false);\n`;
}
- return `${SYSTEM_LOADER}\n${data}\n${instantiate}`;
+ const es5Bundle =
+ target === ts.ScriptTarget.ES3 ||
+ target === ts.ScriptTarget.ES5 ||
+ target === ts.ScriptTarget.ES2015 ||
+ target === ts.ScriptTarget.ES2016
+ ? true
+ : false;
+ return `${
+ es5Bundle ? SYSTEM_LOADER_ES5 : SYSTEM_LOADER
+ }\n${data}\n${instantiate}`;
}
function setRootExports(program: ts.Program, rootModule: string): void {