2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-02-01 06:02:23 -05:00
|
|
|
use deno_core::include_crate_modules;
|
2020-01-22 17:58:13 -05:00
|
|
|
use deno_core::CoreOp;
|
|
|
|
use deno_core::Isolate;
|
|
|
|
use deno_core::Op;
|
|
|
|
use deno_core::StartupData;
|
2020-01-24 15:10:49 -05:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-01-22 17:58:13 -05:00
|
|
|
use std::collections::HashMap;
|
2019-09-02 17:07:11 -04:00
|
|
|
use std::env;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2020-01-22 17:58:13 -05:00
|
|
|
fn op_fetch_asset(
|
|
|
|
custom_assets: HashMap<String, PathBuf>,
|
2020-01-24 15:10:49 -05:00
|
|
|
) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp {
|
|
|
|
move |control: &[u8], zero_copy_buf: Option<ZeroCopyBuf>| -> CoreOp {
|
2020-01-22 17:58:13 -05:00
|
|
|
assert!(zero_copy_buf.is_none()); // zero_copy_buf unused in this op.
|
|
|
|
let custom_assets = custom_assets.clone();
|
|
|
|
let name = std::str::from_utf8(control).unwrap();
|
|
|
|
|
|
|
|
let asset_code = if let Some(source_code) = deno_typescript::get_asset(name)
|
|
|
|
{
|
|
|
|
source_code.to_string()
|
|
|
|
} else if let Some(asset_path) = custom_assets.get(name) {
|
|
|
|
let source_code_vec =
|
|
|
|
std::fs::read(&asset_path).expect("Asset not found");
|
|
|
|
let source_code = std::str::from_utf8(&source_code_vec).unwrap();
|
|
|
|
source_code.to_string()
|
|
|
|
} else {
|
|
|
|
panic!("op_fetch_asset bad asset {}", name)
|
|
|
|
};
|
|
|
|
|
|
|
|
let vec = asset_code.into_bytes();
|
|
|
|
Op::Sync(vec.into_boxed_slice())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 17:07:11 -04:00
|
|
|
fn main() {
|
2020-01-30 03:32:20 -05:00
|
|
|
// Don't build V8 if "cargo doc" is being run. This is to support docs.rs.
|
|
|
|
if env::var_os("RUSTDOCFLAGS").is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-02 17:07:11 -04:00
|
|
|
// To debug snapshot issues uncomment:
|
|
|
|
// deno_typescript::trace_serializer();
|
|
|
|
|
2019-09-15 18:36:27 -04:00
|
|
|
println!(
|
|
|
|
"cargo:rustc-env=TS_VERSION={}",
|
|
|
|
deno_typescript::ts_version()
|
|
|
|
);
|
|
|
|
|
2020-02-01 06:02:23 -05:00
|
|
|
let extern_crate_modules = include_crate_modules![deno_core];
|
|
|
|
|
2020-01-31 12:43:08 -05:00
|
|
|
// The generation of snapshots is slow and often unnecessary. Until we figure
|
|
|
|
// out how to speed it up, or avoid it when unnecessary, this env var provides
|
|
|
|
// an escape hatch for the impatient hacker in need of faster incremental
|
|
|
|
// builds.
|
|
|
|
// USE WITH EXTREME CAUTION
|
|
|
|
if env::var_os("NO_BUILD_SNAPSHOTS").is_some() {
|
|
|
|
println!("NO_BUILD_SNAPSHOTS is set, skipping snapshot building.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-02 17:07:11 -04:00
|
|
|
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
let o = PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
|
|
|
|
2020-01-22 17:58:13 -05:00
|
|
|
// Main snapshot
|
2019-10-04 20:28:51 -04:00
|
|
|
let root_names = vec![c.join("js/main.ts")];
|
2020-01-22 17:58:13 -05:00
|
|
|
let bundle_path = o.join("CLI_SNAPSHOT.js");
|
|
|
|
let snapshot_path = o.join("CLI_SNAPSHOT.bin");
|
|
|
|
|
2020-02-01 06:02:23 -05:00
|
|
|
let main_module_name = deno_typescript::compile_bundle(
|
|
|
|
&bundle_path,
|
|
|
|
root_names,
|
|
|
|
Some(extern_crate_modules.clone()),
|
|
|
|
)
|
|
|
|
.expect("Bundle compilation failed");
|
2020-01-22 17:58:13 -05:00
|
|
|
assert!(bundle_path.exists());
|
|
|
|
|
|
|
|
let runtime_isolate = &mut Isolate::new(StartupData::None, true);
|
|
|
|
|
|
|
|
deno_typescript::mksnapshot_bundle(
|
|
|
|
runtime_isolate,
|
|
|
|
&snapshot_path,
|
|
|
|
&bundle_path,
|
|
|
|
&main_module_name,
|
|
|
|
)
|
|
|
|
.expect("Failed to create snapshot");
|
|
|
|
|
|
|
|
// Compiler snapshot
|
|
|
|
let root_names = vec![c.join("js/compiler.ts")];
|
|
|
|
let bundle_path = o.join("COMPILER_SNAPSHOT.js");
|
|
|
|
let snapshot_path = o.join("COMPILER_SNAPSHOT.bin");
|
|
|
|
let mut custom_libs: HashMap<String, PathBuf> = HashMap::new();
|
|
|
|
custom_libs.insert(
|
2020-01-29 12:54:23 -05:00
|
|
|
"lib.deno.window.d.ts".to_string(),
|
|
|
|
c.join("js/lib.deno.window.d.ts"),
|
2020-01-22 17:58:13 -05:00
|
|
|
);
|
2020-01-24 14:15:01 -05:00
|
|
|
custom_libs.insert(
|
2020-01-29 12:54:23 -05:00
|
|
|
"lib.deno.worker.d.ts".to_string(),
|
|
|
|
c.join("js/lib.deno.worker.d.ts"),
|
|
|
|
);
|
|
|
|
custom_libs.insert(
|
|
|
|
"lib.deno.shared_globals.d.ts".to_string(),
|
|
|
|
c.join("js/lib.deno.shared_globals.d.ts"),
|
|
|
|
);
|
|
|
|
custom_libs.insert(
|
|
|
|
"lib.deno.ns.d.ts".to_string(),
|
|
|
|
c.join("js/lib.deno.ns.d.ts"),
|
2020-01-24 14:15:01 -05:00
|
|
|
);
|
2020-01-22 14:18:01 -05:00
|
|
|
|
2020-02-01 06:02:23 -05:00
|
|
|
let main_module_name = deno_typescript::compile_bundle(
|
|
|
|
&bundle_path,
|
|
|
|
root_names,
|
|
|
|
Some(extern_crate_modules),
|
|
|
|
)
|
|
|
|
.expect("Bundle compilation failed");
|
2020-01-22 17:58:13 -05:00
|
|
|
assert!(bundle_path.exists());
|
|
|
|
|
|
|
|
let runtime_isolate = &mut Isolate::new(StartupData::None, true);
|
|
|
|
runtime_isolate.register_op("fetch_asset", op_fetch_asset(custom_libs));
|
|
|
|
|
|
|
|
deno_typescript::mksnapshot_bundle_ts(
|
|
|
|
runtime_isolate,
|
|
|
|
&snapshot_path,
|
|
|
|
&bundle_path,
|
|
|
|
&main_module_name,
|
|
|
|
)
|
|
|
|
.expect("Failed to create snapshot");
|
2019-09-02 17:07:11 -04:00
|
|
|
}
|