Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat: support unplugin context (#1728)" #1737

Merged
merged 1 commit into from
Jan 2, 2025
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
27 changes: 12 additions & 15 deletions crates/binding/src/js_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use napi::NapiRaw;
use napi_derive::napi;
use serde_json::Value;

use crate::js_plugin::PluginContext;
use crate::threadsafe_function::ThreadsafeFunction;

#[napi(object)]
Expand Down Expand Up @@ -82,21 +81,19 @@ pub struct JsHooks {
pub transform_include: Option<JsFunction>,
}

type ResolveIdFuncParams = (PluginContext, String, String, ResolveIdParams);

pub struct TsFnHooks {
pub build_start: Option<ThreadsafeFunction<PluginContext, ()>>,
pub build_end: Option<ThreadsafeFunction<PluginContext, ()>>,
pub write_bundle: Option<ThreadsafeFunction<PluginContext, ()>>,
pub generate_end: Option<ThreadsafeFunction<(PluginContext, Value), ()>>,
pub load: Option<ThreadsafeFunction<(PluginContext, String), Option<LoadResult>>>,
pub load_include: Option<ThreadsafeFunction<(PluginContext, String), Option<bool>>>,
pub watch_changes: Option<ThreadsafeFunction<(PluginContext, String, WatchChangesParams), ()>>,
pub resolve_id: Option<ThreadsafeFunction<ResolveIdFuncParams, Option<ResolveIdResult>>>,
pub _on_generate_file: Option<ThreadsafeFunction<(PluginContext, WriteFile), ()>>,
pub transform:
Option<ThreadsafeFunction<(PluginContext, String, String), Option<TransformResult>>>,
pub transform_include: Option<ThreadsafeFunction<(PluginContext, String), Option<bool>>>,
pub build_start: Option<ThreadsafeFunction<(), ()>>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removal of PluginContext from TsFnHooks may lead to issues if any functionality relied on the context being passed. Ensure that this change does not break any existing functionality that depends on PluginContext.

pub build_end: Option<ThreadsafeFunction<(), ()>>,
pub write_bundle: Option<ThreadsafeFunction<(), ()>>,
pub generate_end: Option<ThreadsafeFunction<Value, ()>>,
pub load: Option<ThreadsafeFunction<String, Option<LoadResult>>>,
pub load_include: Option<ThreadsafeFunction<String, Option<bool>>>,
pub watch_changes: Option<ThreadsafeFunction<(String, WatchChangesParams), ()>>,
pub resolve_id:
Option<ThreadsafeFunction<(String, String, ResolveIdParams), Option<ResolveIdResult>>>,
pub _on_generate_file: Option<ThreadsafeFunction<WriteFile, ()>>,
pub transform: Option<ThreadsafeFunction<(String, String), Option<TransformResult>>>,
pub transform_include: Option<ThreadsafeFunction<String, Option<bool>>>,
}

impl TsFnHooks {
Expand Down
119 changes: 26 additions & 93 deletions crates/binding/src/js_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use mako::ast::file::{Content, JsContent};
use mako::compiler::Context;
use mako::plugin::{Plugin, PluginGenerateEndParams, PluginLoadParam, PluginResolveIdParams};
use mako::resolve::{ExternalResource, Resolution, ResolvedResource, ResolverResource};
use napi_derive::napi;

use crate::js_hook::{
LoadResult, ResolveIdParams, ResolveIdResult, TransformResult, TsFnHooks, WatchChangesParams,
Expand All @@ -28,29 +27,6 @@ fn content_from_result(result: TransformResult) -> Result<Content> {
}
}

#[napi]
pub struct PluginContext {
context: Arc<Context>,
}

#[napi]
impl PluginContext {
#[napi]
pub fn warn(&self, msg: String) {
println!("WARN: {}", msg)
}
#[napi]
pub fn error(&self, msg: String) {
println!("ERROR: {}", msg)
}
#[napi]
pub fn emit_file(&self, origin_path: String, output_path: String) {
let mut assets_info = self.context.assets_info.lock().unwrap();
assets_info.insert(origin_path, output_path);
drop(assets_info);
}
}

pub struct JsPlugin {
pub hooks: TsFnHooks,
pub name: Option<String>,
Expand All @@ -66,33 +42,27 @@ impl Plugin for JsPlugin {
self.enforce.as_deref()
}

fn build_start(&self, context: &Arc<Context>) -> Result<()> {
fn build_start(&self, _context: &Arc<Context>) -> Result<()> {
if let Some(hook) = &self.hooks.build_start {
hook.call(PluginContext {
context: context.clone(),
})?
hook.call(())?
}
Ok(())
}

fn load(&self, param: &PluginLoadParam, context: &Arc<Context>) -> Result<Option<Content>> {
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((
PluginContext {
context: context.clone(),
},
param.file.path.to_string_lossy().to_string(),
))? == Some(false)
&& 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((
PluginContext {
context: context.clone(),
},
param.file.path.to_string_lossy().to_string(),
))?;
let x: Option<LoadResult> = hook.call(param.file.path.to_string_lossy().to_string())?;
if let Some(x) = x {
return content_from_result(TransformResult {
content: x.content,
Expand All @@ -109,13 +79,10 @@ impl Plugin for JsPlugin {
source: &str,
importer: &str,
params: &PluginResolveIdParams,
context: &Arc<Context>,
_context: &Arc<Context>,
) -> Result<Option<ResolverResource>> {
if let Some(hook) = &self.hooks.resolve_id {
let x: Option<ResolveIdResult> = hook.call((
PluginContext {
context: context.clone(),
},
source.to_string(),
importer.to_string(),
ResolveIdParams {
Expand Down Expand Up @@ -143,31 +110,21 @@ impl Plugin for JsPlugin {
Ok(None)
}

fn generate_end(&self, param: &PluginGenerateEndParams, context: &Arc<Context>) -> Result<()> {
fn generate_end(&self, param: &PluginGenerateEndParams, _context: &Arc<Context>) -> Result<()> {
// keep generate_end for compatibility
// since build_end does not have none error params in unplugin's api spec
if let Some(hook) = &self.hooks.generate_end {
hook.call((
PluginContext {
context: context.clone(),
},
serde_json::to_value(param)?,
))?
hook.call(serde_json::to_value(param)?)?
}
if let Some(hook) = &self.hooks.build_end {
hook.call(PluginContext {
context: context.clone(),
})?
hook.call(())?
}
Ok(())
}

fn watch_changes(&self, id: &str, event: &str, context: &Arc<Context>) -> Result<()> {
fn watch_changes(&self, id: &str, event: &str, _context: &Arc<Context>) -> Result<()> {
if let Some(hook) = &self.hooks.watch_changes {
hook.call((
PluginContext {
context: context.clone(),
},
id.to_string(),
WatchChangesParams {
event: event.to_string(),
Expand All @@ -177,31 +134,19 @@ impl Plugin for JsPlugin {
Ok(())
}

fn write_bundle(&self, context: &Arc<Context>) -> Result<()> {
fn write_bundle(&self, _context: &Arc<Context>) -> Result<()> {
if let Some(hook) = &self.hooks.write_bundle {
hook.call(PluginContext {
context: context.clone(),
})?
hook.call(())?
}
Ok(())
}

fn before_write_fs(
&self,
path: &std::path::Path,
content: &[u8],
context: &Arc<Context>,
) -> Result<()> {
fn before_write_fs(&self, path: &std::path::Path, content: &[u8]) -> Result<()> {
if let Some(hook) = &self.hooks._on_generate_file {
hook.call((
PluginContext {
context: context.clone(),
},
WriteFile {
path: path.to_string_lossy().to_string(),
content: content.to_vec(),
},
))?;
hook.call(WriteFile {
path: path.to_string_lossy().to_string(),
content: content.to_vec(),
})?;
}
Ok(())
}
Expand All @@ -210,16 +155,10 @@ impl Plugin for JsPlugin {
&self,
content: &mut Content,
path: &str,
context: &Arc<Context>,
_context: &Arc<Context>,
) -> Result<Option<Content>> {
if let Some(hook) = &self.hooks.transform_include {
if hook.call((
PluginContext {
context: context.clone(),
},
path.to_string(),
))? == Some(false)
{
if hook.call(path.to_string())? == Some(false) {
return Ok(None);
}
}
Expand All @@ -231,13 +170,7 @@ impl Plugin for JsPlugin {
_ => return Ok(None),
};

let result: Option<TransformResult> = hook.call((
PluginContext {
context: context.clone(),
},
content_str,
path.to_string(),
))?;
let result: Option<TransformResult> = hook.call((content_str, path.to_string()))?;

if let Some(result) = result {
return content_from_result(result).map(Some);
Expand Down
10 changes: 2 additions & 8 deletions crates/mako/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,7 @@ pub trait Plugin: Any + Send + Sync {
Ok(())
}

fn before_write_fs(
&self,
_path: &Path,
_content: &[u8],
_context: &Arc<Context>,
) -> Result<()> {
fn before_write_fs(&self, _path: &Path, _content: &[u8]) -> Result<()> {
Ok(())
}

Expand Down Expand Up @@ -427,10 +422,9 @@ impl PluginDriver {
&self,
path: P,
content: C,
context: &Arc<Context>,
) -> Result<()> {
for p in &self.plugins {
p.before_write_fs(path.as_ref(), content.as_ref(), context)?;
p.before_write_fs(path.as_ref(), content.as_ref())?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/plugins/bundless_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl BundlessCompiler {

self.context
.plugin_driver
.before_write_fs(&to, content.as_ref(), &self.context)
.before_write_fs(&to, content.as_ref())
.unwrap();

if !self.context.config.output.skip_write {
Expand Down
14 changes: 6 additions & 8 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ Notice: When using `"node"`, you also need to set `dynamicImportToRequire` to `t
Specify the plugins to use.

```ts
// JSHooks
{
name?: string;
enforce?: "pre" | "post";
Expand All @@ -598,15 +599,12 @@ Specify the plugins to use.
}
```

And you can also use this methods in hook functions.
JSHooks is a set of hook functions used to extend the compilation process of Mako.

- `this.emitFile({ type: 'asset', fileName: string, source: string | Uint8Array })`, emit a file
- `this.warn(message: string)`, emit a warning
- `this.error(message: string)`, emit a error
- `this.parse(code: string)`, parse the code (CURRENTLY NOT SUPPORTED)
- `this.addWatchFile(filePath: string)`, add a watch file (CURRENTLY NOT SUPPORTED)

Plugins is compatible with [unplugin](https://unplugin.unjs.io/), so you can use plugins from unplugin like [unplugin-icons](https://github.com/unplugin/unplugin-icons), [unplugin-replace](https://github.com/unplugin/unplugin-replace) and so on.
- `name`, plugin name
- `buildStart`, called before Build starts
- `load`, used to load files, return file content and type, type supports `css`, `js`, `jsx`, `ts`, `tsx`
- `generateEnd`, called after Generate completes, `isFirstCompile` can be used to determine if it is the first compilation, `time` is the compilation time, and `stats` is the compilation statistics information

### progress

Expand Down
14 changes: 6 additions & 8 deletions docs/config.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ import(/* webpackIgnore: true */ "./foo");
指定使用的插件。

```ts
// JSHooks
{
name?: string;
enforce?: "pre" | "post";
Expand All @@ -596,15 +597,12 @@ import(/* webpackIgnore: true */ "./foo");
}
```

你还可以在 hook 函数里用以下方法
JSHooks 是一组用来扩展 Mako 编译过程的钩子函数

- `this.emitFile({ type: 'asset', fileName: string, source: string | Uint8Array })`, 添加文件到输出目录
- `this.warn(message: string)`, 添加一个警告
- `this.error(message: string)`, 添加一个错误
- `this.parse(code: string)`, 解析代码 (CURRENTLY NOT SUPPORTED)
- `this.addWatchFile(filePath: string)`, 添加一个监听文件 (CURRENTLY NOT SUPPORTED)

Plugins 兼容 [unplugin](https://unplugin.unjs.io/),所以你可以使用 unplugin 的插件,比如 [unplugin-icons](https://github.com/unplugin/unplugin-icons), [unplugin-replace](https://github.com/unplugin/unplugin-replace) 等。
- `name`,插件名称
- `buildStart`,构建开始前调用
- `load`,用于加载文件,返回文件内容和类型,类型支持 `css`、`js`、`jsx`、`ts`、`tsx`
- `generateEnd`,生成完成后调用,`isFirstCompile` 可用于判断是否为首次编译,`time` 为编译时间,`stats` 是编译统计信息

### progress

Expand Down
7 changes: 0 additions & 7 deletions e2e/fixtures/plugins.context/expect.js

This file was deleted.

6 changes: 0 additions & 6 deletions e2e/fixtures/plugins.context/mako.config.json

This file was deleted.

6 changes: 0 additions & 6 deletions e2e/fixtures/plugins.context/plugin.js

This file was deleted.

27 changes: 0 additions & 27 deletions e2e/fixtures/plugins.context/plugins.config.js

This file was deleted.

1 change: 0 additions & 1 deletion e2e/fixtures/plugins.context/src/foo.hoo

This file was deleted.

1 change: 0 additions & 1 deletion e2e/fixtures/plugins.context/src/index.tsx

This file was deleted.

Loading
Loading