1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

Bundle most types into globals.d.ts (#642)

This commit is contained in:
Kitson Kelly 2018-09-01 07:45:26 -07:00 committed by Ryan Dahl
parent 89dddca3b4
commit f83aee02e6
12 changed files with 50 additions and 78 deletions

View file

@ -186,23 +186,23 @@ executable("snapshot_creator") {
run_node("gen_declarations") { run_node("gen_declarations") {
out_dir = target_gen_dir out_dir = target_gen_dir
sources = [ sources = [
"js/assets.ts",
"js/compiler.ts", "js/compiler.ts",
"js/console.ts", "js/console.ts",
"js/deno.ts", "js/deno.ts",
"js/fetch.ts",
"js/global-eval.ts",
"js/globals.ts", "js/globals.ts",
"js/os.ts", "js/os.ts",
"js/text_encoding.ts",
"js/timers.ts", "js/timers.ts",
"js/tsconfig.generated.json", "js/tsconfig.generated.json",
"js/types.ts",
"js/util.ts", "js/util.ts",
"js/v8_source_maps.ts"
] ]
outputs = [ outputs = [
out_dir + "/js/compiler.d.ts", "$out_dir/types/globals.d.ts",
out_dir + "/js/console.d.ts",
out_dir + "/js/deno.d.ts",
out_dir + "/js/globals.d.ts",
out_dir + "/js/os.d.ts",
out_dir + "/js/timers.d.ts",
out_dir + "/js/util.d.ts",
] ]
deps = [ deps = [
":msg_ts", ":msg_ts",
@ -213,8 +213,8 @@ run_node("gen_declarations") {
rebase_path("js/tsconfig.generated.json", root_build_dir), rebase_path("js/tsconfig.generated.json", root_build_dir),
"--baseUrl", "--baseUrl",
rebase_path(root_build_dir, root_build_dir), rebase_path(root_build_dir, root_build_dir),
"--outDir", "--outFile",
rebase_path(out_dir, root_build_dir), rebase_path("$out_dir/types/globals.js", root_build_dir),
] ]
} }
@ -233,7 +233,7 @@ run_node("bundle") {
"js/plugins.d.ts", "js/plugins.d.ts",
"js/text_encoding.ts", "js/text_encoding.ts",
"js/timers.ts", "js/timers.ts",
"js/types.d.ts", "js/types.ts",
"js/util.ts", "js/util.ts",
"js/v8_source_maps.ts", "js/v8_source_maps.ts",
"rollup.config.js", "rollup.config.js",

View file

@ -1,21 +1,13 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license. // Copyright 2018 the Deno authors. All rights reserved. MIT license.
// tslint:disable-next-line:no-reference // tslint:disable-next-line:no-reference
/// <reference path="plugins.d.ts" /> /// <reference path="./plugins.d.ts" />
// There is a rollup plugin that will inline any module ending with `!string` // There is a rollup plugin that will inline any module ending with `!string`
// tslint:disable:max-line-length // tslint:disable:max-line-length
// Generated definitions // Generated definitions
import compilerDts from "gen/js/compiler.d.ts!string"; import globalsDts from "gen/types/globals.d.ts!string";
import consoleDts from "gen/js/console.d.ts!string";
import denoDts from "gen/js/deno.d.ts!string";
import libdenoDts from "gen/js/libdeno.d.ts!string";
import globalsDts from "gen/js/globals.d.ts!string";
import osDts from "gen/js/os.d.ts!string";
import fetchDts from "gen/js/fetch.d.ts!string";
import timersDts from "gen/js/timers.d.ts!string";
import utilDts from "gen/js/util.d.ts!string";
// Static libraries // Static libraries
import libEs2015Dts from "/third_party/node_modules/typescript/lib/lib.es2015.d.ts!string"; import libEs2015Dts from "/third_party/node_modules/typescript/lib/lib.es2015.d.ts!string";
@ -49,24 +41,16 @@ import libEsnextSymbolDts from "/third_party/node_modules/typescript/lib/lib.esn
import libGlobalsDts from "/js/lib.globals.d.ts!string"; import libGlobalsDts from "/js/lib.globals.d.ts!string";
// Static definitions // Static definitions
import typescriptDts from "/third_party/node_modules/typescript/lib/typescript.d.ts!string";
import typesDts from "/js/types.d.ts!string";
import fetchTypesDts from "/js/fetch_types.d.ts!string"; import fetchTypesDts from "/js/fetch_types.d.ts!string";
import flatbuffersDts from "/third_party/node_modules/@types/flatbuffers/index.d.ts!string";
import textEncodingDts from "/third_party/node_modules/@types/text-encoding/index.d.ts!string";
import typescriptDts from "/third_party/node_modules/typescript/lib/typescript.d.ts!string";
// tslint:enable:max-line-length // tslint:enable:max-line-length
// prettier-ignore // prettier-ignore
export const assetSourceCode: { [key: string]: string } = { export const assetSourceCode: { [key: string]: string } = {
// Generated definitions // Generated definitions
"compiler.d.ts": compilerDts,
"console.d.ts": consoleDts,
"deno.d.ts": denoDts,
"libdeno.d.ts": libdenoDts,
"globals.d.ts": globalsDts, "globals.d.ts": globalsDts,
"os.d.ts": osDts,
"fetch.d.ts": fetchDts,
"fetch_types.d.ts": fetchTypesDts,
"timers.d.ts": timersDts,
"util.d.ts": utilDts,
// Static libraries // Static libraries
"lib.es2015.collection.d.ts": libEs2015CollectionDts, "lib.es2015.collection.d.ts": libEs2015CollectionDts,
@ -100,9 +84,8 @@ export const assetSourceCode: { [key: string]: string } = {
"lib.globals.d.ts": libGlobalsDts, "lib.globals.d.ts": libGlobalsDts,
// Static definitions // Static definitions
"fetch-types.d.ts": fetchTypesDts,
"flatbuffers.d.ts": flatbuffersDts,
"text-encoding.d.ts": textEncodingDts,
"typescript.d.ts": typescriptDts, "typescript.d.ts": typescriptDts,
"types.d.ts": typesDts,
// TODO Remove.
"msg_generated.d.ts": "",
}; };

View file

@ -1,4 +1,5 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license. // Copyright 2018 the Deno authors. All rights reserved. MIT license.
/// <amd-module name="compiler"/>
import * as ts from "typescript"; import * as ts from "typescript";
import { assetSourceCode } from "./assets"; import { assetSourceCode } from "./assets";
import * as deno from "./deno"; import * as deno from "./deno";
@ -669,10 +670,9 @@ export class DenoCompiler implements ts.LanguageServiceHost {
this._log("resolveModuleNames()", { moduleNames, containingFile }); this._log("resolveModuleNames()", { moduleNames, containingFile });
return moduleNames.map(name => { return moduleNames.map(name => {
let resolvedFileName; let resolvedFileName;
if (name === "deno") { if (name === "deno" || name === "compiler") {
resolvedFileName = this.resolveModuleName("deno.d.ts", ASSETS); // builtin modules are part of `globals.d.ts`
} else if (name === "compiler") { resolvedFileName = this.resolveModuleName("globals.d.ts", ASSETS);
resolvedFileName = this.resolveModuleName("compiler.d.ts", ASSETS);
} else if (name === "typescript") { } else if (name === "typescript") {
resolvedFileName = this.resolveModuleName("typescript.d.ts", ASSETS); resolvedFileName = this.resolveModuleName("typescript.d.ts", ASSETS);
} else { } else {

View file

@ -474,7 +474,7 @@ test(function compilerFileExists() {
"/root/project" "/root/project"
); );
assert(compilerInstance.fileExists(moduleMetaData.fileName)); assert(compilerInstance.fileExists(moduleMetaData.fileName));
assert(compilerInstance.fileExists("$asset$/compiler.d.ts")); assert(compilerInstance.fileExists("$asset$/globals.d.ts"));
assertEqual( assertEqual(
compilerInstance.fileExists("/root/project/unknown-module.ts"), compilerInstance.fileExists("/root/project/unknown-module.ts"),
false false
@ -493,7 +493,7 @@ test(function compilerResolveModuleNames() {
["/root/project/foo/bar.ts", false], ["/root/project/foo/bar.ts", false],
["/root/project/foo/baz.ts", false], ["/root/project/foo/baz.ts", false],
["$asset$/lib.globals.d.ts", true], ["$asset$/lib.globals.d.ts", true],
["$asset$/deno.d.ts", true] ["$asset$/globals.d.ts", true]
]; ];
for (let i = 0; i < results.length; i++) { for (let i = 0; i < results.length; i++) {
const result = results[i]; const result = results[i];

View file

@ -1,5 +1,6 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license. // Copyright 2018 the Deno authors. All rights reserved. MIT license.
// Public deno module. // Public deno module.
/// <amd-module name="deno"/>
export { export {
env, env,
exit, exit,

View file

@ -2,7 +2,7 @@
import { Console } from "./console"; import { Console } from "./console";
import * as timers from "./timers"; import * as timers from "./timers";
import { TextDecoder, TextEncoder } from "./text_encoding"; import * as textEncoding from "./text_encoding";
import * as fetch_ from "./fetch"; import * as fetch_ from "./fetch";
import { libdeno } from "./libdeno"; import { libdeno } from "./libdeno";
import { globalEval } from "./global-eval"; import { globalEval } from "./global-eval";
@ -24,8 +24,8 @@ declare global {
const fetch: typeof fetch_.fetch; const fetch: typeof fetch_.fetch;
// tslint:disable:variable-name // tslint:disable:variable-name
let TextEncoder: TextEncoder; let TextEncoder: typeof textEncoding.TextEncoder;
let TextDecoder: TextDecoder; let TextDecoder: typeof textEncoding.TextDecoder;
// tslint:enable:variable-name // tslint:enable:variable-name
} }
@ -41,7 +41,7 @@ window.clearTimeout = timers.clearTimer;
window.clearInterval = timers.clearTimer; window.clearInterval = timers.clearTimer;
window.console = new Console(libdeno.print); window.console = new Console(libdeno.print);
window.TextEncoder = TextEncoder; window.TextEncoder = textEncoding.TextEncoder;
window.TextDecoder = TextDecoder; window.TextDecoder = textEncoding.TextDecoder;
window.fetch = fetch_.fetch; window.fetch = fetch_.fetch;

2
js/lib.globals.d.ts vendored
View file

@ -2,4 +2,4 @@
// This file contains the default TypeScript libraries for the deno runtime. // This file contains the default TypeScript libraries for the deno runtime.
/// <reference no-default-lib="true"/> /// <reference no-default-lib="true"/>
/// <reference lib="esnext" /> /// <reference lib="esnext" />
import "gen/js/globals"; /// <reference path="globals.d.ts"/>

View file

@ -128,8 +128,8 @@ export function readFileSync(filename: string): Uint8Array {
return new Uint8Array(dataArray!); return new Uint8Array(dataArray!);
} }
function createEnv(_msg: fbs.EnvironRes): { [index:string]: string } { function createEnv(_msg: fbs.EnvironRes): { [index: string]: string } {
const env: { [index:string]: string } = {}; const env: { [index: string]: string } = {};
for (let i = 0; i < _msg.mapLength(); i++) { for (let i = 0; i < _msg.mapLength(); i++) {
const item = _msg.map(i)!; const item = _msg.map(i)!;
@ -169,7 +169,7 @@ function setEnv(key: string, value: string): void {
* const newEnv = deno.env(); * const newEnv = deno.env();
* console.log(env.TEST_VAR == newEnv.TEST_VAR); * console.log(env.TEST_VAR == newEnv.TEST_VAR);
*/ */
export function env(): { [index:string]: string } { export function env(): { [index: string]: string } {
/* Ideally we could write /* Ideally we could write
const res = send({ const res = send({
command: fbs.Command.ENV, command: fbs.Command.ENV,

View file

@ -7,6 +7,7 @@
"compilerOptions": { "compilerOptions": {
"declaration": true, "declaration": true,
"emitDeclarationOnly": true, "emitDeclarationOnly": true,
"module": "amd",
"stripInternal": true "stripInternal": true
}, },
"files": [ "files": [

View file

@ -8,6 +8,7 @@ export interface ModuleInfo {
outputCode: string | null; outputCode: string | null;
} }
// tslint:disable:max-line-length
// Following definitions adapted from: // Following definitions adapted from:
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/index.d.ts // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/index.d.ts
// Type definitions for Node.js 10.3.x // Type definitions for Node.js 10.3.x
@ -36,11 +37,13 @@ export interface ModuleInfo {
// Alexander T. <https://github.com/a-tarasyuk> // Alexander T. <https://github.com/a-tarasyuk>
// Lishude <https://github.com/islishude> // Lishude <https://github.com/islishude>
// Andrew Makarov <https://github.com/r3nya> // Andrew Makarov <https://github.com/r3nya>
// tslint:enable:max-line-length
export interface CallSite { export interface CallSite {
/** /**
* Value of "this" * Value of "this"
*/ */
// tslint:disable-next-line:no-any
getThis(): any; getThis(): any;
/** /**
@ -133,13 +136,16 @@ declare global {
// Declare "static" methods in Error // Declare "static" methods in Error
interface ErrorConstructor { interface ErrorConstructor {
/** Create .stack property on a target object */ /** Create .stack property on a target object */
captureStackTrace(targetObject: Object, constructorOpt?: Function): void; captureStackTrace(targetObject: object, constructorOpt?: Function): void;
// tslint:disable:max-line-length
/** /**
* Optional override for formatting stack traces * Optional override for formatting stack traces
* *
* @see https://github.com/v8/v8/wiki/Stack%20Trace%20API#customizing-stack-traces * @see https://github.com/v8/v8/wiki/Stack%20Trace%20API#customizing-stack-traces
*/ */
// tslint:enable:max-line-length
// tslint:disable-next-line:no-any
prepareStackTrace?: (err: Error, stackTraces: CallSite[]) => any; prepareStackTrace?: (err: Error, stackTraces: CallSite[]) => any;
stackTraceLimit: number; stackTraceLimit: number;

View file

@ -24,15 +24,6 @@ const tsconfigOverride = {
} }
}; };
// when the build path is not a child of the root of the project path
// TypeScript will output resources following the same path structure,
// `BASEPATH` will be a relative path to the root of the source project which
// we can use to determine what TypeScript would have output
const basePathParts = process.env.BASEPATH.split("/");
while (basePathParts[0] === "..") {
basePathParts.shift();
}
// this is a rollup plugin which will look for imports ending with `!string` and resolve // this is a rollup plugin which will look for imports ending with `!string` and resolve
// them with a module that will inline the contents of the file as a string. Needed to // them with a module that will inline the contents of the file as a string. Needed to
// support `js/assets.ts`. // support `js/assets.ts`.
@ -54,23 +45,13 @@ function strings({ include, exclude } = {}) {
// strip the `!string` from `importee` // strip the `!string` from `importee`
importee = importee.slice(0, importee.lastIndexOf("!string")); importee = importee.slice(0, importee.lastIndexOf("!string"));
if (!importee.startsWith("gen/")) { if (!importee.startsWith("gen/")) {
// this is a static asset which is located relative to the root of the source project // this is a static asset which is located relative to the root of
// the source project
return path.resolve(path.join(process.env.BASEPATH, importee)); return path.resolve(path.join(process.env.BASEPATH, importee));
} }
// ignoring the first part, which is "gen" // this is an asset which has been generated, therefore it will be
const [, ...importeeParts] = importee.split("/"); // located within the build path
// generated assets will be output by TypeScript relative between the build path and the return path.resolve(path.join(process.cwd(), importee));
// root of the project. For example on Travis, the project path is:
// /home/travis/build/denoland/deno/
// and the build path is:
// /home/travis/out/Default/
// TypeScript will then output `globals.d.ts` from `js/globals.ts` to:
// /home/travis/out/Default/gen/build/denoland/deno/js/globals.d.ts
// therefore we need to insert any non relative BASEPATH parts into
// the final module ID
return path.resolve(
path.join(process.cwd(), "gen", ...basePathParts, ...importeeParts)
);
} }
}, },
@ -96,7 +77,7 @@ function resolveGenerated() {
return { return {
name: "resolve-msg-generated", name: "resolve-msg-generated",
resolveId(importee) { resolveId(importee) {
if (importee.startsWith("gen/")) { if (importee.startsWith("gen/msg_generated")) {
const resolved = path.resolve( const resolved = path.resolve(
path.join(process.cwd(), `${importee}.ts`) path.join(process.cwd(), `${importee}.ts`)
); );

View file

@ -4,7 +4,7 @@
  ~~~~~~   ~~~~~~
$asset$/globals.d.tsILDCARD] $asset$/globals.d.tsILDCARD]
[WILDCARD][0m const console: Console; [WILDCARD][0m const console: Console;
   ~~~~~~~    ~~~~~~~
'console' is declared here. 'console' is declared here.