From 8d03397293b388317299dfb0648b541a7005807d Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Thu, 14 Nov 2019 02:35:56 +1100 Subject: Make bundles fully standalone (#3325) - Bundles are fully standalone. They now include the shared loader with `deno_typescript`. - Refactor of the loader in `deno_typescript` to perform module instantiation in a more - Change of behaviour when an output file is not specified on the CLI. Previously a default name was determined and the bundle written to that file, now the bundle will be sent to `stdout`. - Refactors in the TypeScript compiler to be able to support the concept of a request type. This provides a cleaner abstraction and makes it easier to support things like single module transpiles to the userland. - Remove a "dangerous" circular dependency between `os.ts` and `deno.ts`, and define `pid` and `noColor` in a better way. - Don't bind early to `console` in `repl.ts`. - Add an integration test for generating a bundle. --- std/bundle/utils.ts | 109 ---------------------------------------------------- 1 file changed, 109 deletions(-) delete mode 100644 std/bundle/utils.ts (limited to 'std/bundle/utils.ts') diff --git a/std/bundle/utils.ts b/std/bundle/utils.ts deleted file mode 100644 index 062c62231..000000000 --- a/std/bundle/utils.ts +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. - -import { assert } from "../testing/asserts.ts"; -import { exists } from "../fs/exists.ts"; - -export interface DefineFactory { - /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ - (...args: any): object | void; -} - -export interface ModuleMetaData { - dependencies: string[]; - factory?: DefineFactory | object; - exports: object; -} - -type Define = ( - id: string, - dependencies: string[], - factory: DefineFactory -) => void; - -/* eslint-disable @typescript-eslint/no-namespace */ -declare global { - namespace globalThis { - // eslint-disable-next-line no-var - var define: Define | undefined; - } -} -/* eslint-enable @typescript-eslint/no-namespace */ - -/** Evaluate the bundle, returning a queue of module IDs and their data to - * instantiate. - */ -export function evaluate( - text: string -): [string[], Map] { - const queue: string[] = []; - const modules = new Map(); - - globalThis.define = function define( - id: string, - dependencies: string[], - factory: DefineFactory - ): void { - modules.set(id, { - dependencies, - factory, - exports: {} - }); - queue.push(id); - }; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (Deno as any).core.evalContext(text); - // Deleting `define()` so it isn't accidentally there when the modules - // instantiate. - delete globalThis.define; - - return [queue, modules]; -} - -/** Drain the queue of module IDs while instantiating the modules. */ -export function instantiate( - queue: string[], - modules: Map -): void { - let id: string | undefined; - while ((id = queue.shift())) { - const module = modules.get(id)!; - assert(module != null); - assert(module.factory != null); - - const dependencies = module.dependencies.map( - (id): object => { - if (id === "require") { - // TODO(kitsonk) support dynamic import by passing a `require()` that - // can return a local module or dynamically import one. - return (): void => {}; - } else if (id === "exports") { - return module.exports; - } - const dep = modules.get(id)!; - assert(dep != null); - return dep.exports; - } - ); - - if (typeof module.factory === "function") { - module.factory!(...dependencies); - } else if (module.factory) { - // when bundling JSON, TypeScript just emits it as an object/array as the - // third argument of the `define()`. - module.exports = module.factory; - } - delete module.factory; - } -} - -/** Load the bundle and return the contents asynchronously. */ -export async function load(args: string[]): Promise { - // TODO(kitsonk) allow loading of remote bundles via fetch. - assert(args.length >= 2, "Expected at least two arguments."); - const [, bundleFileName] = args; - assert( - await exists(bundleFileName), - `Expected "${bundleFileName}" to exist.` - ); - return new TextDecoder().decode(await Deno.readFile(bundleFileName)); -} -- cgit v1.2.3