2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-12-13 13:45:53 -05:00
|
|
|
|
|
|
|
use deno_core::JsRuntime;
|
|
|
|
use deno_core::RuntimeOptions;
|
|
|
|
use std::env;
|
2020-12-14 12:42:40 -05:00
|
|
|
use std::path::Path;
|
2020-12-13 13:45:53 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-12-14 12:42:40 -05:00
|
|
|
// TODO(bartlomieju): this module contains a lot of duplicated
|
|
|
|
// logic with `cli/build.rs`, factor out to `deno_core`.
|
|
|
|
fn create_snapshot(
|
|
|
|
mut js_runtime: JsRuntime,
|
|
|
|
snapshot_path: &Path,
|
|
|
|
files: Vec<PathBuf>,
|
|
|
|
) {
|
2021-04-14 15:10:48 -04:00
|
|
|
// Initialization order matters.
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
deno_webidl::init(&mut js_runtime);
|
2021-03-12 15:23:59 -05:00
|
|
|
deno_console::init(&mut js_runtime);
|
2021-04-14 15:10:48 -04:00
|
|
|
deno_timers::init(&mut js_runtime);
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
deno_url::init(&mut js_runtime);
|
2020-12-14 12:42:40 -05:00
|
|
|
deno_web::init(&mut js_runtime);
|
2021-04-06 06:55:05 -04:00
|
|
|
deno_file::init(&mut js_runtime);
|
2020-12-14 12:42:40 -05:00
|
|
|
deno_fetch::init(&mut js_runtime);
|
2021-01-06 10:57:28 -05:00
|
|
|
deno_websocket::init(&mut js_runtime);
|
2020-12-14 12:42:40 -05:00
|
|
|
deno_crypto::init(&mut js_runtime);
|
2021-03-01 05:31:13 -05:00
|
|
|
deno_webgpu::init(&mut js_runtime);
|
2020-12-14 12:42:40 -05:00
|
|
|
// TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the
|
|
|
|
// workspace root.
|
|
|
|
let display_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
|
|
|
|
for file in files {
|
|
|
|
println!("cargo:rerun-if-changed={}", file.display());
|
|
|
|
let display_path = file.strip_prefix(display_root).unwrap();
|
|
|
|
let display_path_str = display_path.display().to_string();
|
|
|
|
js_runtime
|
|
|
|
.execute(
|
|
|
|
&("deno:".to_string() + &display_path_str.replace('\\', "/")),
|
|
|
|
&std::fs::read_to_string(&file).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let snapshot = js_runtime.snapshot();
|
|
|
|
let snapshot_slice: &[u8] = &*snapshot;
|
|
|
|
println!("Snapshot size: {}", snapshot_slice.len());
|
|
|
|
std::fs::write(&snapshot_path, snapshot_slice).unwrap();
|
|
|
|
println!("Snapshot written to: {} ", snapshot_path.display());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<PathBuf>) {
|
|
|
|
let js_runtime = JsRuntime::new(RuntimeOptions {
|
|
|
|
will_snapshot: true,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
create_snapshot(js_runtime, snapshot_path, files);
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:45:53 -05:00
|
|
|
fn main() {
|
2020-12-14 09:15:16 -05:00
|
|
|
// Skip building from docs.rs.
|
|
|
|
if env::var_os("DOCS_RS").is_some() {
|
2020-12-13 13:45:53 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To debug snapshot issues uncomment:
|
|
|
|
// op_fetch_asset::trace_serializer();
|
|
|
|
|
|
|
|
println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
|
|
|
|
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
|
|
|
|
let o = PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
|
|
|
|
|
|
|
// Main snapshot
|
|
|
|
let runtime_snapshot_path = o.join("CLI_SNAPSHOT.bin");
|
|
|
|
|
2020-12-17 11:37:57 -05:00
|
|
|
let js_files = get_js_files("js");
|
2020-12-14 12:42:40 -05:00
|
|
|
create_runtime_snapshot(&runtime_snapshot_path, js_files);
|
|
|
|
}
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2020-12-14 12:42:40 -05:00
|
|
|
fn get_js_files(d: &str) -> Vec<PathBuf> {
|
|
|
|
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
let mut js_files = std::fs::read_dir(d)
|
|
|
|
.unwrap()
|
|
|
|
.map(|dir_entry| {
|
|
|
|
let file = dir_entry.unwrap();
|
|
|
|
manifest_dir.join(file.path())
|
|
|
|
})
|
|
|
|
.filter(|path| path.extension().unwrap_or_default() == "js")
|
|
|
|
.collect::<Vec<PathBuf>>();
|
|
|
|
js_files.sort();
|
|
|
|
js_files
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|