Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/binding/src/js_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub struct JsHooks {
ts_type = "(filePath: string) => Promise<{ content: string, type: 'css'|'js' } | void> | void;"
)]
pub load: Option<JsFunction>,
#[napi(ts_type = "(filePath: string) => Promise<bool> | bool;")]
pub load_include: Option<JsFunction>,
#[napi(ts_type = r#"(data: {
isFirstCompile: boolean;
time: number;
Expand Down Expand Up @@ -66,6 +68,7 @@ pub struct TsFnHooks {
pub build_start: Option<ThreadsafeFunction<(), ()>>,
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 _on_generate_file: Option<ThreadsafeFunction<WriteFile, ()>>,
}
Expand All @@ -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()
}),
Expand Down
11 changes: 11 additions & 0 deletions crates/binding/src/js_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ impl Plugin for JsPlugin {

fn load(&self, param: &PluginLoadParam, _context: &Arc<Context>) -> Result<Option<Content>> {
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<LoadResult> = hook.call(param.file.path.to_string_lossy().to_string())?;
if let Some(x) = x {
match x.content_type.as_str() {
Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }>;
}
```
Expand Down
1 change: 1 addition & 0 deletions docs/config.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }>;
}
```
Expand Down
15 changes: 8 additions & 7 deletions e2e/fixtures/plugins/plugins.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ module.exports = [
}
},
{
async load(path) {
if (path.endsWith('.bar')) {
return {
content: `export default () => <Foooo>.bar</Foooo>;`,
type: 'jsx',
};
}
async loadInclude(path) {
return path.endsWith('.bar');
},
async load() {
return {
content: `export default () => <Foooo>.bar</Foooo>;`,
type: 'jsx',
};
}
},
{
Expand Down
1 change: 1 addition & 0 deletions packages/mako/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface JsHooks {
load?: (
filePath: string,
) => Promise<{ content: string; type: 'css' | 'js' } | void> | void;
loadInclude?: (filePath: string) => Promise<bool> | bool;
generateEnd?: (data: {
isFirstCompile: boolean;
time: number;
Expand Down