Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feat/upgrade_swc
Browse files Browse the repository at this point in the history
  • Loading branch information
stormslowly committed Oct 10, 2024
2 parents 5da9cda + 9097f36 commit 9d219f5
Show file tree
Hide file tree
Showing 53 changed files with 1,487 additions and 1,140 deletions.
12 changes: 12 additions & 0 deletions crates/binding/src/js_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ pub struct JsHooks {
pub _on_generate_file: Option<JsFunction>,
#[napi(ts_type = "() => Promise<void>;")]
pub build_start: Option<JsFunction>,
#[napi(ts_type = "(source: string, importer: string) => Promise<{ id: string }>;")]
pub resolve_id: Option<JsFunction>,
}

pub struct TsFnHooks {
pub build_start: Option<ThreadsafeFunction<(), ()>>,
pub generate_end: Option<ThreadsafeFunction<Value, ()>>,
pub load: Option<ThreadsafeFunction<String, Option<LoadResult>>>,
pub resolve_id: Option<ThreadsafeFunction<(String, String), Option<ResolveIdResult>>>,
pub _on_generate_file: Option<ThreadsafeFunction<WriteFile, ()>>,
}

Expand All @@ -79,6 +82,9 @@ impl TsFnHooks {
load: hooks.load.as_ref().map(|hook| unsafe {
ThreadsafeFunction::from_napi_value(env.raw(), hook.raw()).unwrap()
}),
resolve_id: hooks.resolve_id.as_ref().map(|hook| unsafe {
ThreadsafeFunction::from_napi_value(env.raw(), hook.raw()).unwrap()
}),
_on_generate_file: hooks._on_generate_file.as_ref().map(|hook| unsafe {
ThreadsafeFunction::from_napi_value(env.raw(), hook.raw()).unwrap()
}),
Expand All @@ -99,3 +105,9 @@ pub struct LoadResult {
#[napi(js_name = "type")]
pub content_type: String,
}

#[napi(object, use_nullable = true)]
pub struct ResolveIdResult {
pub id: String,
pub external: Option<bool>,
}
34 changes: 33 additions & 1 deletion crates/binding/src/js_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;
use std::sync::Arc;

use crate::js_hook::{LoadResult, TsFnHooks, WriteFile};
use crate::js_hook::{LoadResult, ResolveIdResult, TsFnHooks, WriteFile};

pub struct JsPlugin {
pub hooks: TsFnHooks,
Expand All @@ -9,6 +10,7 @@ use anyhow::{anyhow, Result};
use mako::ast::file::{Content, JsContent};
use mako::compiler::Context;
use mako::plugin::{Plugin, PluginGenerateEndParams, PluginLoadParam};
use mako::resolve::{ExternalResource, Resolution, ResolvedResource, ResolverResource};

impl Plugin for JsPlugin {
fn name(&self) -> &str {
Expand Down Expand Up @@ -47,6 +49,36 @@ impl Plugin for JsPlugin {
Ok(None)
}

fn resolve_id(
&self,
source: &str,
importer: &str,
_context: &Arc<Context>,
) -> Result<Option<ResolverResource>> {
if let Some(hook) = &self.hooks.resolve_id {
let x: Option<ResolveIdResult> =
hook.call((source.to_string(), importer.to_string()))?;
if let Some(x) = x {
if let Some(true) = x.external {
return Ok(Some(ResolverResource::External(ExternalResource {
source: source.to_string(),
external: source.to_string(),
script: None,
})));
}
return Ok(Some(ResolverResource::Resolved(ResolvedResource(
Resolution {
path: PathBuf::from(x.id),
query: None,
fragment: None,
package_json: None,
},
))));
}
}
Ok(None)
}

fn generate_end(&self, param: &PluginGenerateEndParams, _context: &Arc<Context>) -> Result<()> {
if let Some(hook) = &self.hooks.generate_end {
hook.call(serde_json::to_value(param)?)?
Expand Down
Loading

0 comments on commit 9d219f5

Please sign in to comment.