diff --git a/crates/binding/src/js_hook.rs b/crates/binding/src/js_hook.rs index 444b47681..ba59d51cd 100644 --- a/crates/binding/src/js_hook.rs +++ b/crates/binding/src/js_hook.rs @@ -12,6 +12,8 @@ pub struct JsHooks { ts_type = "(filePath: string) => Promise<{ content: string, type: 'css'|'js' } | void> | void;" )] pub load: Option, + #[napi(ts_type = "(filePath: string) => Promise | bool;")] + pub load_include: Option, #[napi(ts_type = r#"(data: { isFirstCompile: boolean; time: number; @@ -66,6 +68,7 @@ pub struct TsFnHooks { pub build_start: Option>, pub generate_end: Option>, pub load: Option>>, + pub load_include: Option>>, pub resolve_id: Option>>, pub _on_generate_file: Option>, } @@ -82,6 +85,9 @@ impl TsFnHooks { load: hooks.load.as_ref().map(|hook| unsafe { ThreadsafeFunction::from_napi_value(env.raw(), hook.raw()).unwrap() }), + load_include: hooks.load_include.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() }), diff --git a/crates/binding/src/js_plugin.rs b/crates/binding/src/js_plugin.rs index 6d6b47ee5..4739e3fe9 100644 --- a/crates/binding/src/js_plugin.rs +++ b/crates/binding/src/js_plugin.rs @@ -26,6 +26,17 @@ impl Plugin for JsPlugin { fn load(&self, param: &PluginLoadParam, _context: &Arc) -> Result> { if let Some(hook) = &self.hooks.load { + if self.hooks.load_include.is_some() + && self + .hooks + .load_include + .as_ref() + .unwrap() + .call(param.file.path.to_string_lossy().to_string())? + == Some(false) + { + return Ok(None); + } let x: Option = hook.call(param.file.path.to_string_lossy().to_string())?; if let Some(x) = x { match x.content_type.as_str() { diff --git a/docs/config.md b/docs/config.md index 869cc77d6..c1f64314b 100644 --- a/docs/config.md +++ b/docs/config.md @@ -544,6 +544,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 }>; } ``` diff --git a/docs/config.zh-CN.md b/docs/config.zh-CN.md index 4e497031c..378e0324e 100644 --- a/docs/config.zh-CN.md +++ b/docs/config.zh-CN.md @@ -544,6 +544,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 }>; } ``` diff --git a/e2e/fixtures/plugins/plugins.config.js b/e2e/fixtures/plugins/plugins.config.js index 81e88e0bb..69cede8dc 100644 --- a/e2e/fixtures/plugins/plugins.config.js +++ b/e2e/fixtures/plugins/plugins.config.js @@ -11,13 +11,14 @@ module.exports = [ } }, { - async load(path) { - if (path.endsWith('.bar')) { - return { - content: `export default () => .bar;`, - type: 'jsx', - }; - } + async loadInclude(path) { + return path.endsWith('.bar'); + }, + async load() { + return { + content: `export default () => .bar;`, + type: 'jsx', + }; } }, { diff --git a/packages/mako/binding.d.ts b/packages/mako/binding.d.ts index 138b79371..0d9d02643 100644 --- a/packages/mako/binding.d.ts +++ b/packages/mako/binding.d.ts @@ -8,6 +8,7 @@ export interface JsHooks { load?: ( filePath: string, ) => Promise<{ content: string; type: 'css' | 'js' } | void> | void; + loadInclude?: (filePath: string) => Promise | bool; generateEnd?: (data: { isFirstCompile: boolean; time: number;