Skip to content

Commit

Permalink
Replace globals.d.ts with lib.deno_runtime.d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk authored and ry committed Oct 12, 2018
1 parent fda7aaa commit ec402c6
Show file tree
Hide file tree
Showing 23 changed files with 1,183 additions and 100 deletions.
28 changes: 17 additions & 11 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ ts_sources = [
"js/v8_source_maps.ts",
"js/write_file.ts",

"js/tsconfig.declarations.json",
"tsconfig.json",

# Listing package.json and yarn.lock as sources ensures the bundle is rebuilt
Expand Down Expand Up @@ -246,26 +245,33 @@ executable("snapshot_creator") {
configs += [ ":deno_config" ]
}

# Generates type declarations for files that need to be included
# in the runtime bundle
run_node("gen_declarations") {
# Generates the core TypeScript type library for deno that will be
# included in the runtime bundle
run_node("deno_runtime_declaration") {
out_dir = target_gen_dir
sources = ts_sources
outputs = [
"$out_dir/types/globals.d.ts",
"$out_dir/lib/lib.deno_runtime.d.ts",
]
deps = [
":msg_ts",
]
args = [
"./node_modules/typescript/bin/tsc",
"-p",
rebase_path("js/tsconfig.declarations.json", root_build_dir),
"--baseUrl",
rebase_path("node_modules/.bin/ts-node", root_build_dir),
"--project",
rebase_path("tools/ts_library_builder/tsconfig.json"),
rebase_path("tools/ts_library_builder/main.ts", root_build_dir),
"--basePath",
rebase_path(".", root_build_dir),
"--buildPath",
rebase_path(root_build_dir, root_build_dir),
"--outFile",
rebase_path("$out_dir/types/globals.js", root_build_dir),
rebase_path("$out_dir/lib/lib.deno_runtime.d.ts", root_build_dir),
"--silent",
]
if (is_debug) {
args += [ "--debug" ]
}
}

run_node("bundle") {
Expand All @@ -276,7 +282,7 @@ run_node("bundle") {
out_dir + "main.js.map",
]
deps = [
":gen_declarations",
":deno_runtime_declaration",
":msg_ts",
]
args = [
Expand Down
6 changes: 2 additions & 4 deletions js/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// tslint:disable:max-line-length

// Generated default library
import globalsDts from "gen/types/globals.d.ts!string";
import libDts from "gen/lib/lib.deno_runtime.d.ts!string";

// Static libraries
import libEs2015Dts from "/third_party/node_modules/typescript/lib/lib.es2015.d.ts!string";
Expand Down Expand Up @@ -40,15 +40,14 @@ import libEsnextIntlDts from "/third_party/node_modules/typescript/lib/lib.esnex
import libEsnextSymbolDts from "/third_party/node_modules/typescript/lib/lib.esnext.symbol.d.ts!string";

// Static definitions
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

// @internal
export const assetSourceCode: { [key: string]: string } = {
// Generated library
"globals.d.ts": globalsDts,
"lib.deno_runtime.d.ts": libDts,

// Static libraries
"lib.es2015.collection.d.ts": libEs2015CollectionDts,
Expand Down Expand Up @@ -81,7 +80,6 @@ export const assetSourceCode: { [key: string]: string } = {
"lib.esnext.symbol.d.ts": libEsnextSymbolDts,

// Static definitions
"flatbuffers.d.ts": flatbuffersDts,
"text-encoding.d.ts": textEncodingDts,
"typescript.d.ts": typescriptDts
};
7 changes: 4 additions & 3 deletions js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as sourceMaps from "./v8_source_maps";

const EOL = "\n";
const ASSETS = "$asset$";
const LIB_RUNTIME = "lib.deno_runtime.d.ts";

// tslint:disable:no-any
type AmdCallback = (...args: any[]) => void;
Expand Down Expand Up @@ -619,7 +620,7 @@ export class DenoCompiler

getDefaultLibFileName(): string {
this._log("getDefaultLibFileName()");
const moduleSpecifier = "globals.d.ts";
const moduleSpecifier = LIB_RUNTIME;
const moduleMetaData = this.resolveModule(moduleSpecifier, ASSETS);
return moduleMetaData.fileName;
}
Expand Down Expand Up @@ -649,8 +650,8 @@ export class DenoCompiler
return moduleNames.map(name => {
let resolvedFileName;
if (name === "deno") {
// builtin modules are part of `globals.d.ts`
resolvedFileName = this._resolveModuleName("globals.d.ts", ASSETS);
// builtin modules are part of the runtime lib
resolvedFileName = this._resolveModuleName(LIB_RUNTIME, ASSETS);
} else if (name === "typescript") {
resolvedFileName = this._resolveModuleName("typescript.d.ts", ASSETS);
} else {
Expand Down
9 changes: 6 additions & 3 deletions js/compiler_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ test(function compilerGetCurrentDirectory() {

test(function compilerGetDefaultLibFileName() {
setup();
assertEqual(compilerInstance.getDefaultLibFileName(), "$asset$/globals.d.ts");
assertEqual(
compilerInstance.getDefaultLibFileName(),
"$asset$/lib.deno_runtime.d.ts"
);
teardown();
});

Expand All @@ -572,7 +575,7 @@ test(function compilerFileExists() {
"/root/project"
);
assert(compilerInstance.fileExists(moduleMetaData.fileName));
assert(compilerInstance.fileExists("$asset$/globals.d.ts"));
assert(compilerInstance.fileExists("$asset$/lib.deno_runtime.d.ts"));
assertEqual(
compilerInstance.fileExists("/root/project/unknown-module.ts"),
false
Expand All @@ -590,7 +593,7 @@ test(function compilerResolveModuleNames() {
const fixtures: Array<[string, boolean]> = [
["/root/project/foo/bar.ts", false],
["/root/project/foo/baz.ts", false],
["$asset$/globals.d.ts", true]
["$asset$/lib.deno_runtime.d.ts", true]
];
for (let i = 0; i < results.length; i++) {
const result = results[i];
Expand Down
61 changes: 14 additions & 47 deletions js/globals.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,22 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.

import { Console } from "./console";
import * as timers from "./timers";
import * as textEncoding from "./text_encoding";
import * as blob from "./blob";
import * as console from "./console";
import * as fetch_ from "./fetch";
import { libdeno } from "./libdeno";
import { globalEval } from "./global_eval";
import { DenoHeaders } from "./fetch";
import { DenoBlob } from "./blob";

declare global {
interface Window {
define: Readonly<unknown>;

clearTimeout: typeof clearTimeout;
clearInterval: typeof clearInterval;
setTimeout: typeof setTimeout;
setInterval: typeof setInterval;

console: typeof console;
window: typeof window;

fetch: typeof fetch;

TextEncoder: typeof TextEncoder;
TextDecoder: typeof TextDecoder;
atob: typeof atob;
btoa: typeof btoa;
import { libdeno } from "./libdeno";
import * as textEncoding from "./text_encoding";
import * as timers from "./timers";

Headers: typeof Headers;
Blob: typeof Blob;
}
// During the build process, augmentations to the variable `window` in this
// file are tracked and created as part of default library that is built into
// deno, we only need to declare the enough to compile deno.

const clearTimeout: typeof timers.clearTimer;
const clearInterval: typeof timers.clearTimer;
declare global {
const console: console.Console;
const setTimeout: typeof timers.setTimeout;
const setInterval: typeof timers.setInterval;

const console: Console;
const window: Window;

const fetch: typeof fetch_.fetch;

// tslint:disable:variable-name
// tslint:disable-next-line:variable-name
const TextEncoder: typeof textEncoding.TextEncoder;
const TextDecoder: typeof textEncoding.TextDecoder;
const atob: typeof textEncoding.atob;
const btoa: typeof textEncoding.btoa;
const Headers: typeof DenoHeaders;
const Blob: typeof DenoBlob;
// tslint:enable:variable-name
}

// A reference to the global object.
Expand All @@ -61,13 +28,13 @@ window.setInterval = timers.setInterval;
window.clearTimeout = timers.clearTimer;
window.clearInterval = timers.clearTimer;

window.console = new Console(libdeno.print);
window.console = new console.Console(libdeno.print);
window.TextEncoder = textEncoding.TextEncoder;
window.TextDecoder = textEncoding.TextDecoder;
window.atob = textEncoding.atob;
window.btoa = textEncoding.btoa;

window.fetch = fetch_.fetch;

window.Headers = DenoHeaders;
window.Blob = DenoBlob;
window.Headers = fetch_.DenoHeaders;
window.Blob = blob.DenoBlob;
2 changes: 1 addition & 1 deletion js/testing/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
limitations under the License.
*/

export { assert, assertEqual, equal } from "./util.ts";
export { assert, assertEqual, equal } from "./util";

export type TestFunction = () => void | Promise<void>;

Expand Down
19 changes: 0 additions & 19 deletions js/tsconfig.declarations.json

This file was deleted.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"devDependencies": {
"@types/base64-js": "^1.2.5",
"@types/flatbuffers": "^1.9.0",
"@types/prettier": "^1.13.2",
"@types/source-map-support": "^0.4.1",
"@types/text-encoding": "0.0.33",
"base64-js": "^1.3.0",
Expand All @@ -20,6 +21,8 @@
"rollup-pluginutils": "^2.3.0",
"source-map-support": "^0.5.6",
"text-encoding": "0.6.4",
"ts-node": "^7.0.1",
"ts-simple-ast": "^16.0.4",
"tslint": "^5.10.0",
"tslint-eslint-rules": "^5.3.1",
"tslint-no-circular-imports": "^0.5.0",
Expand Down
10 changes: 1 addition & 9 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ const tsconfigOverride = {
}
};

// this is a preamble for the `globals.d.ts` file to allow it to be the default
// lib for deno.
const libPreamble = `/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
`;

// 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`.
Expand Down Expand Up @@ -70,9 +64,7 @@ function strings({ include, exclude } = {}) {
transform(code, id) {
if (filter(id)) {
return {
code: `export default ${JSON.stringify(
id.endsWith("globals.d.ts") ? libPreamble + code : code
)};`,
code: `export default ${JSON.stringify(code)};`,
map: { mappings: "" }
};
}
Expand Down
4 changes: 2 additions & 2 deletions tests/error_003_typescript.ts.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
[WILDCARD][0m consol.log("hello world!");
  ~~~~~~

$asset$/globals.d.tsILDCARD]
[WILDCARD]const console: Console;
$asset$/lib.deno_runtime.d.tsILDCARD]
[WILDCARD]const console: console.Console;
[WILDCARD]~~~~~~~
[WILDCARD]'console' is declared here.

2 changes: 1 addition & 1 deletion third_party
Submodule third_party updated 1539 files
1 change: 1 addition & 0 deletions tools/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
find_exts(".github/", ".md") +
find_exts("js/", ".js", ".ts", ".md") +
find_exts("tests/", ".js", ".ts", ".md") +
find_exts("tools/", ".js", ".json", ".ts", ".md") +
find_exts("website/", ".js", ".ts", ".md"))
# yapf: enable

Expand Down
Loading

0 comments on commit ec402c6

Please sign in to comment.