Skip to content

Commit

Permalink
Reduce size of TypeScript Compiler snapshot (denoland#6809)
Browse files Browse the repository at this point in the history
This PR is intentionally ugly. It duplicates all of the code in cli/js2/ into
cli/tsc/  ... because it's very important that we all understand that this code
is unnecessarily duplicated in our binary. I hope this ugliness provides the
motivation to clean it up.

The typescript git submodule is removed, because it's a very large repo and
contains all sorts of stuff we don't need. Instead the necessary files are
copied directly into the deno repo. Hence +200k lines.

COMPILER_SNAPSHOT.bin size
```
master         3448139
this branch    3320972
```

Fixes denoland#6812
  • Loading branch information
ry committed Jul 22, 2020
1 parent 9d13b53 commit bf99300
Show file tree
Hide file tree
Showing 94 changed files with 188,572 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .dprintrc.json
Expand Up @@ -12,7 +12,8 @@
"includes": ["**/*.{ts,tsx,js,jsx,json,md}"],
"excludes": [
".cargo_home",
"cli/typescript",
"cli/dts",
"cli/tsc/*typescript.js",
"gh-pages",
"std/**/testdata",
"std/**/vendor",
Expand Down
2 changes: 2 additions & 0 deletions .eslintignore
Expand Up @@ -3,3 +3,5 @@ cli/tests/error_syntax.js
std/deno.d.ts
std/**/testdata/
std/**/node_modules/
cli/tsc/*typescript.js
cli/dts/*
5 changes: 0 additions & 5 deletions .gitmodules
@@ -1,8 +1,3 @@
[submodule "deno_third_party"]
path = third_party
url = https://github.com/denoland/deno_third_party.git
[submodule "typescript"]
path = cli/typescript
url = https://github.com/microsoft/TypeScript.git
fetchRecurseSubmodules = false
shallow = true
2 changes: 0 additions & 2 deletions cli/Cargo.toml
Expand Up @@ -16,8 +16,6 @@ path = "main.rs"

[build-dependencies]
deno_core = { path = "../core", version = "0.49.0" }
serde = { version = "1.0.112", features = ["derive"] }
serde_json = { version = "1.0.55", features = [ "preserve_order" ] }

[target.'cfg(windows)'.build-dependencies]
winres = "0.1"
Expand Down
45 changes: 19 additions & 26 deletions cli/build.rs
Expand Up @@ -46,41 +46,34 @@ fn create_compiler_snapshot(
let mut custom_libs: HashMap<String, PathBuf> = HashMap::new();
custom_libs.insert(
"lib.deno.window.d.ts".to_string(),
cwd.join("js2/lib.deno.window.d.ts"),
cwd.join("dts/lib.deno.window.d.ts"),
);
custom_libs.insert(
"lib.deno.worker.d.ts".to_string(),
cwd.join("js2/lib.deno.worker.d.ts"),
cwd.join("dts/lib.deno.worker.d.ts"),
);
custom_libs.insert(
"lib.deno.shared_globals.d.ts".to_string(),
cwd.join("js2/lib.deno.shared_globals.d.ts"),
cwd.join("dts/lib.deno.shared_globals.d.ts"),
);
custom_libs.insert(
"lib.deno.ns.d.ts".to_string(),
cwd.join("js2/lib.deno.ns.d.ts"),
cwd.join("dts/lib.deno.ns.d.ts"),
);
custom_libs.insert(
"lib.deno.unstable.d.ts".to_string(),
cwd.join("js2/lib.deno.unstable.d.ts"),
cwd.join("dts/lib.deno.unstable.d.ts"),
);
runtime_isolate.register_op(
"op_fetch_asset",
op_fetch_asset::op_fetch_asset(custom_libs),
);

js_check(runtime_isolate.execute(
"typescript.js",
&std::fs::read_to_string("typescript/lib/typescript.js").unwrap(),
));

create_snapshot(runtime_isolate, snapshot_path, files);
}

fn ts_version() -> String {
let data = include_str!("typescript/package.json");
let pkg: serde_json::Value = serde_json::from_str(data).unwrap();
pkg["version"].as_str().unwrap().to_string()
// TODO(ry) This should be automatically extracted from typescript.js
"3.9.2".to_string()
}

fn main() {
Expand All @@ -106,26 +99,26 @@ fn main() {
let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin");
let compiler_snapshot_path = o.join("COMPILER_SNAPSHOT.bin");

let mut js_files = std::fs::read_dir("js2/")
let js_files = get_js_files("js2");
create_runtime_snapshot(&runtime_snapshot_path, js_files);

let js_files = get_js_files("tsc");
create_compiler_snapshot(&compiler_snapshot_path, js_files, &c);

set_binary_metadata();
}

fn get_js_files(d: &str) -> Vec<String> {
let mut js_files = std::fs::read_dir(d)
.unwrap()
.map(|dir_entry| {
let file = dir_entry.unwrap();
file.path().to_string_lossy().to_string()
})
.filter(|filename| filename.ends_with(".js"))
.collect::<Vec<String>>();

js_files.sort();

let runtime_files = js_files
.clone()
.into_iter()
.filter(|filepath| !filepath.ends_with("compiler.js"))
.collect::<Vec<String>>();
create_runtime_snapshot(&runtime_snapshot_path, runtime_files);
create_compiler_snapshot(&compiler_snapshot_path, js_files, &c);

set_binary_metadata();
js_files
}

#[cfg(target_os = "windows")]
Expand Down
24 changes: 24 additions & 0 deletions cli/dts/lib.d.ts
@@ -0,0 +1,24 @@
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */



/// <reference no-default-lib="true"/>


/// <reference lib="es5" />
/// <reference lib="dom" />
/// <reference lib="webworker.importscripts" />
/// <reference lib="scripthost" />
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit bf99300

Please sign in to comment.