2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-01-31 07:43:32 -05:00
|
|
|
//! This example shows how to use swc to transpile TypeScript and JSX/TSX
|
|
|
|
//! modules.
|
|
|
|
//!
|
|
|
|
//! It will only transpile, not typecheck (like Deno's `--no-check` flag).
|
|
|
|
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use anyhow::anyhow;
|
|
|
|
use anyhow::bail;
|
|
|
|
use anyhow::Error;
|
|
|
|
use deno_ast::MediaType;
|
|
|
|
use deno_ast::ParseParams;
|
|
|
|
use deno_ast::SourceTextInfo;
|
|
|
|
use deno_core::resolve_import;
|
|
|
|
use deno_core::resolve_path;
|
|
|
|
use deno_core::JsRuntime;
|
|
|
|
use deno_core::ModuleLoader;
|
|
|
|
use deno_core::ModuleSource;
|
|
|
|
use deno_core::ModuleSourceFuture;
|
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_core::ModuleType;
|
2023-01-10 08:35:44 -05:00
|
|
|
use deno_core::ResolutionKind;
|
2022-01-31 07:43:32 -05:00
|
|
|
use deno_core::RuntimeOptions;
|
|
|
|
use futures::FutureExt;
|
|
|
|
|
|
|
|
struct TypescriptModuleLoader;
|
|
|
|
|
|
|
|
impl ModuleLoader for TypescriptModuleLoader {
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2023-01-10 08:35:44 -05:00
|
|
|
_kind: ResolutionKind,
|
2022-01-31 07:43:32 -05:00
|
|
|
) -> Result<ModuleSpecifier, Error> {
|
|
|
|
Ok(resolve_import(specifier, referrer)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<ModuleSpecifier>,
|
|
|
|
_is_dyn_import: bool,
|
|
|
|
) -> Pin<Box<ModuleSourceFuture>> {
|
|
|
|
let module_specifier = module_specifier.clone();
|
|
|
|
async move {
|
|
|
|
let path = module_specifier
|
|
|
|
.to_file_path()
|
|
|
|
.map_err(|_| anyhow!("Only file: URLs are supported."))?;
|
|
|
|
|
|
|
|
let media_type = MediaType::from(&path);
|
|
|
|
let (module_type, should_transpile) = match MediaType::from(&path) {
|
|
|
|
MediaType::JavaScript | MediaType::Mjs | MediaType::Cjs => {
|
|
|
|
(ModuleType::JavaScript, false)
|
|
|
|
}
|
|
|
|
MediaType::Jsx => (ModuleType::JavaScript, true),
|
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Dts
|
|
|
|
| MediaType::Dmts
|
|
|
|
| MediaType::Dcts
|
|
|
|
| MediaType::Tsx => (ModuleType::JavaScript, true),
|
|
|
|
MediaType::Json => (ModuleType::Json, false),
|
|
|
|
_ => bail!("Unknown extension {:?}", path.extension()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let code = std::fs::read_to_string(&path)?;
|
|
|
|
let code = if should_transpile {
|
|
|
|
let parsed = deno_ast::parse_module(ParseParams {
|
|
|
|
specifier: module_specifier.to_string(),
|
2022-05-20 16:40:55 -04:00
|
|
|
text_info: SourceTextInfo::from_string(code),
|
2022-01-31 07:43:32 -05:00
|
|
|
media_type,
|
|
|
|
capture_tokens: false,
|
|
|
|
scope_analysis: false,
|
|
|
|
maybe_syntax: None,
|
|
|
|
})?;
|
|
|
|
parsed.transpile(&Default::default())?.text
|
|
|
|
} else {
|
|
|
|
code
|
|
|
|
};
|
|
|
|
let module = ModuleSource {
|
2022-05-05 07:16:25 -04:00
|
|
|
code: code.into_bytes().into_boxed_slice(),
|
2022-01-31 07:43:32 -05:00
|
|
|
module_type,
|
|
|
|
module_url_specified: module_specifier.to_string(),
|
|
|
|
module_url_found: module_specifier.to_string(),
|
|
|
|
};
|
|
|
|
Ok(module)
|
|
|
|
}
|
|
|
|
.boxed_local()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<(), Error> {
|
|
|
|
let args: Vec<String> = std::env::args().collect();
|
|
|
|
if args.len() < 2 {
|
|
|
|
println!("Usage: target/examples/debug/ts_module_loader <path_to_module>");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2023-01-05 14:29:50 -05:00
|
|
|
let main_url = &args[1];
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("Run {main_url}");
|
2022-01-31 07:43:32 -05:00
|
|
|
|
|
|
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
|
|
|
module_loader: Some(Rc::new(TypescriptModuleLoader)),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
2023-01-05 14:29:50 -05:00
|
|
|
let main_module = resolve_path(main_url)?;
|
2022-01-31 07:43:32 -05:00
|
|
|
|
|
|
|
let future = async move {
|
|
|
|
let mod_id = js_runtime.load_main_module(&main_module, None).await?;
|
2022-06-20 08:40:57 -04:00
|
|
|
let result = js_runtime.mod_evaluate(mod_id);
|
2022-01-31 07:43:32 -05:00
|
|
|
js_runtime.run_event_loop(false).await?;
|
2022-06-20 08:40:57 -04:00
|
|
|
result.await?
|
2022-01-31 07:43:32 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
tokio::runtime::Builder::new_current_thread()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
.block_on(future)
|
|
|
|
}
|