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

refactor: migrate to hook macro #5922

Merged
merged 3 commits into from
Mar 13, 2024
Merged

refactor: migrate to hook macro #5922

merged 3 commits into from
Mar 13, 2024

Conversation

ahabhgk
Copy link
Collaborator

@ahabhgk ahabhgk commented Mar 12, 2024

Summary

#[plugin]
#[derive(Debug, Default)]
pub struct Plugin {
  options: PluginOptions,
}

#[plugin_hook(AsyncSeries<Compilation> for Plugin, stage = 100)]
async process_assets(&self, compilation: &mut Compilation) -> Result<()> {
  // ...
}

expands to this (simplified):

#[derive(Debug, Default)]
pub struct Plugin {
  inner: Arc<PluginInner>,
}

#[derive(Debug, Default)]
struct PluginInner {
  options: PluginOptions,
}

impl Deref for Plugin {
  type Target = PluginInner
  // ...
}

struct process_assets {
  inner: Arc<PluginInner>,
}

impl process_assets {
  pub fn new(plugin: &Plugin) -> Self {
    Self { inner: Arc::clone(&plugin.inner) }
  }
}

impl Deref for process_assets {
  type Target = PluginInner
  // ...
}

#[async_trait]
impl AsyncSeries<Compilation> for process_assets {
  async run(&self, compilation: &mut Compilation) -> Result<()> {
    // ...
  }
  fn stage(&self) -> i32 {
    100
  }
}

The Inner is too common in the new hook design so made a macro to hide it, and this will make the migration more smooth

Require Documentation?

  • No
  • Yes, the corresponding rspack-website PR is __

@ahabhgk ahabhgk requested a review from hardfist as a code owner March 12, 2024 11:17
@github-actions github-actions bot added the team The issue/pr is created by the member of Rspack. label Mar 12, 2024
@ahabhgk ahabhgk requested a review from h-a-n-a March 12, 2024 12:36
@ahabhgk ahabhgk merged commit c62c5bc into main Mar 13, 2024
27 checks passed
@ahabhgk ahabhgk deleted the hook-macro branch March 13, 2024 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team The issue/pr is created by the member of Rspack.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants