Skip to content

Commit

Permalink
feat: add { isEntry } for resolve_id plugin hook (#1631)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc authored Oct 12, 2024
1 parent bcdb3c4 commit 7a21451
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 17 deletions.
12 changes: 10 additions & 2 deletions crates/binding/src/js_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ 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 }>;")]
#[napi(
ts_type = "(source: string, importer: string, { isEntry: bool }) => Promise<{ id: string }>;"
)]
pub resolve_id: Option<JsFunction>,
}

Expand All @@ -69,7 +71,8 @@ pub struct TsFnHooks {
pub generate_end: Option<ThreadsafeFunction<Value, ()>>,
pub load: Option<ThreadsafeFunction<String, Option<LoadResult>>>,
pub load_include: Option<ThreadsafeFunction<String, Option<bool>>>,
pub resolve_id: Option<ThreadsafeFunction<(String, String), Option<ResolveIdResult>>>,
pub resolve_id:
Option<ThreadsafeFunction<(String, String, ResolveIdParams), Option<ResolveIdResult>>>,
pub _on_generate_file: Option<ThreadsafeFunction<WriteFile, ()>>,
}

Expand Down Expand Up @@ -117,3 +120,8 @@ pub struct ResolveIdResult {
pub id: String,
pub external: Option<bool>,
}

#[napi(object)]
pub struct ResolveIdParams {
pub is_entry: bool,
}
14 changes: 10 additions & 4 deletions crates/binding/src/js_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::path::PathBuf;
use std::sync::Arc;

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

pub struct JsPlugin {
pub hooks: TsFnHooks,
}
use anyhow::{anyhow, Result};
use mako::ast::file::{Content, JsContent};
use mako::compiler::Context;
use mako::plugin::{Plugin, PluginGenerateEndParams, PluginLoadParam};
use mako::plugin::{Plugin, PluginGenerateEndParams, PluginLoadParam, PluginResolveIdParams};
use mako::resolve::{ExternalResource, Resolution, ResolvedResource, ResolverResource};

impl Plugin for JsPlugin {
Expand Down Expand Up @@ -64,11 +64,17 @@ impl Plugin for JsPlugin {
&self,
source: &str,
importer: &str,
params: &PluginResolveIdParams,
_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()))?;
let x: Option<ResolveIdResult> = hook.call((
source.to_string(),
importer.to_string(),
ResolveIdParams {
is_entry: params.is_entry,
},
))?;
if let Some(x) = x {
if let Some(true) = x.external {
return Ok(Some(ResolverResource::External(ExternalResource {
Expand Down
1 change: 0 additions & 1 deletion crates/mako/src/build/analyze_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ impl AnalyzeDeps {

for dep in deps {
let result = resolve(
// .
&file.resolve_from(&context),
&dep,
&context.resolvers,
Expand Down
9 changes: 8 additions & 1 deletion crates/mako/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ pub struct PluginLoadParam<'a> {
pub file: &'a File,
}

#[derive(Debug)]
pub struct PluginResolveIdParams {
pub is_entry: bool,
}

pub struct PluginParseParam<'a> {
pub file: &'a File,
}
Expand Down Expand Up @@ -57,6 +62,7 @@ pub trait Plugin: Any + Send + Sync {
&self,
_source: &str,
_importer: &str,
_params: &PluginResolveIdParams,
_context: &Arc<Context>,
) -> Result<Option<ResolverResource>> {
Ok(None)
Expand Down Expand Up @@ -256,10 +262,11 @@ impl PluginDriver {
&self,
source: &str,
importer: &str,
params: &PluginResolveIdParams,
context: &Arc<Context>,
) -> Result<Option<ResolverResource>> {
for plugin in &self.plugins {
let ret = plugin.resolve_id(source, importer, context)?;
let ret = plugin.resolve_id(source, importer, params, context)?;
if ret.is_some() {
return Ok(ret);
}
Expand Down
14 changes: 10 additions & 4 deletions crates/mako/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::config::{
};
use crate::features::rsc::Rsc;
use crate::module::{Dependency, ResolveType};
use crate::plugin::PluginResolveIdParams;
use crate::utils::create_cached_regex;

#[derive(Debug, Error)]
Expand Down Expand Up @@ -52,10 +53,15 @@ pub fn resolve(
crate::mako_profile_scope!("resolve", &dep.source);

// plugin first
if let Some(resolved) = context
.plugin_driver
.resolve_id(&dep.source, path, context)?
{
if let Some(resolved) = context.plugin_driver.resolve_id(
&dep.source,
path,
// it's a compatibility feature for unplugin hooks
// is_entry is always false for dependencies
// since entry file does not need be be resolved
&PluginResolveIdParams { is_entry: false },
context,
)? {
return Ok(resolved);
}

Expand Down
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ Specify the plugins to use.
}) => void;
load?: (filePath: string) => Promise<{ content: string, type: 'css'|'js'|'jsx'|'ts'|'tsx' }>;
loadInclude?: (filePath: string) => boolean;
resolveId?: (id: string, importer: string) => Promise<{ id: string, external: bool }>;
resolveId?: (id: string, importer: string, { isEntry: bool }) => Promise<{ id: string, external: bool }>;
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/config.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ e.g.
}) => void;
load?: (filePath: string) => Promise<{ content: string, type: 'css'|'js'|'jsx'|'ts'|'tsx' }>;
loadInclude?: (filePath: string) => boolean;
resolveId?: (id: string, importer: string) => Promise<{ id: string, external: bool }>;
resolveId?: (id: string, importer: string, { isEntry: bool }) => Promise<{ id: string, external: bool }>;
}
```

Expand Down
4 changes: 2 additions & 2 deletions e2e/fixtures/plugins/plugins.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ module.exports = [
}
},
{
async resolveId(source, importer) {
console.log('resolveId', source, importer);
async resolveId(source, importer, options) {
console.log('resolveId', source, importer, options);
if (source === 'resolve_id') {
return { id: require('path').join(__dirname, 'resolve_id_mock.js'), external: false };
}
Expand Down
9 changes: 8 additions & 1 deletion packages/mako/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export interface JsHooks {
}) => void;
onGenerateFile?: (path: string, content: Buffer) => Promise<void>;
buildStart?: () => Promise<void>;
resolveId?: (source: string, importer: string) => Promise<{ id: string }>;
resolveId?: (
source: string,
importer: string,
{ isEntry: bool },
) => Promise<{ id: string }>;
}
export interface WriteFile {
path: string;
Expand All @@ -66,6 +70,9 @@ export interface ResolveIdResult {
id: string;
external: boolean | null;
}
export interface ResolveIdParams {
isEntry: boolean;
}
export interface BuildParams {
root: string;
config: {
Expand Down

0 comments on commit 7a21451

Please sign in to comment.