1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -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") {
out_dir = target_gen_dir
sources = [
"js/assets.ts",
"js/compiler.ts",
"js/console.ts",
"js/deno.ts",
"js/fetch.ts",
"js/global-eval.ts",
"js/globals.ts",
"js/os.ts",
"js/text_encoding.ts",
"js/timers.ts",
"js/tsconfig.generated.json",
"js/types.ts",
"js/util.ts",
"js/v8_source_maps.ts"
]
outputs = [
out_dir + "/js/compiler.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",
"$out_dir/types/globals.d.ts",
]
deps = [
":msg_ts",
@ -213,8 +213,8 @@ run_node("gen_declarations") {
rebase_path("js/tsconfig.generated.json", root_build_dir),
"--baseUrl",
rebase_path(root_build_dir, root_build_dir),
"--outDir",
rebase_path(out_dir, root_build_dir),
"--outFile",
rebase_path("$out_dir/types/globals.js", root_build_dir),
]
}
@ -233,7 +233,7 @@ run_node("bundle") {
"js/plugins.d.ts",
"js/text_encoding.ts",
"js/timers.ts",
"js/types.d.ts",
"js/types.ts",
"js/util.ts",
"js/v8_source_maps.ts",
"rollup.config.js",

View file

@ -1,21 +1,13 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
// 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`
// tslint:disable:max-line-length
// Generated definitions
import compilerDts from "gen/js/compiler.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";
import globalsDts from "gen/types/globals.d.ts!string";
// Static libraries
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";
// 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 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
// prettier-ignore
export const assetSourceCode: { [key: string]: string } = {
// Generated definitions
"compiler.d.ts": compilerDts,
"console.d.ts": consoleDts,
"deno.d.ts": denoDts,
"libdeno.d.ts": libdenoDts,
"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
"lib.es2015.collection.d.ts": libEs2015CollectionDts,
@ -100,9 +84,8 @@ export const assetSourceCode: { [key: string]: string } = {
"lib.globals.d.ts": libGlobalsDts,
// Static definitions
"fetch-types.d.ts": fetchTypesDts,
"flatbuffers.d.ts": flatbuffersDts,
"text-encoding.d.ts": textEncodingDts,
"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.
/// <amd-module name="compiler"/>
import * as ts from "typescript";
import { assetSourceCode } from "./assets";
import * as deno from "./deno";
@ -669,10 +670,9 @@ export class DenoCompiler implements ts.LanguageServiceHost {
this._log("resolveModuleNames()", { moduleNames, containingFile });
return moduleNames.map(name => {
let resolvedFileName;
if (name === "deno") {
resolvedFileName = this.resolveModuleName("deno.d.ts", ASSETS);
} else if (name === "compiler") {
resolvedFileName = this.resolveModuleName("compiler.d.ts", ASSETS);
if (name === "deno" || name === "compiler") {
// builtin modules are part of `globals.d.ts`
resolvedFileName = this.resolveModuleName("globals.d.ts", ASSETS);
} else if (name === "typescript") {
resolvedFileName = this.resolveModuleName("typescript.d.ts", ASSETS);
} else {

View file

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

View file

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

View file

@ -2,7 +2,7 @@
import { Console } from "./console";
import * as timers from "./timers";
import { TextDecoder, TextEncoder } from "./text_encoding";
import * as textEncoding from "./text_encoding";
import * as fetch_ from "./fetch";
import { libdeno } from "./libdeno";
import { globalEval } from "./global-eval";
@ -24,8 +24,8 @@ declare global {
const fetch: typeof fetch_.fetch;
// tslint:disable:variable-name
let TextEncoder: TextEncoder;
let TextDecoder: TextDecoder;
let TextEncoder: typeof textEncoding.TextEncoder;
let TextDecoder: typeof textEncoding.TextDecoder;
// tslint:enable:variable-name
}
@ -41,7 +41,7 @@ window.clearTimeout = timers.clearTimer;
window.clearInterval = timers.clearTimer;
window.console = new Console(libdeno.print);
window.TextEncoder = TextEncoder;
window.TextDecoder = TextDecoder;
window.TextEncoder = textEncoding.TextEncoder;
window.TextDecoder = textEncoding.TextDecoder;
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.
/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
import "gen/js/globals";
/// <reference path="globals.d.ts"/>

View file

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

View file

@ -8,6 +8,7 @@ export interface ModuleInfo {
outputCode: string | null;
}
// tslint:disable:max-line-length
// Following definitions adapted from:
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/index.d.ts
// Type definitions for Node.js 10.3.x
@ -36,11 +37,13 @@ export interface ModuleInfo {
// Alexander T. <https://github.com/a-tarasyuk>
// Lishude <https://github.com/islishude>
// Andrew Makarov <https://github.com/r3nya>
// tslint:enable:max-line-length
export interface CallSite {
/**
* Value of "this"
*/
// tslint:disable-next-line:no-any
getThis(): any;
/**
@ -133,13 +136,16 @@ declare global {
// Declare "static" methods in Error
interface ErrorConstructor {
/** 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
*
* @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;
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
// them with a module that will inline the contents of the file as a string. Needed to
// support `js/assets.ts`.
@ -54,23 +45,13 @@ function strings({ include, exclude } = {}) {
// strip the `!string` from `importee`
importee = importee.slice(0, importee.lastIndexOf("!string"));
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));
}
// ignoring the first part, which is "gen"
const [, ...importeeParts] = importee.split("/");
// generated assets will be output by TypeScript relative between the build path and the
// 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)
);
// this is an asset which has been generated, therefore it will be
// located within the build path
return path.resolve(path.join(process.cwd(), importee));
}
},
@ -96,7 +77,7 @@ function resolveGenerated() {
return {
name: "resolve-msg-generated",
resolveId(importee) {
if (importee.startsWith("gen/")) {
if (importee.startsWith("gen/msg_generated")) {
const resolved = path.resolve(
path.join(process.cwd(), `${importee}.ts`)
);