From 7a21451d69a0768cdedcaab7b2a6840be4ec42f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?chencheng=20=28=E4=BA=91=E8=B0=A6=29?= Date: Sat, 12 Oct 2024 13:59:36 +0800 Subject: [PATCH] feat: add { isEntry } for resolve_id plugin hook (#1631) --- crates/binding/src/js_hook.rs | 12 ++++++++++-- crates/binding/src/js_plugin.rs | 14 ++++++++++---- crates/mako/src/build/analyze_deps.rs | 1 - crates/mako/src/plugin.rs | 9 ++++++++- crates/mako/src/resolve.rs | 14 ++++++++++---- docs/config.md | 2 +- docs/config.zh-CN.md | 2 +- e2e/fixtures/plugins/plugins.config.js | 4 ++-- packages/mako/binding.d.ts | 9 ++++++++- 9 files changed, 50 insertions(+), 17 deletions(-) diff --git a/crates/binding/src/js_hook.rs b/crates/binding/src/js_hook.rs index ba59d51cd..fcf0615ea 100644 --- a/crates/binding/src/js_hook.rs +++ b/crates/binding/src/js_hook.rs @@ -60,7 +60,9 @@ pub struct JsHooks { pub _on_generate_file: Option, #[napi(ts_type = "() => Promise;")] pub build_start: Option, - #[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, } @@ -69,7 +71,8 @@ pub struct TsFnHooks { pub generate_end: Option>, pub load: Option>>, pub load_include: Option>>, - pub resolve_id: Option>>, + pub resolve_id: + Option>>, pub _on_generate_file: Option>, } @@ -117,3 +120,8 @@ pub struct ResolveIdResult { pub id: String, pub external: Option, } + +#[napi(object)] +pub struct ResolveIdParams { + pub is_entry: bool, +} diff --git a/crates/binding/src/js_plugin.rs b/crates/binding/src/js_plugin.rs index 4739e3fe9..e42b76762 100644 --- a/crates/binding/src/js_plugin.rs +++ b/crates/binding/src/js_plugin.rs @@ -1,7 +1,7 @@ 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, @@ -9,7 +9,7 @@ pub struct JsPlugin { 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 { @@ -64,11 +64,17 @@ impl Plugin for JsPlugin { &self, source: &str, importer: &str, + params: &PluginResolveIdParams, _context: &Arc, ) -> Result> { if let Some(hook) = &self.hooks.resolve_id { - let x: Option = - hook.call((source.to_string(), importer.to_string()))?; + let x: Option = 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 { diff --git a/crates/mako/src/build/analyze_deps.rs b/crates/mako/src/build/analyze_deps.rs index 5286717e0..1bb374aa5 100644 --- a/crates/mako/src/build/analyze_deps.rs +++ b/crates/mako/src/build/analyze_deps.rs @@ -52,7 +52,6 @@ impl AnalyzeDeps { for dep in deps { let result = resolve( - // . &file.resolve_from(&context), &dep, &context.resolvers, diff --git a/crates/mako/src/plugin.rs b/crates/mako/src/plugin.rs index 1b4d27883..5d1d54225 100644 --- a/crates/mako/src/plugin.rs +++ b/crates/mako/src/plugin.rs @@ -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, } @@ -57,6 +62,7 @@ pub trait Plugin: Any + Send + Sync { &self, _source: &str, _importer: &str, + _params: &PluginResolveIdParams, _context: &Arc, ) -> Result> { Ok(None) @@ -256,10 +262,11 @@ impl PluginDriver { &self, source: &str, importer: &str, + params: &PluginResolveIdParams, context: &Arc, ) -> Result> { 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); } diff --git a/crates/mako/src/resolve.rs b/crates/mako/src/resolve.rs index 4a74f4462..f283eba13 100644 --- a/crates/mako/src/resolve.rs +++ b/crates/mako/src/resolve.rs @@ -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)] @@ -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); } diff --git a/docs/config.md b/docs/config.md index c1f64314b..2f6ed7799 100644 --- a/docs/config.md +++ b/docs/config.md @@ -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 }>; } ``` diff --git a/docs/config.zh-CN.md b/docs/config.zh-CN.md index 378e0324e..0e9845571 100644 --- a/docs/config.zh-CN.md +++ b/docs/config.zh-CN.md @@ -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 }>; } ``` diff --git a/e2e/fixtures/plugins/plugins.config.js b/e2e/fixtures/plugins/plugins.config.js index 69cede8dc..2dbeafc23 100644 --- a/e2e/fixtures/plugins/plugins.config.js +++ b/e2e/fixtures/plugins/plugins.config.js @@ -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 }; } diff --git a/packages/mako/binding.d.ts b/packages/mako/binding.d.ts index 0d9d02643..b99643f94 100644 --- a/packages/mako/binding.d.ts +++ b/packages/mako/binding.d.ts @@ -52,7 +52,11 @@ export interface JsHooks { }) => void; onGenerateFile?: (path: string, content: Buffer) => Promise; buildStart?: () => Promise; - resolveId?: (source: string, importer: string) => Promise<{ id: string }>; + resolveId?: ( + source: string, + importer: string, + { isEntry: bool }, + ) => Promise<{ id: string }>; } export interface WriteFile { path: string; @@ -66,6 +70,9 @@ export interface ResolveIdResult { id: string; external: boolean | null; } +export interface ResolveIdParams { + isEntry: boolean; +} export interface BuildParams { root: string; config: {