From bd9561f4de8f940ce6ed8b5eedfa84161a749c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 22 Jan 2020 20:18:01 +0100 Subject: Reland "Create an old program to be used in snapshot." (#3747) * read CLI assets from disk during snapshotting --- deno_typescript/lib.rs | 21 ++++++++++++++++++++- deno_typescript/ops.rs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) (limited to 'deno_typescript') diff --git a/deno_typescript/lib.rs b/deno_typescript/lib.rs index 016adcca0..ccad69a3d 100644 --- a/deno_typescript/lib.rs +++ b/deno_typescript/lib.rs @@ -16,6 +16,7 @@ use deno_core::PinnedBuf; use deno_core::StartupData; pub use ops::EmitResult; use ops::WrittenFile; +use std::collections::HashMap; use std::fs; use std::path::Path; use std::path::PathBuf; @@ -37,6 +38,7 @@ pub struct TSState { bundle: bool, exit_code: i32, emit_result: Option, + custom_assets: HashMap, /// A list of files emitted by typescript. WrittenFile is tuple of the form /// (url, corresponding_module, source_code) written_files: Vec, @@ -76,6 +78,7 @@ impl TSIsolate { let state = Arc::new(Mutex::new(TSState { bundle, + custom_assets: HashMap::new(), exit_code: 0, emit_result: None, written_files: Vec::new(), @@ -119,13 +122,24 @@ impl TSIsolate { self.isolate.execute("", source)?; Ok(self.state) } + + pub fn add_custom_assets(&mut self, custom_assets: Vec<(String, PathBuf)>) { + let mut state = self.state.lock().unwrap(); + for (name, path) in custom_assets { + state.custom_assets.insert(name, path); + } + } } pub fn compile_bundle( bundle: &Path, root_names: Vec, + custom_assets: Option>, ) -> Result>, ErrBox> { - let ts_isolate = TSIsolate::new(true); + let mut ts_isolate = TSIsolate::new(true); + if let Some(assets) = custom_assets { + ts_isolate.add_custom_assets(assets); + } let config_json = serde_json::json!({ "compilerOptions": { @@ -203,6 +217,10 @@ pub fn mksnapshot_bundle_ts( state: Arc>, ) -> Result<(), ErrBox> { let runtime_isolate = &mut Isolate::new(StartupData::None, true); + runtime_isolate.register_op( + "fetch_asset", + compiler_op(state.clone(), ops::json_op(ops::fetch_asset)), + ); let source_code_vec = std::fs::read(bundle)?; let source_code = std::str::from_utf8(&source_code_vec)?; @@ -255,6 +273,7 @@ pub fn get_asset(name: &str) -> Option<&'static str> { } match name { "bundle_loader.js" => Some(include_str!("bundle_loader.js")), + "bootstrap.ts" => Some("console.log(\"hello deno\");"), "typescript.d.ts" => inc!("typescript.d.ts"), "lib.esnext.d.ts" => inc!("lib.esnext.d.ts"), "lib.es2020.d.ts" => inc!("lib.es2020.d.ts"), diff --git a/deno_typescript/ops.rs b/deno_typescript/ops.rs index e45349591..0680d07b3 100644 --- a/deno_typescript/ops.rs +++ b/deno_typescript/ops.rs @@ -41,11 +41,16 @@ struct ReadFile { should_create_new_source_file: bool, } -pub fn read_file(_s: &mut TSState, v: Value) -> Result { +pub fn read_file(s: &mut TSState, v: Value) -> Result { let v: ReadFile = serde_json::from_value(v)?; let (module_name, source_code) = if v.file_name.starts_with("$asset$/") { let asset = v.file_name.replace("$asset$/", ""); - let source_code = crate::get_asset2(&asset)?.to_string(); + + let source_code = match s.custom_assets.get(&asset) { + Some(asset_path) => std::fs::read_to_string(&asset_path)?, + None => crate::get_asset2(&asset)?.to_string(), + }; + (asset, source_code) } else { assert!(!v.file_name.starts_with("$assets$"), "you meant $asset$"); @@ -110,6 +115,27 @@ pub fn resolve_module_names( Ok(json!(resolved)) } +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +struct FetchAssetArgs { + name: String, +} + +pub fn fetch_asset(s: &mut TSState, v: Value) -> Result { + let args: FetchAssetArgs = serde_json::from_value(v)?; + + if let Some(asset_path) = s.custom_assets.get(&args.name) { + let source_code = std::fs::read_to_string(&asset_path)?; + return Ok(json!(source_code)); + } + + if let Some(source_code) = crate::get_asset(&args.name) { + Ok(json!(source_code)) + } else { + panic!("op_fetch_asset bad asset {}", args.name) + } +} + #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] struct Exit { -- cgit v1.2.3